code_junkie

How to double buffer .NET controls on a form?

Nov 10th, 2011
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. public static void SetDoubleBuffered(System.Windows.Forms.Control c)
  2. {
  3. //Taxes: Remote Desktop Connection and painting
  4. //http://blogs.msdn.com/oldnewthing/archive/2006/01/03/508694.aspx
  5. if (System.Windows.Forms.SystemInformation.TerminalServerSession)
  6. return;
  7.  
  8. System.Reflection.PropertyInfo aProp =
  9. typeof(System.Windows.Forms.Control).GetProperty(
  10. "DoubleBuffered",
  11. System.Reflection.BindingFlags.NonPublic |
  12. System.Reflection.BindingFlags.Instance);
  13.  
  14. aProp.SetValue(c, true, null);
  15. }
  16.  
  17. System.Reflection.PropertyInfo aProp = typeof(System.Windows.Forms.Control)
  18. .GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic |
  19. System.Reflection.BindingFlags.Instance);
  20. aProp.SetValue(ListView1, true, null);
  21.  
  22. public void EnableDoubleBuffering()
  23. {
  24. this.SetStyle(ControlStyles.DoubleBuffer |
  25. ControlStyles.UserPaint |
  26. ControlStyles.AllPaintingInWmPaint,
  27. true);
  28. this.UpdateStyles();
  29. }
  30.  
  31. class Foo : Panel
  32. {
  33. public Foo() { DoubleBuffered = true; }
  34. }
  35.  
  36. protected override CreateParams CreateParams
  37. {
  38. get
  39. {
  40. CreateParams cp = base.CreateParams;
  41. cp.ExStyle |= 0x02000000;
  42. return cp;
  43. }
  44. }
  45.  
  46. protected override CreateParams CreateParams
  47. {
  48. get
  49. {
  50. CreateParams cp = base.CreateParams;
  51. cp.ExStyle |= 0x02000000;
  52. return cp;
  53. }
  54. }
Add Comment
Please, Sign In to add comment