Author Topic: User Control Flickering  (Read 1445 times)

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
User Control Flickering
« on: September 30, 2018, 12:17:10 PM »
Archie, I wanted to create a custom control for a gauge.  I created a new user control and set the background image of it to the gauge i want then I draw the line of the gauge using the following code.  Everything works great except the control 'flickers' when it is updated too fast.  I set the Double Buffer to true and enabled AntiAliasing, however this does not seem to help. What can I do to prevent this flickering?  Thank you 

P.S. How do you post code in this forum?

 protected override void OnPaint(PaintEventArgs e)
{
    this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
    //call the base paint method
    base.OnPaint(e);
    // enable anti Aliasing
    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
   
       
    // draw the objects
    Pen pointer = new Pen(this.BackColor, needleWidth);// create an initial pointer for the gauge using the controls back color
    // draw the pointer on the gauge
    e.Graphics.TranslateTransform(Width * 0.5f, Height * 0.5f);  // sets the center of the control as the point to rotate the needle around
    e.Graphics.RotateTransform(180+n_value);  //the value to rotate the needle based on a public "Value" property.  Had to offset by 180 degrees
    e.Graphics.DrawLine(pointer, 0, 0, 0, Height * 0.5f);  // this sets the length of the needle to just under half the height
    pointer.Dispose();  // release the resources

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5323
    • View Profile
    • AdvancedHMI
Re: User Control Flickering
« Reply #1 on: September 30, 2018, 12:26:10 PM »
Flickering can be difficult to control I a WinForms application. The best results I have come up with is to use this in the constructor:
Code: [Select]
    Public Sub New()
        MyBase.New()

             ''* reduce the flicker
            Me.SetStyle(System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer Or
                        System.Windows.Forms.ControlStyles.AllPaintingInWmPaint Or
                        System.Windows.Forms.ControlStyles.UserPaint Or
                        System.Windows.Forms.ControlStyles.SupportsTransparentBackColor, True)

    End Sub



If you started with a UserControl, then a constructor may already exist in the designer.vb file

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Re: User Control Flickering
« Reply #2 on: September 30, 2018, 03:03:24 PM »
Nice job Archie, works great.  Thank you

How can I post code in this forum?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5323
    • View Profile
    • AdvancedHMI
Re: User Control Flickering
« Reply #3 on: September 30, 2018, 04:22:21 PM »
If you started with a UserControl, then you will need to attached the *.vb and the *.designer.vb files. If you start with inheriting Control, then you will only need to the *.vb file.

Phrog30

  • Guest
Re: User Control Flickering
« Reply #4 on: September 30, 2018, 08:02:00 PM »
Nice job Archie, works great.  Thank you

How can I post code in this forum?

Click the hashtag button and paste your code between the markers. It's no different than adding a link.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5323
    • View Profile
    • AdvancedHMI
Re: User Control Flickering
« Reply #5 on: September 30, 2018, 09:39:56 PM »
Ignore my last post. My mind was on a complete different train of thought. Phrog's post is the answer you were looking for.

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Re: User Control Flickering
« Reply #6 on: October 04, 2018, 09:16:35 AM »
LOL, Thanks Archie.  You had me baffled for a sec but I figured you were referring to something else.

Anyway, I took a stab at creating a custom control because nothing in the toolbox fit the bill.  This is a .dll of a simple synchroscope I made.  I didn't get too crazy as it does what I need.  Hope it helps someone else.  Thanks.