将应用程序最小化到系统托盘时出现问题

Trouble with minimizing app to system tray

(WPF C#) 我想将应用程序最小化到系统托盘,但任务栏中没有图标: 所以我设置:

this.ShowInTaskbar = false;

然后不需要的图标消失了,但桌面上出现了新栏!

有人知道最小化后如何解决这个问题吗?

这是我的代码的重要部分:

private void stateChangedEvent(object sender, EventArgs e)
        {            
            if (WindowState.Minimized == WindowState)
            {
                this.ShowInTaskbar = false;
                var iconHandle = Properties.Resources.iconPaw.GetHicon();
                notifyIcon.Icon = System.Drawing.Icon.FromHandle(iconHandle);
                notifyIcon.Click += new EventHandler(this.WindowsStateToNormal);
                notifyIcon.Visible = true;
                notifyIcon.BalloonTipText = "Radek app";
                notifyIcon.BalloonTipTitle = "Welcome Message";
                notifyIcon.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info;
                notifyIcon.ShowBalloonTip(3000);
            }
        }

        private void WindowsStateToNormal(object Sender, EventArgs e)
        {
            this.WindowState = WindowState.Normal;
            notifyIcon.Visible = false;               
        }

你的主window和它的通知图标在激活意义上没有关系。

您通常不希望 运行 应用程序消失并且无法再次激活它。 Windows 因此为您的应用程序用户提供了重新激活 window 的选项,方法是单击其任务栏图标(您想要隐藏)或单击其边框(您也不想隐藏)显示).

要避免这种情况,只需在主 window 最小化时将其隐藏,并在(双击)单击通知图标时将其取消隐藏。

这在 minimize app to system tray, How do I minimize a WinForms application to the notification area?, Minimize to tray, Minimizing a system windows form in tray in C# without seeing it hanging where taskbar 等中用更少的单词和更多的代码进行解释。

尝试调用

this.Hide()

表单最小化时,最好在Form.Resize事件处理程序中:

private void frmMain_Resize(object sender, EventArgs e)
{
    if (this.WindowState == FormWindowState.Minimized)
       this.Hide();
}

在某些时候你必须调用

this.Show()

例如,在 NotifyIconDoubleClick 处理程序中。

最小化到托盘有点麻烦。要完成它,您需要:

  • 捕获最小化事件并取消它以防止实际最小化
  • this.Hide()
  • 隐藏您的应用程序 window
  • 侦听任务栏图标上的单击事件并在单击时取消隐藏应用程序window