Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.


Go Back   Vista Forums > Misc Newsgroups > .NET General

RB

Vista - Custom Control - OnPaint - not drawing

Reply
 
04-29-2009   #1
Ashutosh


 
 

Custom Control - OnPaint - not drawing

Hi,
I am trying to create a custom control in which I have to display my custom
data. The custom control is placed on a form and docked completely. When I
have the following code for the custom control, it doesn't re-paint all the
data when I re-size the form vertically at run time. If I switch to other
window and use ALT-TAB to switch back to my form, everything works fine.


The code for my custom control is

public partial class CustomControl1 : Control
{
Dictionary<int, string> data = new Dictionary<int, string>();
public CustomControl1()
{
InitializeComponent();
for (int i = 1; i <= 40; i++)
{
data[i * 20] = "This is a sample text for line number " +
i.ToString();
}
}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
foreach (int y in data.Keys)
{
if (e.ClipRectangle.Y <= y && (e.ClipRectangle.Y +
e.ClipRectangle.Height) >= y)
{
e.Graphics.DrawString(data[y], SystemFonts.DefaultFont,
SystemBrushes.ControlText, new PointF(20, y));
}
}
}
}

I am actually confused with the the usage of ClipRectangle. Please advice.

Thanks & Regards,
Ashu

My System SpecsSystem Spec
04-29-2009   #2
Linda Liu[MSFT]


 
 

RE: Custom Control - OnPaint - not drawing

Hi Ashu,

I notice you post the same issue in the
microsoft.public.dotnet.languages.csharp newsgroup, in which I have
replied. Please check my reply there. For your convenience, I include it
here:
=====================================
I performed a test on your sample code and did see the problem on my side.

If you print out the value of the PaintEventArgs.ClipRectangle property
from within the OnPaint method, you will see why the custom control doesn't
re-paint all the data when the form is resized vertically at tun time.

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// add this line of code to print the value of the
ClipRectangle property to the Output window
Console.WriteLine(e.ClipRectangle.ToString());
foreach (int y in data.Keys)
{
if (e.ClipRectangle.Y <= y && (e.ClipRectangle.Y +
e.ClipRectangle.Height) >= y)
{
e.Graphics.DrawString(data[y], SystemFonts.DefaultFont,
SystemBrushes.ControlText, new PointF(20, y));
}
}
}

Press F5 to run the application and resize the form vertically. The values
printed in the Output window are as follows:

{X=0,Y=0,Width=419,Height=413}
{X=0,Y=413,Width=419,Height=3}
{X=0,Y=416,Width=419,Height=3}
{X=0,Y=419,Width=419,Height=8}
{X=0,Y=427,Width=419,Height=3}
{X=0,Y=430,Width=419,Height=3}
{X=0,Y=433,Width=419,Height=1}
{X=0,Y=434,Width=419,Height=2}
{X=0,Y=436,Width=419,Height=4}
{X=0,Y=440,Width=419,Height=5}
{X=0,Y=445,Width=419,Height=3}
{X=0,Y=448,Width=419,Height=9}
{X=0,Y=457,Width=419,Height=2}
...

As you can see, the Paint event of the custom control is raised continously
when I resize the form. Each time when the Paint event is raised, the
PaintEventArgs.ClipRectangle is a small rectangle that doesn't meet the
condition you set in the OnPaint method. Thus, the new strings are not
drawn in the new rectangle on the custom control that needs painting.

Use the ClientRectangle property of the custom control instead of the
PaintEventArgs.ClipRectangle and this should solve your problem. For
example:

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
foreach (int y in data.Keys)
{
if (this.ClientRectangle.Y <= y && (this.ClientRectangle.Y
+ this.ClientRectangle.Height) >= y)
{
e.Graphics.DrawString(data[y], SystemFonts.DefaultFont,
SystemBrushes.ControlText, new PointF(20, y));
}
}
}

==========================================

If you have any question, please reply to that thread and I will follow up
with you in time.

Have a nice day!

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxx.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

My System SpecsSystem Spec
04-29-2009   #3
Ashutosh


 
 

RE: Custom Control - OnPaint - not drawing

Hi,
Please ignore the post! The issue is resolved.

I didn't consider the various cases of overlapping of clipping region and he
drawing text.

Thanks & Regards,
Ashu
My System SpecsSystem Spec
04-29-2009   #4
Ashutosh


 
 

RE: Custom Control - OnPaint - not drawing

Hi,
I did exactly the same just after posting the question...thanks anyways!

But, you mentioned using ClientRectangle! I would not recommend that. It's
totally un-necessary. Why do you want to paint an area which is already
painted?

Regards,
Ashutosh
My System SpecsSystem Spec
04-29-2009   #5
Linda Liu[MSFT]


 
 

RE: Custom Control - OnPaint - not drawing

Hi Ashu,

Thank you for your reply and I'm glad to hear that you have solved the
problem.
Quote:

> But, you mentioned using ClientRectangle! I would not recommend that.
It's totally un-necessary. Why do you want to paint an area which is
already painted?

Do you have any better option?

Sincerely,
Linda Liu
Microsoft Online Community Support

My System SpecsSystem Spec
Reply

RB


Thread Tools



Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd