您永远不会在通知区域中显示任何内容。跟踪您的代码并尝试查看发生了什么。我添加了一些评论:
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;
}