Skip to main content
 首页 » 编程设计

c#之在 c# winforms 中使用 paint 方法时如何防止闪烁

2024年02月06日11mayingbao

代码:

private void panel1_Paint(object sender, PaintEventArgs e) 
{ 
    e.Graphics.DrawRectangle(Pens.White, 
    e.ClipRectangle.Left, 
    e.ClipRectangle.Top, 
    e.ClipRectangle.Width - 1, 
    e.ClipRectangle.Height - 1); 
    base.OnPaint(e); 
} 

如何避免闪烁。以上是面板中Paint事件的panel1_Paint方法。谢谢。

请您参考如下方法:

某些 Windows 窗体控件默认情况下没有启用双缓冲。我不确定该面板是否是其中之一,但尝试启用它不会有什么坏处。

不幸的是Control.DoubleBuffered是 protected ,因此除非您从控件继承,否则您必须诉诸反射来启用它。

Control ctrl; 
ctrl.GetType() 
    .GetProperty("DoubleBuffered",  
                 BindingFlags.Instance | BindingFlags.NonPublic) 
    .SetValue(control, true, null);