使用NotifyIcon时,Windowstate Minimized不起作用

时间:2011-08-30 08:34:51

标签: c# winforms notifyicon

我正在尝试在表单关闭时显示NotifyIcon。它关闭但是当我点击最小化按钮时它也关闭了。这是我的代码。

    private void Home_Resize(object sender, EventArgs e)
    {

        if (FormWindowState.Minimized == this.WindowState)
        {
            notifyIcon1.Visible = true;
            notifyIcon1.ShowBalloonTip(500);
            this.Hide();
        }

        else if (FormWindowState.Normal == this.WindowState)
        {
            notifyIcon1.Visible = true;
        }
    }

    private void notifyIcon1_DoubleClick(object sender, EventArgs e)
    {
        if (this.WindowState == FormWindowState.Minimized)
        {
            this.Show();
            this.Activate();
            this.WindowState = FormWindowState.Normal;

        }
    }

    private void Home_FormClosing(object sender, FormClosingEventArgs e)
    {   
        e.Cancel = true;
        this.WindowState = FormWindowState.Minimized;           
    }

    private void toolStripMenuItem1_Click_1(object sender, EventArgs e)
    {
        //Exit App
        notifyIcon1.Visible = false;
        Environment.Exit(0);
    }

1 个答案:

答案 0 :(得分:1)

只需将代码从Resize事件处理程序移动到FormClosing事件处理程序即可。同时检查e.CloseReason,当Windows关闭时,您的表单需要关闭。