我在尝试将表单最小化到通知区域时遇到问题。
以下代码继续运行,但我的表单从任务栏中消失并且不会显示。
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
bool cursorNotInBar = Screen.GetWorkingArea(this).Contains(Cursor.Position);
if(this.WindowState == FormWindowState.Minimized && cursorNotInBar)
{
this.ShowInTaskbar = false;
notifyIcon1.Visible = true;
this.Hide();
}
我该如何解决这个问题?
最佳答案
这段代码中有几个非常令人不愉快的交互。从 ShowInTaskbar 属性开始,它有许多副作用,因为它强制重新创建 native 窗口。只是不要修改它,没有必要,因为当您隐藏窗口时任务栏按钮已经隐藏了。 NotifyIcon.MouseDoubleClick 事件也很古怪,您必须正确恢复窗口以防止其以 0x0 大小保持隐藏状态。
这样做:
protected override void OnResize(EventArgs e) {
base.OnResize(e);
if (this.WindowState == FormWindowState.Minimized && !notifyIcon1.Visible) {
notifyIcon1.Visible = true;
this.Hide();
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) {
this.Show();
this.WindowState = FormWindowState.Normal;
notifyIcon1.Visible = false;
this.BringToFront();
}
Show() 方法的放置至关重要。如果您在 WindowState 分配之后移动它,则窗口将无法正确恢复。
关于c# - 最小化表单到通知区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34270821/