How to double buffer .NET controls on a form?

后端 未结 12 1876
旧巷少年郎
旧巷少年郎 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 17:09

    Extension method to turn double buffering on or off for controls

    public static class ControlExtentions
    {
        /// 
        /// Turn on or off control double buffering (Dirty hack!)
        /// 
        /// Control to operate
        /// true to turn on double buffering
        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);
    

提交回复
热议问题