PC Review


Reply
Thread Tools Rate Thread

ClientRectangle() Results Not Correct?

 
 
Joe Keller
Guest
Posts: n/a
 
      1st Dec 2003
Hello,

In writing custom components that, among other things, draw a border around
themselves (e.g. Panel component) I rely heavily on the "ClientRectangle()"
call to get the rectangle size of the component. However, I've found
without fail that if I rely solely on the measurements of what
ClientRectangle() returns to me, the right hand side and bottom of the
border will not be visible (it appears it is drawn past the bounds of the
component). Inevitably, I need to decrease the size of the width and the
height by 1 to get the border around the component (a rectangle) to draw
properly.

Is this expected behavior? Am I doing something incorrectly or is this
possibly a bug in how ClientRectangle() returns its results?

Sample code is listed below - just override the OnPaint() method of a Panel
component with the following code to see what I am talking about

protected override void OnPaint(PaintEventArgs e)
{
Graphics g;
Rectangle r;
Pen p;

g = e.Graphics;
r = this.ClientRectangle;
p = new Pen(Color.Black);

// If you comment out the following two lines the component will not draw
the border properly.
r.Width -= 1;
r.Height -= 1;

g.DrawRectangle(p,r);
g.Dispose();

base.OnPaint(e);
}


Thanks!

Joe


 
Reply With Quote
 
 
 
 
Alex Yakhnin, eMVP
Guest
Posts: n/a
 
      2nd Dec 2003
Joe,

That's the way it's supposed to be. The ClientRectange's Width/Height is
equal to the actual width/height of the control. You have to take an account
for the width of the line itself (1 pixel). It means that the right /bottom
lines of the rectangle will be drawn past the boundaries of a control.

-
Alex Yakhnin, .NET CF MVP
http://www.intelliprog.com

"Joe Keller" <(E-Mail Removed)> wrote in message
news:MPKdnUO9GLZOWFaiRVn-(E-Mail Removed)...
> Hello,
>
> In writing custom components that, among other things, draw a border

around
> themselves (e.g. Panel component) I rely heavily on the

"ClientRectangle()"
> call to get the rectangle size of the component. However, I've found
> without fail that if I rely solely on the measurements of what
> ClientRectangle() returns to me, the right hand side and bottom of the
> border will not be visible (it appears it is drawn past the bounds of the
> component). Inevitably, I need to decrease the size of the width and the
> height by 1 to get the border around the component (a rectangle) to draw
> properly.
>
> Is this expected behavior? Am I doing something incorrectly or is this
> possibly a bug in how ClientRectangle() returns its results?
>
> Sample code is listed below - just override the OnPaint() method of a

Panel
> component with the following code to see what I am talking about
>
> protected override void OnPaint(PaintEventArgs e)
> {
> Graphics g;
> Rectangle r;
> Pen p;
>
> g = e.Graphics;
> r = this.ClientRectangle;
> p = new Pen(Color.Black);
>
> // If you comment out the following two lines the component will not draw
> the border properly.
> r.Width -= 1;
> r.Height -= 1;
>
> g.DrawRectangle(p,r);
> g.Dispose();
>
> base.OnPaint(e);
> }
>
>
> Thanks!
>
> Joe
>
>



 
Reply With Quote
 
Joe Keller
Guest
Posts: n/a
 
      2nd Dec 2003
Ahhh - makes sense - thanks!

Joe

"Alex Yakhnin, eMVP" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Joe,
>
> That's the way it's supposed to be. The ClientRectange's Width/Height is
> equal to the actual width/height of the control. You have to take an

account
> for the width of the line itself (1 pixel). It means that the right

/bottom
> lines of the rectangle will be drawn past the boundaries of a control.
>
> -
> Alex Yakhnin, .NET CF MVP
> http://www.intelliprog.com
>
> "Joe Keller" <(E-Mail Removed)> wrote in message
> news:MPKdnUO9GLZOWFaiRVn-(E-Mail Removed)...
> > Hello,
> >
> > In writing custom components that, among other things, draw a border

> around
> > themselves (e.g. Panel component) I rely heavily on the

> "ClientRectangle()"
> > call to get the rectangle size of the component. However, I've found
> > without fail that if I rely solely on the measurements of what
> > ClientRectangle() returns to me, the right hand side and bottom of the
> > border will not be visible (it appears it is drawn past the bounds of

the
> > component). Inevitably, I need to decrease the size of the width and

the
> > height by 1 to get the border around the component (a rectangle) to draw
> > properly.
> >
> > Is this expected behavior? Am I doing something incorrectly or is this
> > possibly a bug in how ClientRectangle() returns its results?
> >
> > Sample code is listed below - just override the OnPaint() method of a

> Panel
> > component with the following code to see what I am talking about
> >
> > protected override void OnPaint(PaintEventArgs e)
> > {
> > Graphics g;
> > Rectangle r;
> > Pen p;
> >
> > g = e.Graphics;
> > r = this.ClientRectangle;
> > p = new Pen(Color.Black);
> >
> > // If you comment out the following two lines the component will not

draw
> > the border properly.
> > r.Width -= 1;
> > r.Height -= 1;
> >
> > g.DrawRectangle(p,r);
> > g.Dispose();
> >
> > base.OnPaint(e);
> > }
> >
> >
> > Thanks!
> >
> > Joe
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code not giving the correct results Bob07790 Microsoft Access Form Coding 5 7th Mar 2010 06:42 PM
Getting correct results for lookup? tgcali Microsoft Excel Misc 4 15th Jul 2008 02:32 AM
what formulas will give correct results =?Utf-8?B?VFVOR0FOQSBLVVJNQSBSQUpV?= Microsoft Excel Misc 0 26th Feb 2007 02:13 AM
getting correct SUM results in a form =?Utf-8?B?QmFydA==?= Microsoft Access Reports 1 12th Jan 2006 11:18 AM
In correct Query Results =?Utf-8?B?Ym1vcmdhbmg=?= Microsoft Access 1 26th May 2005 07:04 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:52 PM.