1

(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;               
        }
user7968180
  • 145
  • 1
  • 15

3 Answers3

3

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.

adjan
  • 13,371
  • 2
  • 31
  • 48
2

Minimizing to the tray is a bit of a hack. To accomplish it you need to:

  • Capture the minimize event and cancel it to prevent actual minimization
  • Hide your application window with this.Hide()
  • Listen for a click event on the taskbar icon and unhide the application window when clicked
Soviut
  • 88,194
  • 49
  • 192
  • 260
  • I updated why this.Hide() does not work to me in question. – user7968180 Jun 13 '17 at 17:25
  • 1
    If you listen for a `Click` event from the `NotifyIcon` and do `this.Show()` it will show up immediately. – Soviut Jun 13 '17 at 18:16
  • 1
    You can also configure the app now to show up in the taskbar. – Soviut Jun 13 '17 at 18:17
  • 1
    That's normal windows behaviour; It hides icons that the user doesn't consider important. You can tell windows to always show your icon, but your application has no control over it. – Soviut Jun 13 '17 at 18:21
1

Your main window and its notification icon have no relation in the sense of activation.

You generally wouldn't want a running application to disappear and not be able to activate it again. Windows is therefore giving the user of your application the option to reactivate the window by either clicking its taskbar icon (which you want to hide) or by clicking its border (which you also don't want to show).

To circumvent this, simply hide your main window when it's minimized, and unhide it when your notification icon is (double)clicked.

This is explained with fewer words and more code in 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 and so on.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272