Search code examples
c#wpfsystem-tray

Trouble with minimizing app to system tray


(WPF C#) I want to minimize app to system tray, but without icon in taskbar: enter image description here So i set:

this.ShowInTaskbar = false;

Then undesirable icon disappeared but new bar appears on desktop!

enter image description here

Does somebody please have idea how to solve this problem after minimization?

Here is my important part of code:

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

Solution

  • Try to call

    this.Hide()

    when the form is minimized, preferably in the Form.Resize event handler:

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

    At some point you have to call

    this.Show()

    for example, in the DoubleClick handler of the NotifyIcon.