473,473 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

UserControl: How to use ClipRectangle in OnPaint handler

Hi everyone,

I'm developing a custom control that basically draws a circle. However I
notice that when I resize a form that contains the control, there is a lot of
flicker. I suppose that is because I'm not clipping using the ClipRectangle
property of the PaintEventArgs that comes with the OnPaint handler.

The OnPaint handler currently looks like this:

protected override void OnPaint( System.Windows.Forms.PaintEventArgs e )
{
e.Graphics.DrawEllipse (
Pens.Black,
ClientRectangle
);
e.Graphics.FillEllipse (
Brushes.Green,
ClientRectangle
);

StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;

Font textFont = new Font( "Times New Roman", 10F );

e.Graphics.DrawString (
"Head",
textFont,
Brushes.White,
ClientRectangle,
stringFormat
);

base.OnPaint( e );
}

I do not see how I could use the ClipRectangle to only draw the portion that
needs to be redrawn... Can someone help me out?

Thanks, and kind regards,
--
Tom Tempelaere.
Nov 17 '05 #1
7 10090
Tom,

Before you approach it from that angle, are you setting the
ControlStyles.OptimizedDoubleBuffer flag to true (through a call to the
SetControlStyles method on the Control)? This will cause your painting to
be done to a buffer first, which will then be BitBlt'ed to the device
context, and should reduce flicker.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
wrote in message news:0D**********************************@microsof t.com...
Hi everyone,

I'm developing a custom control that basically draws a circle. However I
notice that when I resize a form that contains the control, there is a lot
of
flicker. I suppose that is because I'm not clipping using the
ClipRectangle
property of the PaintEventArgs that comes with the OnPaint handler.

The OnPaint handler currently looks like this:

protected override void OnPaint( System.Windows.Forms.PaintEventArgs e )
{
e.Graphics.DrawEllipse (
Pens.Black,
ClientRectangle
);
e.Graphics.FillEllipse (
Brushes.Green,
ClientRectangle
);

StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;

Font textFont = new Font( "Times New Roman", 10F );

e.Graphics.DrawString (
"Head",
textFont,
Brushes.White,
ClientRectangle,
stringFormat
);

base.OnPaint( e );
}

I do not see how I could use the ClipRectangle to only draw the portion
that
needs to be redrawn... Can someone help me out?

Thanks, and kind regards,
--
Tom Tempelaere.

Nov 17 '05 #2
Hi Nicholas,

The control is UserControl derived and honestly I don't find a method named
SetControlStyles. Not even in the Control class. I searched the help files,
and didn't find a method named SetControlStyles.

Note that I'm still using .NET 1.1, perhaps it is a new addition?

TT.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Tom,

Before you approach it from that angle, are you setting the
ControlStyles.OptimizedDoubleBuffer flag to true (through a call to the
SetControlStyles method on the Control)? This will cause your painting to
be done to a buffer first, which will then be BitBlt'ed to the device
context, and should reduce flicker.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
wrote in message news:0D**********************************@microsof t.com...
Hi everyone,

I'm developing a custom control that basically draws a circle. However I
notice that when I resize a form that contains the control, there is a lot
of
flicker. I suppose that is because I'm not clipping using the
ClipRectangle
property of the PaintEventArgs that comes with the OnPaint handler.

The OnPaint handler currently looks like this:

protected override void OnPaint( System.Windows.Forms.PaintEventArgs e )
{
e.Graphics.DrawEllipse (
Pens.Black,
ClientRectangle
);
e.Graphics.FillEllipse (
Brushes.Green,
ClientRectangle
);

StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;

Font textFont = new Font( "Times New Roman", 10F );

e.Graphics.DrawString (
"Head",
textFont,
Brushes.White,
ClientRectangle,
stringFormat
);

base.OnPaint( e );
}

I do not see how I could use the ClipRectangle to only draw the portion
that
needs to be redrawn... Can someone help me out?

Thanks, and kind regards,
--
Tom Tempelaere.


Nov 17 '05 #3
Nicholas,

