123

I have a Windows forms app powered by C# and Visual Studio 2010.

How can I minimize my app to system tray (not taskbar), then bring it back when doubled click in the system tray? any idea? also, how can I make some menu in the icon in system tray and when I right click it, it shows a menu like Login, Disconnect, Connect, something like that. Also, are there any methods to show like a baloon popping up from the system tray?

PS: I already added a notifyIcon, but I do not know how to use it.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
vvavepacket
  • 1,882
  • 4
  • 25
  • 38
  • Here you can download a great sample code https://www.simple-talk.com/dotnet/.net-framework/creating-tray-applications-in-.net-a-practical-guide/ – NoWar Nov 27 '15 at 19:07
  • You are going to want to be sure to go into the form's Designer.cs and "register" the event handler or none of these will work (I could not figure it out). You can do this by adding something like this: this.Resize += new System.EventHandler(this.ManagementForm_Resize); – Brad Mar 11 '16 at 13:41

9 Answers9

160

Handle the form’s Resize event. In this handler, you override the basic functionality of the Resize event to make the form minimize to the system tray and not to the taskbar. This can be done by doing the following in your form’s Resize event handler: Check whether the form’s WindowState property is set to FormWindowState.Minimized. If yes, hide your form, enable the NotifyIcon object, and show the balloon tip that shows some information. Once the WindowState becomes FormWindowState.Normal, disable the NotifyIcon object by setting its Visible property to false. Now, you want the window to reappear when you double click on the NotifyIcon object in the taskbar. For this, handle the NotifyIcon’s MouseDoubleClick event. Here, you show the form using the Show() method.

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

    else if (FormWindowState.Normal == this.WindowState)
    {
       mynotifyicon.Visible = false;
    }
}
Brian
  • 5,069
  • 7
  • 37
  • 47
CD..
  • 72,281
  • 25
  • 154
  • 163
  • 5
    you can change the Form.FormBorderStyle Property http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formborderstyle.aspx – CD.. Oct 02 '11 at 09:12
  • tnx! it worked.. also any idea to disable those minimize, close, maximize buttons in top right? tnx – vvavepacket Oct 02 '11 at 09:16
  • 8
    http://stackoverflow.com/questions/3025923/disabling-minimize-maximize-on-winform – CD.. Oct 02 '11 at 09:17
  • tnx it worked too! also, when i double click the icon in system tray, the window is not on top with other applications, i have to take effort by clicking the window in the taskbar for it to show up. How can we handle this? tnx so much – vvavepacket Oct 02 '11 at 09:25
  • 7
    http://stackoverflow.com/questions/683330/how-to-make-a-window-always-stay-on-top-in-net – CD.. Oct 02 '11 at 09:34
  • Also, just wanted to note that it seems like `Resize` is one of those places where [exceptions can get swallowed](http://stackoverflow.com/q/4933958/119527). – Jonathon Reinhart Nov 07 '13 at 03:23
  • Please also mentioned following instructions otherwise gives error mynotifyicon.BallonTipText = "My application still working..."; mynotifyicon.BallonTipTitle = "My Sample Application"; mynotifyicon.BallonTipIcon = ToolTipIcon.Info; –  Apr 30 '14 at 16:42
76

I found this to accomplish the entire solution. The answer above fails to remove the window from the task bar.

private void ImportStatusForm_Resize(object sender, EventArgs e)
{
    if (this.WindowState == FormWindowState.Minimized)
    {
        notifyIcon.Visible = true;
        notifyIcon.ShowBalloonTip(3000);
        this.ShowInTaskbar = false;
    }
}

private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
    this.WindowState = FormWindowState.Normal;
    this.ShowInTaskbar = true;
    notifyIcon.Visible = false;
}

Also it is good to set the following properties of the notify icon control using the forms designer.

this.notifyIcon.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info; //Shows the info icon so the user doesn't think there is an error.
this.notifyIcon.BalloonTipText = "[Balloon Text when Minimized]";
this.notifyIcon.BalloonTipTitle = "[Balloon Title when Minimized]";
this.notifyIcon.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon.Icon"))); //The tray icon to use
this.notifyIcon.Text = "[Message shown when hovering over tray icon]";
Chad Hedgcock
  • 11,125
  • 3
  • 36
  • 44
