如果在控件上进行动态绘制时不让其闪烁一般情况下,在控件上进行动态绘制时,会出现闪烁效果,为了解决这一问题,可以用SetStyle方法来减少控件的闪烁。下面在窗体中将panel1控件设置为不闪烁的效果。代码如下: 242342 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace ppp { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public class pp : Panel public pp() UpdateStyles(); } } 说明:ControlStyles.OptimizedDoubleBuffer值是先在缓冲区中绘制,而不是直接绘制在屏幕上。ControlStyles.AllPaintingInWmPaint值是用来减少闪烁。 在窗体的加载事件中,窗体中的panel1控件进行实例化,以实现不让其闪烁。代码如下: private void Form1_Load(object sender, EventArgs e) { panel1 = new pp(); } } } 在制作第三方控件时,也可以直接去除闪烁效果,直接将大括号中的代码添加到第三方控件的重载方法中(以自定义控件Panel为例)。代码如下所示: public partial class UserPanel : Panel { public UserPanel() { InitializeComponent(); SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true); UpdateStyles(); } } |