I guess not... but you may move the drawing position half the pen size to the bottom right
Scoregraphic
2009-05-29 11:21:42
I guess not... but you may move the drawing position half the pen size to the bottom right
This isn't a direct answer to the question, but you might want to consider using the ControlPaint.DrawBorder
method. You can specify the border style, colour, and various other properties. I also believe it handles adjusting the margins for you.
If you want the outer bounds of the rectangle to be constrained in all directions you will need to recalculate it in relation to the pen width:
private void DrawRectangle(Graphics g, Rectangle rect, float penWidth)
{
using (Pen pen = new Pen(SystemColors.ControlDark, penWidth))
{
float shrinkAmount = pen.Width / 2;
g.DrawRectangle(
pen,
rect.X + shrinkAmount, // move half a pen-width to the right
rect.Y + shrinkAmount, // move half a pen-width to the down
rect.Width - penWidth, // shrink width with one pen-width
rect.Height - penWidth); // shrink height with one pen-width
}
}
You can do this by specifying PenAlignment
Pen pen = new Pen(Color.Black, 2);
pen.Alignment = PenAlignment.Inset; //<-- this
g.DrawRectangle(pen, rect);