473,772 Members | 3,665 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Draw rounded rectangle inside a specific area

Hi, how can i draw a rounded rectange, with a border within a specified
area?

i.e. if i have a Rectangle with width and height of 100, how can i draw
a rectange with 2 pixel border inside of the original one? (the current
attempts all draw the border just outside of the original rectangle).

At the moment im using the following code, but it allows the rectangle
to grow - which i definately dont want!

m_edgePath = new GraphicsPath();

Point topLeft = new Point(this.Clie ntRectangle.Lef t +
m_borderWidth, this.ClientRect angle.Top + m_borderWidth);
Point topRight = new Point(this.Clie ntRectangle.Rig ht -
m_curveSize.Wid th - m_borderWidth, this.ClientRect angle.Top +
m_borderWidth);
Point bottomRight = new Point(this.Clie ntRectangle.Rig ht -
m_curveSize.Wid th - m_borderWidth, this.ClientRect angle.Bottom -
m_curveSize.Hei ght - m_borderWidth);
Point bottomLeft = new Point(this.Clie ntRectangle.Lef t +
m_borderWidth, this.ClientRect angle.Bottom - m_curveSize.Hei ght -
m_borderWidth);

m_edgePath.AddA rc(topLeft.X, topLeft.Y, m_curveSize.Wid th,
m_curveSize.Hei ght, 180, 90);
m_edgePath.AddA rc(topRight.X, topRight.Y, m_curveSize.Wid th,
m_curveSize.Hei ght, 270, 90);
m_edgePath.AddA rc(bottomRight. X, bottomRight.Y, m_curveSize.Wid th,
m_curveSize.Hei ght, 0, 90);
m_edgePath.AddA rc(bottomLeft.X , bottomLeft.Y, m_curveSize.Wid th,
m_curveSize.Hei ght, 90, 90);
m_edgePath.Clos eFigure();

Region preWidenRegion = new Region(m_edgePa th);

m_edgePath.Wide n(new Pen(Color.Black , m_borderWidth)) ;

Region postWidenRegion = new Region(m_edgePa th);

m_edgeRegion = new Region();
m_edgeRegion.Ma keEmpty();

m_edgeRegion.Un ion(preWidenReg ion);
m_edgeRegion.Un ion(postWidenRe gion);


Thanks,
Jan 18 '06 #1
7 10692
Hi Mark,
Not sure if there is no direct way of doing it, but it should be simple
doing something like:

1- Draw the 4 lines that will conform the rectangle , you do it 4 pixels
short to leave space for the corners. like :
DrawLine( thePen, startX+2 , startY, endX-2 , startY ) // the top
horizontal
DrawLine( thePen, startX , startY + 2, startX , startY-2 ) //the
left vertical
2- using DrawArc you draw the corners ,
DrawArc( thePen, new Rectangle( startX, startY, 2, 2) , 270, 360 )
//top left corner
it should work

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Mark Ingram" <no****@nowhere .com> wrote in message
news:Ok******** *****@tk2msftng p13.phx.gbl...
Hi, how can i draw a rounded rectange, with a border within a specified
area?

i.e. if i have a Rectangle with width and height of 100, how can i draw a
rectange with 2 pixel border inside of the original one? (the current
attempts all draw the border just outside of the original rectangle).

At the moment im using the following code, but it allows the rectangle to
grow - which i definately dont want!

m_edgePath = new GraphicsPath();

Point topLeft = new Point(this.Clie ntRectangle.Lef t + m_borderWidth,
this.ClientRect angle.Top + m_borderWidth);
Point topRight = new Point(this.Clie ntRectangle.Rig ht -
m_curveSize.Wid th - m_borderWidth, this.ClientRect angle.Top +
m_borderWidth);
Point bottomRight = new Point(this.Clie ntRectangle.Rig ht -
m_curveSize.Wid th - m_borderWidth, this.ClientRect angle.Bottom -
m_curveSize.Hei ght - m_borderWidth);
Point bottomLeft = new Point(this.Clie ntRectangle.Lef t + m_borderWidth,
this.ClientRect angle.Bottom - m_curveSize.Hei ght - m_borderWidth);

