Home All Groups Group Topic Archive Search About

UserControl: How to use ClipRectangle in OnPaint handler

Author
8 Apr 2005 4:15 PM
TT (Tom Tempelaere)
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.

Author
8 Apr 2005 4:25 PM
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.


--
               - Nicholas Paldino [.NET/C# MVP]
               - mvp@spam.guard.caspershouse.com

Show quoteHide quote
"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
wrote in message news:0D23DFF4-A377-4249-9524-A950E905DD5B@microsoft.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.
Are all your drivers up to date? click for free checkup

Author
8 Apr 2005 4:51 PM
TT (Tom Tempelaere)
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.

Show quoteHide quote
"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]
>                - mvp@spam.guard.caspershouse.com
>
> "TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
> wrote in message news:0D23DFF4-A377-4249-9524-A950E905DD5B@microsoft.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.
>
>
>
Author
8 Apr 2005 4:53 PM
TT (Tom Tempelaere)
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.

Show quoteHide quote
"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]
> >                - mvp@spam.guard.caspershouse.com
> >
> > "TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
> > wrote in message news:0D23DFF4-A377-4249-9524-A950E905DD5B@microsoft.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.
> >
> >
> >
Author
8 Apr 2005 4:57 PM
TT (Tom Tempelaere)
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.

Show quoteHide quote
"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]
>                - mvp@spam.guard.caspershouse.com
>
> "TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
> wrote in message news:0D23DFF4-A377-4249-9524-A950E905DD5B@microsoft.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.
>
>
>
Author
8 Apr 2005 4:59 PM
TT (Tom Tempelaere)
Nicholas,

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

Kind regards,
TT.

Show quoteHide quote
"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]
> >                - mvp@spam.guard.caspershouse.com
> >
> > "TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
> > wrote in message news:0D23DFF4-A377-4249-9524-A950E905DD5B@microsoft.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.
> >
> >
> >
Author
8 Apr 2005 5:07 PM
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]
               - mvp@spam.guard.caspershouse.com

Show quoteHide quote
"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
wrote in message news:8CDC484C-526D-4813-893D-CC82B18E13C0@microsoft.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]
>>                - mvp@spam.guard.caspershouse.com
>>
>> "TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
>> wrote in message
>> news:0D23DFF4-A377-4249-9524-A950E905DD5B@microsoft.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.
>>
>>
>>
Author
15 Apr 2005 4:20 PM
TT (Tom Tempelaere)
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.

Show quoteHide quote
"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]
>                - mvp@spam.guard.caspershouse.com
>
> "TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
> wrote in message news:8CDC484C-526D-4813-893D-CC82B18E13C0@microsoft.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]
> >>                - mvp@spam.guard.caspershouse.com
> >>
> >> "TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
> >> wrote in message
> >> news:0D23DFF4-A377-4249-9524-A950E905DD5B@microsoft.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.
> >>
> >>
> >>
>
>
>

Bookmark and Share