DrawRectangle positioning

J

Jon Slaughter

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
 
K

Kevin Spencer

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
 
J

Jon Slaughter

Kevin Spencer said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top