m_edgePath.AddA rc(topLeft.X, topLeft.Y, m_curveSize.Wid th,
m_curveSize.Hei ght, 180, 90);
m_edgePath.AddA rc(topRight.X, topRight.Y, m_curveSize.Wid th,
m_curveSize.Hei ght, 270, 90);
m_edgePath.AddA rc(bottomRight. X, bottomRight.Y, m_curveSize.Wid th,
m_curveSize.Hei ght, 0, 90);
m_edgePath.AddA rc(bottomLeft.X , bottomLeft.Y, m_curveSize.Wid th,
m_curveSize.Hei ght, 90, 90);
m_edgePath.Clos eFigure();

Region preWidenRegion = new Region(m_edgePa th);

m_edgePath.Wide n(new Pen(Color.Black , m_borderWidth)) ;

Region postWidenRegion = new Region(m_edgePa th);

m_edgeRegion = new Region();
m_edgeRegion.Ma keEmpty();

m_edgeRegion.Un ion(preWidenReg ion);
m_edgeRegion.Un ion(postWidenRe gion);


Thanks,

Jan 18 '06 #2
Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi Mark,
Not sure if there is no direct way of doing it, but it should be simple
doing something like:

1- Draw the 4 lines that will conform the rectangle , you do it 4 pixels
short to leave space for the corners. like :
DrawLine( thePen, startX+2 , startY, endX-2 , startY ) // the top
horizontal
DrawLine( thePen, startX , startY + 2, startX , startY-2 ) //the
left vertical
2- using DrawArc you draw the corners ,
DrawArc( thePen, new Rectangle( startX, startY, 2, 2) , 270, 360 )
//top left corner
it should work

Its not the actual arcs or lines that are causing the problems, its the
drawing with the pen, if you use anything bigger than width 1, it starts
growing outside of the rectangle.

i.e. if you use PenWidth of 2, the rectangle will increase in size to
102x102 (extra 1 pixel on each side).
Jan 18 '06 #3
Hi,

"Mark Ingram" <no****@nowhere .com> wrote in message
news:Ol******** ******@TK2MSFTN GP10.phx.gbl...
Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi Mark,
Not sure if there is no direct way of doing it, but it should be simple
doing something like:

1- Draw the 4 lines that will conform the rectangle , you do it 4 pixels
short to leave space for the corners. like :
DrawLine( thePen, startX+2 , startY, endX-2 , startY ) // the top
horizontal
DrawLine( thePen, startX , startY + 2, startX , startY-2 ) //the
left vertical
2- using DrawArc you draw the corners ,
DrawArc( thePen, new Rectangle( startX, startY, 2, 2) , 270,
360 ) //top left corner
it should work

Its not the actual arcs or lines that are causing the problems, its the
drawing with the pen, if you use anything bigger than width 1, it starts
growing outside of the rectangle.

i.e. if you use PenWidth of 2, the rectangle will increase in size to
102x102 (extra 1 pixel on each side).


Well, what do you expect? if the pen is bigger than 1 you should calculate
accordingly ,

Another question, if this is the same pen used to draw the lines you should
have the same problem no? or it start at the selected X and takes 2 pixels
to the right?
What happens in the right side?
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 18 '06 #4
Ignacio Machin ( .NET/ C# MVP ) wrote:

Well, what do you expect? if the pen is bigger than 1 you should calculate
accordingly ,

Another question, if this is the same pen used to draw the lines you should
have the same problem no? or it start at the selected X and takes 2 pixels
to the right?
What happens in the right side?


If i draw with a 2 pixel width pen, there is a 1 pixel border all the
way around the outside of the rectangle, i want the 2 pixel border to be
inside the rectangle. normally this wouldnt be a problem, but with the
rounded edges its being a right pain. any help is much appreciated.
Jan 18 '06 #5
Take a look at the Pen.Alignment property and the PenAlignment
enumeration. Have you tried setting the Alignment property to Inset?
The default is Center.

Perhaps this will help you.

Jan 18 '06 #6
Hi,

"Chris Dunaway" <du******@gmail .com> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.com...
Take a look at the Pen.Alignment property and the PenAlignment
enumeration. Have you tried setting the Alignment property to Inset?
The default is Center.


I think this is the solution :)

