应用程序不会最小化通知栏
本文关键字:通知 最小化 应用程序 | 更新日期: 2023-09-27 18:10:21
我仍然不能得到这个工作,没有出现在通知栏。这是到目前为止要最小化的完整代码:
private void button6_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void Form_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
private void notifyIcon_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
为什么不工作?
永远不要在通知区域显示任何内容。跟踪你的代码,看看发生了什么。我添加了一些注释:
private void button6_Click(object sender, EventArgs e)
{
// When button 6 is clicked, minimize the form.
this.WindowState = FormWindowState.Minimized;
}
private void Form_Resize(object sender, EventArgs e)
{
// Catch the case where the form is minimized, including but not limited to
// clicks on button 6.
if (WindowState == FormWindowState.Minimized)
{
// In that case, hide the form.
this.Hide();
}
}
private void notifyIcon_Click(object sender, EventArgs e)
{
// If the notification icon is clicked, reshow the form as a normal window.
this.Show();
this.WindowState = FormWindowState.Normal;
}
现在注意到问题了吗?当窗体最小化时,您所要做的就是隐藏它。你从来没有告诉NotifyIcon
显示它的图标!"Visible
"属性的默认值为"false
"。你必须将其设置为true
以使图标显示,并将其设置为false
以使其消失。
所以修改代码如下:
private void Form_Resize(object sender, EventArgs e)
{
// Catch the case where the form is minimized, including but not limited to
// clicks on button 6.
if (WindowState == FormWindowState.Minimized)
{
// In that case, hide the form.
this.Hide();
// And display the notification icon.
notifyIcon.Visible = true;
// TODO: You might also want to set other properties on the
// notification icon, like Text and/or Icon.
}
}
private void notifyIcon_Click(object sender, EventArgs e)
{
// If the notification icon is clicked, reshow the form as a normal window.
this.Show();
this.WindowState = FormWindowState.Normal;
// And hide the icon in the notification area.
notifyIcon.Visible = false;
}
试试下面的代码:
private NotifyIcon notifyIcon;
public Form1()
{
InitializeComponent();
button6.Click += (sender, e) =>
{
this.WindowState = FormWindowState.Minimized;
};
this.Resize += (sender, e) =>
{
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip(1000);
}
};
notifyIcon = new NotifyIcon()
{
Text = "I'm here!",
BalloonTipText = "I'm here!",
Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath)
};
notifyIcon.Click += (sender, e) =>
{
this.Show();
this.WindowState = FormWindowState.Normal;
notifyIcon.Visible = false;
};
}