471,610 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,610 software developers and data experts.

DrawRectangle positioning

How does DrawRectangle position a rectangle w.r.t to the Rectangle passed?
Lets suppose I use a pen of width 5. Are the lines centered about the passed
Rectangle or is it enclosed or offset? Are there ways to change the default
behavior?

The reason I ask is cause when I do

g.DrawRectangle(P, this.ClientRectangle) for drawing a rectangle as the
border for a control(button) and the width of P is larger than 1(or even
when its not it still looks wrong) the lines thickness is not what its
suppose to be,

Basically my code is

g.FillRegion(B, this.Region);

g.DrawRectangle(S, r1);

g.DrawRectangle(H, r2);

which draws two rectangles as the "border" for a button control. S and H are
pens for shadow and highlight.

my code for the rectangles is something like

Rectangle r1 = new Rectangle(2, 2, this.Width-x-2, this.Height-x-2);

Rectangle r2 = new Rectangle(1 + x+1+1, 1 + x+1+1, this.Width - x - 1 -
x-y-2-2, this.Height - x - 1 - x-y-2-2);

were x is the pens with. the values are not correct now as I was playing
around with them to see try and get it right.

Basicaly my first border would be (0,0, this.Width, this.Height) but because
of the way its draw I have to offset it some. the second border would then
be draw on the inside of this rectangle.

What I essentially want is to draw a rectangle that is interior to the pass
rectangle parameter and not where the lines of the rectangle are centered on
it. For larger pens it will should still be on the interior.

Its not to hard to convert from one method to the other if I knew which one
it used. Although there is the slight problem with rounding for pens of odd
width.

Thanks,

Jon
Oct 26 '06 #1
2 3386
Hi Jon,

I'm not surprised that you've had difficulty with this issue, as it is not
well-documented in the .Net SDK. The crux of the matter is this: When you
draw a rectangle with a pen, the width of the pen must be taken into account
with regards to the size of the target rectangle. There are 3 basic logical
possibilities:

1. The entire line is drawn *inside* the rectangle.
2. The width of the line is split, so that the edge of the rectangle is
centered in the line.
3. The entire line is drawn *outside* the rectangle.

It turns out that number *2* is the method employed with GDI+.

The following is an excellent and detailed article on the subject:

http://www.bobpowell.net/beginnersgdi.htm

--
HTH,

Kevin Spencer
Microsoft MVP
Short Order Coder
http://unclechutney.blogspot.com

The devil is in the yada yada yada
"Jon Slaughter" <Jo***********@Hotmail.comwrote in message
news:gQ*****************@newssvr14.news.prodigy.co m...
How does DrawRectangle position a rectangle w.r.t to the Rectangle passed?
Lets suppose I use a pen of width 5. Are the lines centered about the
passed Rectangle or is it enclosed or offset? Are there ways to change the
default behavior?

The reason I ask is cause when I do

g.DrawRectangle(P, this.ClientRectangle) for drawing a rectangle as the
border for a control(button) and the width of P is larger than 1(or even
when its not it still looks wrong) the lines thickness is not what its
suppose to be,

Basically my code is

g.FillRegion(B, this.Region);

g.DrawRectangle(S, r1);

g.DrawRectangle(H, r2);

which draws two rectangles as the "border" for a button control. S and H
are pens for shadow and highlight.

my code for the rectangles is something like

Rectangle r1 = new Rectangle(2, 2, this.Width-x-2, this.Height-x-2);

Rectangle r2 = new Rectangle(1 + x+1+1, 1 + x+1+1, this.Width - x - 1 -
x-y-2-2, this.Height - x - 1 - x-y-2-2);

were x is the pens with. the values are not correct now as I was playing
around with them to see try and get it right.

Basicaly my first border would be (0,0, this.Width, this.Height) but
because of the way its draw I have to offset it some. the second border
would then be draw on the inside of this rectangle.

What I essentially want is to draw a rectangle that is interior to the
pass rectangle parameter and not where the lines of the rectangle are
centered on it. For larger pens it will should still be on the interior.

Its not to hard to convert from one method to the other if I knew which
one it used. Although there is the slight problem with rounding for pens
of odd width.

Thanks,

Jon


Oct 26 '06 #2

"Kevin Spencer" <sp**@uce.govwrote in message
news:OV****************@TK2MSFTNGP02.phx.gbl...
Hi Jon,

I'm not surprised that you've had difficulty with this issue, as it is not
well-documented in the .Net SDK. The crux of the matter is this: When you
draw a rectangle with a pen, the width of the pen must be taken into
account with regards to the size of the target rectangle. There are 3
basic logical possibilities:

1. The entire line is drawn *inside* the rectangle.
2. The width of the line is split, so that the edge of the rectangle is
centered in the line.
3. The entire line is drawn *outside* the rectangle.

It turns out that number *2* is the method employed with GDI+.

The following is an excellent and detailed article on the subject:

http://www.bobpowell.net/beginnersgdi.htm
Thanks. I figured it was 2 because the other 2 I tried didn't work. At first
I thought it was a mixture of 1 and 2(the left and top being on the inside
and the bottom and right being on the outside) but none of my calculations
worked and I couldn't find any info regarding it.

Thanks again,
Jon
Oct 26 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Bryan R. Meyer | last post: by
4 posts views Thread by Jane Withnolastname | last post: by
1 post views Thread by Siri Krishna | last post: by
5 posts views Thread by Cristian | last post: by
6 posts views Thread by Mark | last post: by
1 post views Thread by t f | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by
1 post views Thread by ZEDKYRIE | last post: by

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.