PC Review


Reply
Thread Tools Rate Thread

Custom Control - OnPaint - not drawing

 
 
Ashutosh
Guest
Posts: n/a
 
      29th Apr 2009
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
 
Reply With Quote
 
 
 
 
Linda Liu[MSFT]
Guest
Posts: n/a
 
      29th Apr 2009
Hi Ashu,

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));
}
}
}

Hope this helps.
If you have anything unclear, please feel free to let me know.

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:
(E-Mail Removed).

==================================================
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.


 
Reply With Quote
 
Ashutosh
Guest
Posts: n/a
 
      29th Apr 2009
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
 
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
Custom Control - OnPaint - not drawing Ashutosh Microsoft Dot NET 4 30th Apr 2009 04:12 AM
Can I do this? VB.Net, XML, Custom Controls, Drawing, OnPaint JimBob Microsoft VB .NET 0 11th Aug 2006 03:57 PM
Problem with OnPaint in custom label control Kenneth Siewers Møller Microsoft C# .NET 1 7th Mar 2006 10:48 PM
Custom OnPaint and disabled control Microsoft C# .NET 4 26th Jul 2004 04:39 PM
GDI Onpaint Problem/Question in Custom control Sagaert Johan Microsoft C# .NET 11 17th Oct 2003 02:23 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:55 AM.