最小化托盘的问题

时间:2013-01-07 21:21:23

标签: c# winforms icons system-tray trayicon

在我的Windows窗体应用程序(C#)中,我有这样的代码:

            private void frm_main_Resize(object sender, EventArgs e)
    {
        if ((this.WindowState == FormWindowState.Minimized) && (checkBox1.Checked))
        {
            this.ShowInTaskbar = false;

            notifyIcon1.Visible = true;
        }
    }

    private void notifyIcon1_DoubleClick(object Sender, EventArgs e)
    {
        if (this.WindowState == FormWindowState.Minimized)
        {
            this.WindowState = FormWindowState.Normal;
        }
        else
        {
            this.WindowState = FormWindowState.Minimized;
        }

        this.Activate();
    }

我的publick表单有双击处理程序notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);

当最小化的应用程序仍在任务栏中出现时,如何更改?我希望在最小化状态下它只在系统托盘中。为什么这可以解决问题呢?

1 个答案:

答案 0 :(得分:2)

我在我的项目中使用了类似的代码,但是你不需要隐藏和显示:

private void frm_main_Resize(object sender, EventArgs e)
{
    if (this.WindowState == FormWindowState.Minimized && checkBox1.Checked)
    {
        this.ShowInTaskbar = false;
        notifyIcon1.Visible = true;
    }
}

还尝试处理MouseClick事件而不是DoubleClick

  private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
  {
     if (e.Button == System.Windows.Forms.MouseButtons.Left && e.Clicks == 2)
     {
        this.WindowState = FormWindowState.Normal;
        this.ShowInTaskbar = true;
        notifyIcon1.Visible = false;
        this.Activate();
     }
  }