Ben Gripka
  • 16,012
  • 6
  • 45
  • 41
  • 9
    You're missing `this.Show();` before `this.WindowState = FormWindowState.Normal;` – developerwjk Sep 11 '14 at 22:13
  • 1
    Typo in "the user doesn't thing". ;) – Andrew Dec 22 '16 at 05:09
  • 1
    @developerwjk, it's not missing because he's not calling `Hide()`. But the problem with that approach is that the window still appears when you press Alt-Tab. I prefer calling `Hide()` and that way you don't have to use the `ShowInTaskbar` property at all. – Andrew Dec 22 '16 at 05:18
  • @Andrew I'm having problems when using `Show()` and `Hide()`; after hiding,showing, and then hiding, it won't show again. – newbieguy Dec 08 '19 at 02:21
  • @newbieguy, I guess you need to create a new question, as your problem is not a good fit for a comment on an answer. You will need to provide more information, as the code you are using. You may include a link to the answer you tried to use. – Andrew Dec 09 '19 at 14:44
22

don't forget to add icon file to your notifyIcon or it will not appear in the tray.

ewwink
  • 18,382
  • 2
  • 44
  • 54
  • the most needed part in the topic. Can you add a bit more info, how to force it to use the default icon? – T.Todua Jan 09 '20 at 19:41
17

I'd go with

private void Form1_Resize(object sender, EventArgs e)
{
     if (FormWindowState.Minimized == this.WindowState)
     {
          notifyIcon1.Visible = true;
          notifyIcon1.ShowBalloonTip(500);
          this.Hide();    
     }
     else if (FormWindowState.Normal == this.WindowState)
     {
          notifyIcon1.Visible = false;
     }
}

private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
     this.Show();
     this.WindowState = FormWindowState.Normal;
}
Alejandro del Río
  • 3,966
  • 3
  • 33
  • 31
13

try this

 private void Form1_Load(object sender, EventArgs e)
    {
        notifyIcon1.BalloonTipText = "Application Minimized.";
        notifyIcon1.BalloonTipTitle = "test";
    }

    private void Form1_Resize(object sender, EventArgs e)
    {
        if (WindowState == FormWindowState.Minimized)
        {
            ShowInTaskbar = false;
            notifyIcon1.Visible = true;
            notifyIcon1.ShowBalloonTip(1000);
        }
    }

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        ShowInTaskbar = true;
        notifyIcon1.Visible = false;
        WindowState = FormWindowState.Normal;
    }
Tanmay Nehete
  • 2,138
  • 4
  • 31
  • 42
  • 2
    If you just hide from taskbar instead of calling `this.Hide()`, the window still appears when doing Alt-Tab. – Andrew Dec 22 '16 at 05:17
3

This is the method I use in my applications, it's fairly simple and self explanatory but I'm happy to give more details in answer to your comments.

    public Form1()
    {
        InitializeComponent();

        // When window state changed, trigger state update.
        this.Resize += SetMinimizeState;

        // When tray icon clicked, trigger window state change.       
        systemTrayIcon.Click += ToggleMinimizeState;
    }      

    // Toggle state between Normal and Minimized.
    private void ToggleMinimizeState(object sender, EventArgs e)
    {    
        bool isMinimized = this.WindowState == FormWindowState.Minimized;
        this.WindowState = (isMinimized) ? FormWindowState.Normal : FormWindowState.Minimized;
    }

    // Show/Hide window and tray icon to match window state.
    private void SetMinimizeState(object sender, EventArgs e)
    {    
        bool isMinimized = this.WindowState == FormWindowState.Minimized;

        this.ShowInTaskbar = !isMinimized;           
        systemTrayIcon.Visible = isMinimized;
        if (isMinimized) systemTrayIcon.ShowBalloonTip(500, "Application", "Application minimized to tray.", ToolTipIcon.Info);
    }
Wasp
  • 59
  • 6
  • 2
    This and other solutions have one issue: if the form is hidden under other windows, the user most likely doesn't want to minimize it when clicking on the icon. Instead it should be focused. See my question here: http://stackoverflow.com/questions/36797442/proper-way-of-activating-my-mainform-from-a-notifyicon-if-it-wasnt-focused-alre – maf-soft Apr 22 '16 at 16:36
1

At the click on the image in System tray, you can verify if the frame is visible and then you have to set Visible = true or false

1

...and for your right click notification menu add a context menu to the form and edit it and set mouseclick events for each of contextmenuitems by double clicking them and then attach it to the notifyicon1 by selecting the ContextMenuStrip in notifyicon property.

Ryu
  • 191
  • 10
-3

this.WindowState = FormWindowState.Minimized;