After some searching, I found the SetStyle method which does that. Thanks
for the hint.

What do you think is the best place to set this style? In the
InitializeComponent method, an OnLoad method, the constructor, or somewhere
else.

Thanks again,
Tom Tempelaere.

"TT (Tom Tempelaere)" wrote:
Hi Nicholas,

The control is UserControl derived and honestly I don't find a method named
SetControlStyles. Not even in the Control class. I searched the help files,
and didn't find a method named SetControlStyles.

Note that I'm still using .NET 1.1, perhaps it is a new addition?

TT.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Tom,

Before you approach it from that angle, are you setting the
ControlStyles.OptimizedDoubleBuffer flag to true (through a call to the
SetControlStyles method on the Control)? This will cause your painting to
be done to a buffer first, which will then be BitBlt'ed to the device
context, and should reduce flicker.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
wrote in message news:0D**********************************@microsof t.com...
Hi everyone,

I'm developing a custom control that basically draws a circle. However I
notice that when I resize a form that contains the control, there is a lot
of
flicker. I suppose that is because I'm not clipping using the
ClipRectangle
property of the PaintEventArgs that comes with the OnPaint handler.

The OnPaint handler currently looks like this:

protected override void OnPaint( System.Windows.Forms.PaintEventArgs e )
{
e.Graphics.DrawEllipse (
Pens.Black,
ClientRectangle
);
e.Graphics.FillEllipse (
Brushes.Green,
ClientRectangle
);

StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;

Font textFont = new Font( "Times New Roman", 10F );

e.Graphics.DrawString (
"Head",
textFont,
Brushes.White,
ClientRectangle,
stringFormat
);

base.OnPaint( e );
}

I do not see how I could use the ClipRectangle to only draw the portion
that
needs to be redrawn... Can someone help me out?

Thanks, and kind regards,
--
Tom Tempelaere.


Nov 17 '05 #4
Hey Nicholas,

I set the ControlStyles.DoubleBuffer style to true in the IntializeComponent
method using the Control.SetStyle method, but alas there is still a lot of
flicker.

I'm putting a matrix of these controls inside a Panel. Should I set the same
property to true for the Panel? (already trying ... ;-))

Thanks,
TT.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Tom,

Before you approach it from that angle, are you setting the
ControlStyles.OptimizedDoubleBuffer flag to true (through a call to the
SetControlStyles method on the Control)? This will cause your painting to
be done to a buffer first, which will then be BitBlt'ed to the device
context, and should reduce flicker.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
wrote in message news:0D**********************************@microsof t.com...
Hi everyone,

I'm developing a custom control that basically draws a circle. However I
notice that when I resize a form that contains the control, there is a lot
of
flicker. I suppose that is because I'm not clipping using the
ClipRectangle
property of the PaintEventArgs that comes with the OnPaint handler.

The OnPaint handler currently looks like this:

protected override void OnPaint( System.Windows.Forms.PaintEventArgs e )
{
e.Graphics.DrawEllipse (
Pens.Black,
ClientRectangle
);
e.Graphics.FillEllipse (
Brushes.Green,
ClientRectangle
);

StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;

Font textFont = new Font( "Times New Roman", 10F );

e.Graphics.DrawString (
"Head",
textFont,
Brushes.White,
ClientRectangle,
stringFormat
);

base.OnPaint( e );
}

I do not see how I could use the ClipRectangle to only draw the portion
that
needs to be redrawn... Can someone help me out?

Thanks, and kind regards,
--
Tom Tempelaere.


Nov 17 '05 #5
Nicholas,

Nope, the Panel class does not have a SetStyle method. Pitty. I'll have to
look for alternatives.

Kind regards,
TT.

"TT (Tom Tempelaere)" wrote:
Hey Nicholas,

I set the ControlStyles.DoubleBuffer style to true in the IntializeComponent
method using the Control.SetStyle method, but alas there is still a lot of
flicker.

I'm putting a matrix of these controls inside a Panel. Should I set the same
property to true for the Panel? (already trying ... ;-))

