Re: What property defines a controls paint bounds?
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Wed, 27 Feb 2008 15:11:42 -0800
On Wed, 27 Feb 2008 13:20:02 -0800, Tom P. <padilla.henry@xxxxxxxxx> wrote:
I am creating a custom control and I'm trying to get the painting of
it correct. I'd like to simply use:
e.Graphics.FillRectangle(Brushes.White, DisplayRectangle);
... or ...
e.Graphics.FillRectangle(Brushes.White, ClientRectangle);
... or ...
e.Graphics.FillRectangle(Brushes.White, Bounds);
But each of those fails to draw a rectangle all the way around (most
are 1 pixel off). Instead I find myself having to use:
e.Graphics.FillRectangle(Brushes.White, DisplayRectangle.X,
DisplayRectangle.Y, DisplayRectangle.Width - 1,
DisplayRectangle.Height - 1);
What am I really supposed to use? How do I get that last pixel
correct?
Just what you're doing. Either DisplayRectangle or ClientRectangle are appropriate, depending on your specific need (by default, they are the same). And you need to subtract one from the width and height to get the whole rectangle to draw.
This may seem arbitrary, but there's actually a good reason for it. Graphical coordinates are treated as being the intersection of 0-width lines in a grid. The pixels are _between_ these lines. A line drawn from (0,0) to (0,100) for example will fill all of the pixels just to the right of that 0-width vertical line between those coordinates, inclusive.
When you try to draw a rectangle a specific width and height, all of the pixels are being drawn to the right and below of any coordinate that describes the rectangle. This means that on the right and bottom of the rectangle, if you've specified coordinates that are actually the absolute outer bounds of the area in which the control can draw, those pixels fall outside of the control and aren't drawn.
The fix is to reduce the width and height of the rectangle you're trying to draw by the width of one pixel, so that the pixels drawn for that rectangle fall within the control's drawable area.
Pete
.
- Follow-Ups:
- Re: What property defines a controls paint bounds?
- From: Tom P.
- Re: What property defines a controls paint bounds?
- References:
- What property defines a controls paint bounds?
- From: Tom P.
- What property defines a controls paint bounds?
- Prev by Date: Re: What property defines a controls paint bounds?
- Next by Date: Re: Detecting active forms
- Previous by thread: Re: What property defines a controls paint bounds?
- Next by thread: Re: What property defines a controls paint bounds?
- Index(es):
Relevant Pages
|