How to double buffer .NET controls on a form?

后端 未结 12 1892
旧巷少年郎
旧巷少年郎 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:58

    Here's a more generic version of Dummy's solution.

    We can use reflection to get at the protected DoubleBuffered property, and then it can be set to true.

    Note: You should pay your developer taxes and not use double-buffering if the user is running in a terminal services session (e.g. Remote Desktop) This helper method will not turn on double buffering if the person is running in remote desktop.

    public static void SetDoubleBuffered(System.Windows.Forms.Control c)
    {
       //Taxes: Remote Desktop Connection and painting
       //http://blogs.msdn.com/oldnewthing/archive/2006/01/03/508694.aspx
       if (System.Windows.Forms.SystemInformation.TerminalServerSession)
          return;
    
       System.Reflection.PropertyInfo aProp = 
             typeof(System.Windows.Forms.Control).GetProperty(
                   "DoubleBuffered", 
                   System.Reflection.BindingFlags.NonPublic | 
                   System.Reflection.BindingFlags.Instance);
    
       aProp.SetValue(c, true, null); 
    }
    

提交回复
热议问题