NotifyIcon和Form.在Windows应用程序中调整大小事件

本文关键字:调整 大小事 小事件 应用程序 Windows Form NotifyIcon | 更新日期: 2023-09-27 17:49:53

我正在尝试最小化我的winapp到系统托盘。我已经从codeproject下载了一个示例项目。但它会进入系统形式。调整大小事件。代码——

    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;
        }
    }

有必要在调整大小事件时处理它吗?我可以在按钮点击事件?

NotifyIcon和Form.在Windows应用程序中调整大小事件

您可以在您的按钮中这样做。很明显,你不能依赖按钮中的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