1

I am just beginning to learn C# and I would like to seek advice on good practices when it comes to painting to avoid flickering.

I have used methods as mentioned on here, such as double buffering and turning on WS_EX_COMPOSITED, but the method that works for me is:

Calling the PaintEventHandler :

this.dgData.Paint += new System.Windows.Forms.PaintEventHandler(this.myPaint);

and using the PaintEventArgs to draw:

private void myPaint(object sender, System.Windows.Forms.PaintEventArgs e)
{
    Graphics myGraphics = e.Graphics;
    // Use myGraphics to draw
}

And that the PaintEventHandler is triggered via a Refresh() on a Timer.

private void TimerElapsed(object sender, System.EventArgs eventArgs)
{
    Refresh();
}

However, my concern is that this way of drawing would require me to have a PaintEventHandler for every control that I have on my form.

As such, I would like to ask if there is a more elegant way of accomplishing this.

Thanks!

John Tan
  • 1,331
  • 1
  • 19
  • 35
  • If you're manually drawing your controls, you will need a paint event handler for each one, yes.. What kind of application is this? What kind of flicker are you experiencing? – Blorgbeard Nov 27 '14 at 02:57
  • 2
    your code `.Paint += ...` is actually subscribing to a Control's paint event, the handler is executed when a control is painted; If you are to draw a custom control you should _override_ its `OnPaint` method. They are different. – kennyzx Nov 27 '14 at 03:00
  • @Blorgbeard Basically the application captures a live data set (about a few hundred points per second) and displays it as a graph. My flicker problem includes flickering of the list box displaying the additional information. – John Tan Nov 27 '14 at 04:05
  • @kennyzx What I am trying to do: I have a DataGridView control. I am trying to paint onto this control (a graph with dots and lines). Is this considered a custom control? – John Tan Nov 27 '14 at 04:06
  • Double-buffering is the solution. But how to make sure _all_ rendering is being done double-buffered depends on your actual code, which you haven't provided here. Please provide a good code example: _complete_ and _minimal_. See http://stackoverflow.com/help/mcve – Peter Duniho Nov 27 '14 at 04:13
  • I think you should __separate__ your problems instead of trying to find a silver bullet that solves them all. From what you write I take that you have a flicker problem with __two__ of your controls: a `ListBox` and a `DGV`. I would use a `doubleBuffered Panel` class for the graph and place it onto the DGV and make sure I use `Suspend-` and `ResumeLayout` with `AddRange` in the `ListBox`.. I think I would also at least test the `Chart` control. – TaW Nov 27 '14 at 08:28
  • `Drawing Framework in C# to avoid flickering` - You're looking for WPF. winforms doesn't support anything and is deprecated. Use current technology instead. – Federico Berasategui Nov 28 '14 at 01:24

1 Answers1

0

I cannot tell any more where I found this code but this is what I do to remove flickering: I set these styles in my control's constructor:

public partial class MyControl : Control
{    
    public MyControl()
    {
        InitializeComponent()
        SetStyle(ControlStyles.UserPaint, true);
        SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        SetStyle(ControlStyles.ResizeRedraw, true);
        SetStyle(ControlStyles.UserMouse, true);
    }
}
t3chb0t
  • 16,340
  • 13
  • 78
  • 118