Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid Flickering in Windows Forms?

Double buffering not working with combo-box. is there any another methods to avoid flickering in windows forms?

i have one windows form with number of panels in it. I'm showing only one panel at a time based on my menu selection.

i have one icon panel,one header panel and the combo box. based on the selected item of that combo-box the gridview1 and 2 are filling. when I'm rapidly selecting the combo-box item using my keyboard down arrow the icon panel and the header panel are always repainting. i need to keep that both without any change. this two panels producing some flashing effect(ie,they are blinking or flashing) while I'm changing the combo box selected index. is there any way to avoid this flashing.? i tried double-buffered enabled in form constructor and form load event. Please Help..............

InitializeComponent();
                this.SetStyle(ControlStyles.DoubleBuffer, true);
                this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
                this.SetStyle(ControlStyles.UserPaint, true);
                this.SetStyle(ControlStyles.SupportsTransparentBackColor, false);
                this.SetStyle(ControlStyles.Opaque, false);
                this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
                this.SetStyle(ControlStyles.ResizeRedraw, true);

i tried this code in form constuctor and form load event

like image 756
ManjuVijayan Avatar asked Oct 10 '12 07:10

ManjuVijayan


People also ask

Which property of a control is used to reduce flickering when it is drawn?

Buffered graphics can reduce or eliminate flicker that is caused by progressive redrawing of parts of a displayed surface.

How do I turn on double buffering?

To enable double buffering, simply call the setDoubleBuffered() method (inherited from JComponent) to set the double-buffered property to true for any components that should use double-buffered drawing.


3 Answers

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams handleParam = base.CreateParams;
            handleParam.ExStyle |= 0x02000000;   // WS_EX_COMPOSITED       
            return handleParam;
        }
    }
like image 122
user3541403 Avatar answered Oct 17 '22 17:10

user3541403


Yet another solution:

//TODO: Don't forget to include using System.Runtime.InteropServices.

internal static class NativeWinAPI
{
    internal static readonly int GWL_EXSTYLE = -20;
    internal static readonly int WS_EX_COMPOSITED = 0x02000000;

    [DllImport("user32")]
    internal static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    [DllImport("user32")]
    internal static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
}

And your form constructor should look as follows:

public MyForm()
{
    InitializeComponent();

    int style = NativeWinAPI.GetWindowLong(this.Handle, NativeWinAPI.GWL_EXSTYLE);
    style |= NativeWinAPI.WS_EX_COMPOSITED;
    NativeWinAPI.SetWindowLong(this.Handle, NativeWinAPI.GWL_EXSTYLE, style);
}

In the code above, you might change this.Handle to something like MyFlickeringPanel.Handle

You can read a bit more about it here: Extended Window Styles and here: CreateWindowEx.

With WS_EX_COMPOSITED set, all descendants of a window get bottom-to-top painting order using double-buffering. Bottom-to-top painting order allows a descendent window to have translucency (alpha) and transparency (color-key) effects, but only if the descendent window also has the WS_EX_TRANSPARENT bit set. Double-buffering allows the window and its descendents to be painted without flicker.

like image 44
Nikolay Khil Avatar answered Oct 17 '22 16:10

Nikolay Khil


Solution #1:
Use ComboxBox.BeginUpdate() before you add items. This will prevent the Control from repainting the ComboBox each time an item is added to the list. After adding the items, you can use ComboBox.EndUpdate() to repaint.

Solution #2

private void EnableDoubleBuffering()
{
    this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
}
like image 21
varg Avatar answered Oct 17 '22 15:10

varg