How to double buffer .NET controls on a form?

后端 未结 12 1770
旧巷少年郎
旧巷少年郎 2020-11-22 16:21

How can I set the protected DoubleBuffered property of the controls on a form that are suffering from flicker?

相关标签:
12条回答
  • 2020-11-22 16:59

    You can also inherit the controls into your own classes, and set the property in there. This method is also nice if you tend to be doing a lot of set up that is the same on all of the controls.

    0 讨论(0)
  • 2020-11-22 17:00

    I have found that simply setting the DoubleBuffered setting on the form automatically sets all the properties listed here.

    0 讨论(0)
  • 2020-11-22 17:01

    vb.net version of this fine solution....:

    Protected Overrides ReadOnly Property CreateParams() As CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = cp.ExStyle Or &H2000000
            Return cp
        End Get
    End Property
    
    0 讨论(0)
  • 2020-11-22 17:04

    This caused me a lot of grief for two days with a third party control until I tracked it down.

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x02000000;
            return cp;
        }
    }
    

    I recently had a lot of holes (droppings) when re-sizing / redrawing a control containing several other controls.

    I tried WS_EX_COMPOSITED and WM_SETREDRAW but nothing worked until I used this:

    private void myPanel_SizeChanged(object sender, EventArgs e)
    {
         Application.DoEvents();
    }
    

    Just wanted to pass it on.

    0 讨论(0)
  • 2020-11-22 17:09
    System.Reflection.PropertyInfo aProp = typeof(System.Windows.Forms.Control)
        .GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic |
        System.Reflection.BindingFlags.Instance);
    aProp.SetValue(ListView1, true, null);
    

    Ian has some more information about using this on a terminal server.

    0 讨论(0)
  • 2020-11-22 17:09

    Extension method to turn double buffering on or off for controls

    public static class ControlExtentions
    {
        /// <summary>
        /// Turn on or off control double buffering (Dirty hack!)
        /// </summary>
        /// <param name="control">Control to operate</param>
        /// <param name="setting">true to turn on double buffering</param>
        public static void MakeDoubleBuffered(this Control control, bool setting)
        {
            Type controlType = control.GetType();
            PropertyInfo pi = controlType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
            pi.SetValue(control, setting, null);
        }
    }
    

    Usage (for example how to make DataGridView DoubleBuffered):

    DataGridView _grid = new DataGridView();
    //  ...
    _grid.MakeDoubleBuffered(true);
    
    0 讨论(0)
提交回复
热议问题