Also note that with a little math he could had solve this before, if you see
that the draw is centered in the pen, well then just shrink it accordingly ,
(1/2 the width of the pen)
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 18 '06 #7
Chris Dunaway wrote:
Take a look at the Pen.Alignment property and the PenAlignment
enumeration. Have you tried setting the Alignment property to Inset?
The default is Center.

Perhaps this will help you.


Thats great, should do the trick nicely :)
Jan 19 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1859
by: dunkonu | last post by:
I am new to VB.net, can anybody help me how to create a form that ask user to enter the "WIDTH" and "HEIGHT" and draw a box display in the textbox? thanks
2
8410
by: Starlite | last post by:
I am using Microsoft visual c++ 6, and I want to draw a shape (say a rectangle) on a view. The code that I have used is below, but this draws a solid -filled rectangle, what I want is a displayed shape with a semi-transparent color i.e. we should still be able to see whatever is beneath the shape. Can anyone help me? Thanks. void OnPaint() { CPaintDC dc(this); CPen pPen(PS_SOLID, 1, RGB(0,0,0));
15
9080
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for the purpose of learning to work with C++. It therefore makes some sense for me to give the situation the amount of consideration presented below. To be quite honest, I'm amazed at the amount there is to say about such a seemingly simple...
3
4266
by: Richard | last post by:
I have a requirement to put a GDI style circle or rectangle border around the selected row of a datagrid/ It will overlap into the row above and below the selected row. Doing this in a the OnPaint of a subclassed DataGridTextBoxColum dos not seem like a practical way to do it. I have subclassed a DataGrid and overridden the OnPaint as such:
3
10329
by: Colin McGuire | last post by:
Hi there. I have written a small procedure to draw various shapes on things. A bit of it is shown below. Private Sub drawShape(ByVal shapeType As Integer, ByRef g As Graphics) Select Case shapeType Case 1 : g.DrawRectangle(New Pen(Color.Black), 0, 0, 50, 10) Case 2 'draw a circle Case 3 'draw a triangle Case 4 'draw other shape Case 5 'draw other shape
0
2035
by: cmbardon | last post by:
I'm trying to create a form with some rounded controls, and I'm having some problems with the rounding not working correctly. I've tried a couple of different methods for creating the graphics path, including the one from the GDI+ FAQ. It's a pretty simple path, and mathematically it looks just fine. When I apply it to a control though, I have two problems. First, the corners on the right side of the control are obviously not rounded...
1
3777
by: Jeff Waskiewicz | last post by:
Hello All, I'm trying to solve a nagging problem. The goal is to draw a rectangle over the top of all the other controls on a form. Specifically, over a ChartFX control. The user would draw the rectangle using the right mouse button to represent the area of the chart they want to zoom on. I haev been able to draw the rectangle on a blank form but I cannot get it to draw on top of other controls. I have pasted in the code i am using ....
1
2577
by: kidelectric | last post by:
The issue I am having is that I would like to be able to drag-and-drop div elements that have rounded corners.* Since these elements will be dynamically created (including background color), I could not use the "standard" rounded corner method of sliced images for the corners (since that would only allow a certain color to match) I tried using a corner-rounding js tool from http://openrico.org, but it failed miserably (in both IE and Opera) ...
1
5133
by: kummu4help | last post by:
hi, i want to draw rectangle based on mousedrag event. if user dragging the mouse, then the rectangle on the applet should increase or decrease basing on current mouse coordinates. i have the following code. in the following code i am using SelectionArea class which extends a canvas on which i am performing drawing operation. i am using image variable in this class for double buffering to reduce flickering and to save the applet's previous...
0
9620
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.