Avoid Flickering in Windows Forms?

前端 未结 3 1023
悲哀的现实
悲哀的现实 2020-12-15 00:10

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.

3条回答
  •  悲&欢浪女
    2020-12-15 00:27

    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.

提交回复
热议问题