UserControl: How to use ClipRectangle in OnPaint handler

G

Guest

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,
 
N

Nicholas Paldino [.NET/C# MVP]

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

Guest

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 said:
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]
- (e-mail address removed)

TT (Tom Tempelaere) said:
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,
 
G

Guest

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) said:
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 said:
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]
- (e-mail address removed)

TT (Tom Tempelaere) said:
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,
 
G

Guest

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 said:
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]
- (e-mail address removed)

TT (Tom Tempelaere) said:
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,
 
G

Guest

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) said:
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 said:
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]
- (e-mail address removed)

TT (Tom Tempelaere) said:
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,
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

TT (Tom Tempelaere) said:
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 said:
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]
- (e-mail address removed)

"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
wrote in message
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,
 
G

Guest

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 said:
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]
- (e-mail address removed)

TT (Tom Tempelaere) said:
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 said:
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]
- (e-mail address removed)

"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
wrote in message
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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top