473,693 Members | 2,832 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 10686
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
1854
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
8405
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
9059
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
4248
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
10320
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
2030
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
3760
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
2558
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
5128
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
8611
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...
0
8549
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9100
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8812
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6475
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
4332
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
4565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2989
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
2245
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.