Windows应用程序中的NotifyIcon和Form.Resize事件

时间:2014-03-12 05:56:39

标签: c# winforms systray

我正在努力减少我对系统托盘的winapp。我从codeproject下载了一个示例项目。但它会在Form.Resize事件上进入systary。代码 -

    private void Form_Resize(object sender, EventArgs e)
    {
        notifyIcon1.BalloonTipTitle = "Minimize to Tray App";
        notifyIcon1.BalloonTipText = "You have successfully minimized your form.";

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

是否有必要在resize事件中处理它?我可以在按钮点击事件上执行此操作吗?

1 个答案:

答案 0 :(得分:1)

您可以在按钮中执行此操作。出于显而易见的原因,您不能依赖按钮中的WindowState,因为只有当窗口没有最小化到托盘时才能单击它。

private void button1_Click(object sender, EventArgs e)
{
    notifyIcon1.BalloonTipTitle = "Minimize to Tray App";
    notifyIcon1.BalloonTipText = "You have successfully minimized your form.";

    notifyIcon1.Visible = true;
    notifyIcon1.ShowBalloonTip(500);
    this.Hide();
} 

这应该可以“最小化”到托盘。虽然它应该被称为hide-on-button-click-to-tray。