底层重绘每次会清除画布,然后再全部重新绘制,这是导致闪烁最主要的原因。于是重载消息发送函数操作,禁掉这条消息。
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0014) // 禁掉清除背景消息
return;
base.WndProc(ref m);
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000;
return cp;
}
}
public static void SetDoubleBuffered(object obj)
{
Type type = obj.GetType();
PropertyInfo pi = type.GetProperty("DoubleBuffered",
BindingFlags.Instance | BindingFlags.NonPublic);
pi.SetValue(obj, true, null);
}
C#Label实时更新防闪烁