Thanks,
TT.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Tom,

Before you approach it from that angle, are you setting the
ControlStyles.OptimizedDoubleBuffer flag to true (through a call to the
SetControlStyles method on the Control)? This will cause your painting to
be done to a buffer first, which will then be BitBlt'ed to the device
context, and should reduce flicker.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
wrote in message news:0D**********************************@microsof t.com...
Hi everyone,

I'm developing a custom control that basically draws a circle. However I
notice that when I resize a form that contains the control, there is a lot
of
flicker. I suppose that is because I'm not clipping using the
ClipRectangle
property of the PaintEventArgs that comes with the OnPaint handler.

The OnPaint handler currently looks like this:

protected override void OnPaint( System.Windows.Forms.PaintEventArgs e )
{
e.Graphics.DrawEllipse (
Pens.Black,
ClientRectangle
);
e.Graphics.FillEllipse (
Brushes.Green,
ClientRectangle
);

StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;

Font textFont = new Font( "Times New Roman", 10F );

e.Graphics.DrawString (
"Head",
textFont,
Brushes.White,
ClientRectangle,
stringFormat
);

base.OnPaint( e );
}

I do not see how I could use the ClipRectangle to only draw the portion
that
needs to be redrawn... Can someone help me out?

Thanks, and kind regards,
--
Tom Tempelaere.


Nov 17 '05 #6
Tom,

Sorry for giving you the wrong name. Can you post your code?

I would set the style in the constructor, personally, as code in the
InitializeComponent section gets overwritten quite often.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
wrote in message news:8C**********************************@microsof t.com...
Hey Nicholas,

I set the ControlStyles.DoubleBuffer style to true in the
IntializeComponent
method using the Control.SetStyle method, but alas there is still a lot of
flicker.

I'm putting a matrix of these controls inside a Panel. Should I set the
same
property to true for the Panel? (already trying ... ;-))

Thanks,
TT.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Tom,

Before you approach it from that angle, are you setting the
ControlStyles.OptimizedDoubleBuffer flag to true (through a call to the
SetControlStyles method on the Control)? This will cause your painting
to
be done to a buffer first, which will then be BitBlt'ed to the device
context, and should reduce flicker.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
wrote in message
news:0D**********************************@microsof t.com...
> Hi everyone,
>
> I'm developing a custom control that basically draws a circle. However
> I
> notice that when I resize a form that contains the control, there is a
> lot
> of
> flicker. I suppose that is because I'm not clipping using the
> ClipRectangle
> property of the PaintEventArgs that comes with the OnPaint handler.
>
> The OnPaint handler currently looks like this:
>
> protected override void OnPaint( System.Windows.Forms.PaintEventArgs
> e )
> {
> e.Graphics.DrawEllipse (
> Pens.Black,
> ClientRectangle
> );
> e.Graphics.FillEllipse (
> Brushes.Green,
> ClientRectangle
> );
>
> StringFormat stringFormat = new StringFormat();
> stringFormat.Alignment = StringAlignment.Center;
> stringFormat.LineAlignment = StringAlignment.Center;
>
> Font textFont = new Font( "Times New Roman", 10F );
>
> e.Graphics.DrawString (
> "Head",
> textFont,
> Brushes.White,
> ClientRectangle,
> stringFormat
> );
>
> base.OnPaint( e );
> }
>
> I do not see how I could use the ClipRectangle to only draw the portion
> that
> needs to be redrawn... Can someone help me out?
>
> Thanks, and kind regards,
> --
> Tom Tempelaere.


Nov 17 '05 #7
Hey Nicholas,

I solved the issue entirely by doing this for my UserControl controls
(inside constructor):

this.SetStyle( ControlStyles.DoubleBuffer, true );
this.SetStyle( ControlStyles.UserPaint, true );
this.SetStyle( ControlStyles.AllPaintingInWmPaint, true );

Setting the DoubleBuffer property alone is not enough.

Thanks,
Tom T.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Tom,

