问题:
在做一个窗体,底部有一个Panel,Panel上面有一些button,在最小化后还原最大化的时候,会有处理不掉的一个黑块一闪而过,体验非常不好。使用双缓冲DoubleBuffer后问题一样存在。
解决:
搜索了很多方法,最后使用WinForm的计时器控制窗体的TransparencyKey消除了这个黑块。
1.在窗体最小化的时候设置TransparencyKey为黑色,这样再打开的时候,黑块就会被透明掉。然后就看不见黑块了。
private void btHide_Click(object sender, EventArgs e)
{
TransparencyKey = Color.Black;
this.WindowState = FormWindowState.Minimized;
}
2.然后在窗体最大化的时候启动计时器恢复TransparencyKey。(需要预先在构造函数中申明计时器)
private void ResetTransparencyKeyTimer_Tick(object sender, EventArgs e)
{
//Utility.SafeTask(() => {
TransparencyKey = Color.Empty;
ResetTransparencyKeyTimer.Stop();
//});
}
完整代码:
private Timer ResetTransparencyKeyTimer;
private const int WM_NCLBUTTONDOWN = 0x00A1;
public MyForm()
{
InitializeComponent();
ResetTransparencyKeyTimer = new System.Windows.Forms.Timer();
ResetTransparencyKeyTimer.Interval = 1;
ResetTransparencyKeyTimer.Enabled = false;
ResetTransparencyKeyTimer.Tick += ResetTransparencyKeyTimer_Tick;
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg)
{
case WM_NCLBUTTONDOWN:
if (this.WindowState == FormWindowState.Minimized)
{
highlighterTimer.Stop();
this.Resize -= SpiderTriggerViewForm_Resize;
this.LocationChanged += LocationChangedHander;
this.WindowState = FormWindowState.Normal;
this.Resize += SpiderTriggerViewForm_Resize;
}
break;
}
}
private void LocationChangedHander(object sender, EventArgs e)
{
ResetTransparencyKeyTimer.Start();
this.LocationChanged -= LocationChangedHander;
}
private void ResetTransparencyKeyTimer_Tick(object sender, EventArgs e)
{
//Utility.SafeTask(() => {
TransparencyKey = Color.Empty;
ResetTransparencyKeyTimer.Stop();
// });
}