473,734 Members | 2,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What property defines a controls paint bounds?

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.Fill Rectangle(Brush es.White, DisplayRectangl e);
.... or ...
e.Graphics.Fill Rectangle(Brush es.White, ClientRectangle );
.... or ...
e.Graphics.Fill Rectangle(Brush es.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.Fill Rectangle(Brush es.White, DisplayRectangl e.X,
DisplayRectangl e.Y, DisplayRectangl e.Width - 1,
DisplayRectangl e.Height - 1);

What am I really supposed to use? How do I get that last pixel
correct?

Tom P.
Feb 27 '08 #1
9 4268
On Feb 27, 2:20*pm, "Tom P." <padilla.he...@ gmail.comwrote:
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.Fill Rectangle(Brush es.White, DisplayRectangl e);
... or ...
e.Graphics.Fill Rectangle(Brush es.White, ClientRectangle );
... or ...
e.Graphics.Fill Rectangle(Brush es.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.Fill Rectangle(Brush es.White, DisplayRectangl e.X,
DisplayRectangl e.Y, DisplayRectangl e.Width - 1,
DisplayRectangl e.Height - 1);

What am I really supposed to use? How do I get that last pixel
correct?

Tom P.
What is wrong with e.Graphics.Fill Rectangle( Brushes.White,
e.ClipRectangle );

?
Matt
Feb 27 '08 #2
On Wed, 27 Feb 2008 13:20:02 -0800, Tom P. <pa***********@ gmail.comwrote:
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.Fill Rectangle(Brush es.White, DisplayRectangl e);
... or ...
e.Graphics.Fill Rectangle(Brush es.White, ClientRectangle );
... or ...
e.Graphics.Fill Rectangle(Brush es.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.Fill Rectangle(Brush es.White, DisplayRectangl e.X,
DisplayRectangl e.Y, DisplayRectangl e.Width - 1,
DisplayRectangl e.Height - 1);

What am I really supposed to use? How do I get that last pixel
correct?
Just what you're doing. Either DisplayRectangl e 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
Feb 27 '08 #3


"Tom P." 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.Fill Rectangle(Brush es.White, DisplayRectangl e);
.... or ...
e.Graphics.Fill Rectangle(Brush es.White, ClientRectangle );
.... or ...
e.Graphics.Fill Rectangle(Brush es.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.Fill Rectangle(Brush es.White, DisplayRectangl e.X,
DisplayRectangl e.Y, DisplayRectangl e.Width - 1,
DisplayRectangl e.Height - 1);

What am I really supposed to use? How do I get that last pixel
correct?

Tom P.
It looks like you really could do it with e.Graphics.Clea r(Color.White);

Feb 28 '08 #4
On Feb 27, 6:58 pm, Family Tree Mike
<FamilyTreeM... @discussions.mi crosoft.comwrot e:
"Tom P." 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.Fill Rectangle(Brush es.White, DisplayRectangl e);
.... or ...
e.Graphics.Fill Rectangle(Brush es.White, ClientRectangle );
.... or ...
e.Graphics.Fill Rectangle(Brush es.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.Fill Rectangle(Brush es.White, DisplayRectangl e.X,
DisplayRectangl e.Y, DisplayRectangl e.Width - 1,
DisplayRectangl e.Height - 1);
What am I really supposed to use? How do I get that last pixel
correct?
Tom P.

It looks like you really could do it with e.Graphics.Clea r(Color.White);
That's fine for clearing the control to white but what would I use to
draw the black outline?

Tom P.
Feb 28 '08 #5
On Feb 27, 5:10 pm, Matt <matttel...@spr ynet.comwrote:
On Feb 27, 2:20 pm, "Tom P." <padilla.he...@ gmail.comwrote:
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.Fill Rectangle(Brush es.White, DisplayRectangl e);
... or ...
e.Graphics.Fill Rectangle(Brush es.White, ClientRectangle );
... or ...
e.Graphics.Fill Rectangle(Brush es.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.Fill Rectangle(Brush es.White, DisplayRectangl e.X,
DisplayRectangl e.Y, DisplayRectangl e.Width - 1,
DisplayRectangl e.Height - 1);
What am I really supposed to use? How do I get that last pixel
correct?
Tom P.

What is wrong with e.Graphics.Fill Rectangle( Brushes.White,
e.ClipRectangle );

?
Matt
The same thing that's wrong with everything else - it's off by one
pixel.

Tom P.
Feb 28 '08 #6
On Feb 27, 5:11 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
wrote:
On Wed, 27 Feb 2008 13:20:02 -0800, Tom P. <padilla.he...@ gmail.comwrote:
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.Fill Rectangle(Brush es.White, DisplayRectangl e);
... or ...
e.Graphics.Fill Rectangle(Brush es.White, ClientRectangle );
... or ...
e.Graphics.Fill Rectangle(Brush es.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.Fill Rectangle(Brush es.White, DisplayRectangl e.X,
DisplayRectangl e.Y, DisplayRectangl e.Width - 1,
DisplayRectangl e.Height - 1);
What am I really supposed to use? How do I get that last pixel
correct?

Just what you're doing. Either DisplayRectangl e 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
I guess you're right. There's nothing else I can find to change this.
There should be a "DrawRectan gle" property for this type of
situation. Oh, well... thanks.

Tom P.
Feb 28 '08 #7
On Thu, 28 Feb 2008 10:59:36 -0800, Tom P. <pa***********@ gmail.comwrote:
I guess you're right. There's nothing else I can find to change this.
There should be a "DrawRectan gle" property for this type of
situation. Oh, well... thanks.
I don't understand what you mean. There is a DrawRectangle() method, but
it just outlines the rectangle rather than filling it.

There's not an actual problem here with .NET. The only issue is that you
need to comprehend the API differently. Once you understand that
coordinates you provide don't refer to pixels, but rather to the grid
between pixels, everything makes sense.

If you don't understand that, then yes...you'll continue to believe a
problem exists, even though none does.

Pete
Feb 28 '08 #8
On Feb 28, 1:13 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
wrote:
On Thu, 28 Feb 2008 10:59:36 -0800, Tom P. <padilla.he...@ gmail.comwrote:
I guess you're right. There's nothing else I can find to change this.
There should be a "DrawRectan gle" property for this type of
situation. Oh, well... thanks.

I don't understand what you mean. There is a DrawRectangle() method, but
it just outlines the rectangle rather than filling it.

There's not an actual problem here with .NET. The only issue is that you
need to comprehend the API differently. Once you understand that
coordinates you provide don't refer to pixels, but rather to the grid
between pixels, everything makes sense.

If you don't understand that, then yes...you'll continue to believe a
problem exists, even though none does.

Pete
I know there is a method DrawRectangle() , but I was talking about a
property that describe the ACTUAL painting region. In the same units,
or relationship, as the method is going to use it. What use have I for
a description of an area I have to adjust before using? Why provide me
with measurements that are close to what I need, but don't finally
describe the area I want? There are no fewer than four rectangles that
describe areas one pixel larger than the Displayed area of a control.
You mean to tell me they couldn't find it in their hearts to provide
one single property that depicts the ACTUAL area that methods like
DrawRectangle() are expecting? It doesn't seem like that big a deal.

Tom P.
Feb 28 '08 #9
On Thu, 28 Feb 2008 13:07:46 -0800, Tom P. <pa***********@ gmail.comwrote:
[...]
Understanding all that, what is wrong with saying the PaintingArea is
from (x, y) 0, 0 to (width, height) 99, 99?
Because that would describe an area only 99 pixels wide and 99 pixels high
(9801 pixels total), when in fact the "painting area" (that is, the area
that the control is responsible for painting) is 100 pixels wide by 100
pixels high (10000 pixels total).
>
But I think your last statement gives me some insight into my issue
(and a better way to paint the object, so thank you) The particular
rectangles provided are only 1 pixel too small because the pen I am
using is 1 pixel. They are, in point of fact, "Pen.Width" smaller. I
should not be subtracting 1, I should be subtracting the Pen.Width to
ensure the entire pen falls within the paintable area regardless of
what size the pen is.
Yes, that's exactly right.
To that end I thank you for helping me
understand exactly what is being presented and how to use it.
You're welcome.

Pete
Feb 28 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1620
by: jinu | last post by:
In a vb.net application, we are using the DataGridColoredTextBoxColumn class which is derived from DataGridTextBoxColumn class inorder to give additional colouring features. For aligning certain cell contents to right (since the values are numbers), we are using the following code. (alignToRight = True) Protected Overloads Overrides Sub Paint(ByVal grp As Graphics, ByVal bounds As Rectangle, ByVal source As CurrencyManager, ByVal rowNum...
7
10197
by: anders | last post by:
Hi! I have a panel formed as an A4-page, in this panel I have som labels, pictureboxes and a datagrid. How can I print the complete panel vith all it's controls? Regards Anders Aleborg
3
1355
by: Blair Bonnett | last post by:
Hi all, Run into a problem I can't find a solution for. I'm writing a C# app which has a custom background for the form (a gradient). I've successfully overridden the forms OnPaintBackground(PaintEventArgs pevent) method to paint the background. However, it seems that some of the controls on the form (such as the labels) also call this method and hence get painted with the gradient. Is there some way I can tell what control (or type...
4
13181
by: Dan Baker | last post by:
I have a form with several controls. I would like to paint *over* several controls (mainly GroupBoxes and Labels). But, Everywhere I try to perform painting, I end up "behind" the controls. I need a device context for the entire form, that I can render to *after* all the controls have been painted. Suggestions? Thanks DanB
3
1981
by: Mike Cooper | last post by:
I have been staring at the above error for over a week now! I have a an inherited data class looking like thus: Public Class DataGridBoolColumnInherit Inherits System.Windows.Forms.DataGridBoolColumn Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal As CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)
3
1256
by: VJ | last post by:
How do I use shade effect as colors. For example when I change the backcolor of my tab page, how do I set it to a faded 2 color combination? VJ
3
2669
by: fripper | last post by:
I think I am using the proper terminology here ... I am using VB 2003 and have a DataGrid to which I then attach a TableStyle ... then I set the properties of the TableStyle (BackColor, HeaderBackColor, etc.) ... then I create several DataGridTextBoxColumns and set their properties (MappingName, Width, Alignment, etc.) ... then I add these columns to the DataGridStyle. Next, using the MappingNames I add the columns to a DataTable ... then I...
1
2098
by: fiaolle | last post by:
Hi The first set of source code is the class for a combobox in a grid, hopefully. In the second set of code we try to use the combobox class, but the grid is empty. I don't understand how this works. The first set of code I downloaded from internet and when i tried it, it worked fine. But when I changed the second part of the code it started to give me trouble. Before I changed the code they used Datacolumns,Datarows and Datatables and...
0
1512
by: Hans Koller | last post by:
Hello group, I design a class to bind it to a property grid for easy modification of some settings. My problem is now that I want to raise an event when a settings has been changed. Thats not a problem with some "normal" properties (see property DisplayGrid). But when I use a subclass and TypeConverter the setter of the property is not called. Set(ByVal value As LineSetting) m_Limit1 = value
0
9449
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
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
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
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
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
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
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
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.