Sorry for giving you the wrong name. Can you post your code?

I would set the style in the constructor, personally, as code in the
InitializeComponent section gets overwritten quite often.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
wrote in message news:8C**********************************@microsof t.com...
Hey Nicholas,

I set the ControlStyles.DoubleBuffer style to true in the
IntializeComponent
method using the Control.SetStyle method, but alas there is still a lot of
flicker.

I'm putting a matrix of these controls inside a Panel. Should I set the
same
property to true for the Panel? (already trying ... ;-))

Thanks,
TT.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Tom,

Before you approach it from that angle, are you setting the
ControlStyles.OptimizedDoubleBuffer flag to true (through a call to the
SetControlStyles method on the Control)? This will cause your painting
to
be done to a buffer first, which will then be BitBlt'ed to the device
context, and should reduce flicker.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
wrote in message
news:0D**********************************@microsof t.com...
> Hi everyone,
>
> I'm developing a custom control that basically draws a circle. However
> I
> notice that when I resize a form that contains the control, there is a
> lot
> of
> flicker. I suppose that is because I'm not clipping using the
> ClipRectangle
> property of the PaintEventArgs that comes with the OnPaint handler.
>
> The OnPaint handler currently looks like this:
>
> protected override void OnPaint( System.Windows.Forms.PaintEventArgs
> e )
> {
> e.Graphics.DrawEllipse (
> Pens.Black,
> ClientRectangle
> );
> e.Graphics.FillEllipse (
> Brushes.Green,
> ClientRectangle
> );
>
> StringFormat stringFormat = new StringFormat();
> stringFormat.Alignment = StringAlignment.Center;
> stringFormat.LineAlignment = StringAlignment.Center;
>
> Font textFont = new Font( "Times New Roman", 10F );
>
> e.Graphics.DrawString (
> "Head",
> textFont,
> Brushes.White,
> ClientRectangle,
> stringFormat
> );
>
> base.OnPaint( e );
> }
>
> I do not see how I could use the ClipRectangle to only draw the portion
> that
> needs to be redrawn... Can someone help me out?
>
> Thanks, and kind regards,
> --
> Tom Tempelaere.


Nov 17 '05 #8

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

Similar topics

2
by: Jose Michael Meo R. Barrido | last post by:
I made a custom runded rectangle usercontrol. I used a function i found on the internet. the function works fine(see "GetRoundRect" below). I use the fullowing code to make my usercontrol...
11
by: Sagaert Johan | last post by:
I have made a custom control that draws a rectangle when the mouse is down, and does nothing when the mouse is up. I set/reset a flag in MouseDown/Mouse up and use this to do the drawing in the...
2
by: Jaikumar | last post by:
Hi, 1) I have created one windows application, In the main form ( form1) i have added one usercontrol (usercontrol1), In that user control i am drawing one image. 2) In the UserControl1 i am...
5
by: Michael Chapman | last post by:
I have a UserControl-derived class that behaves somewhat strangely in the form designer. Some properties (BorderStyle and Font) cause the control to be updated in the designer, but others...
4
by: steve bull | last post by:
I have created a UserControl and added it to my toolbox. It appears in the toolbox with the icon I created for it. The problem is that when I drag the control onto a form no code is generated. If I...
41
by: JohnR | last post by:
In it's simplest form, assume that I have created a usercontrol, WSToolBarButton that contains a button. I would like to eventually create copies of WSToolBarButton dynamically at run time based...
7
by: Dino Buljubasic | last post by:
Hi, I am using C# 2.0 (VS2005) to build my user control that contains a number of dynamically loaded ListViews. ListViewItems have their ForeColor properties set to either black or blue to...
4
by: grayaii | last post by:
Hi, I have a simple form that handles all its paint functionality like so: this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true); And the entry point to this...
14
by: raylopez99 | last post by:
KeyDown won't work KeyPress fails KeyDown not seen inspired by a poster here:http://tinyurl.com/62d97l I found some interesting stuff, which I reproduce below for newbies like me. The main...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.