Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

minimize app to system tray

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.

like image 479
WantIt Avatar asked Oct 02 '22 20:10

WantIt


People also ask

What does minimizing to system tray do?

To interact with a program in the system tray, select an icon with the mouse and double-click or right-click the icon. When minimizing the program after using it, it shrinks back into the system tray instead of into the main part of the taskbar.

How do I move a program to my system tray?

To send an active open window to the system tray and remove it from the taskbar, press Alt + F1 , then press Alt + F2 to restore the last minimized window. Alternatively, if you want to restore all your minimized system tray windows at once, press F10 .


2 Answers

  • C# System Tray Minimize To Tray With NotifyIcon
  • Minimize window to system tray

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;
    }
}
like image 159
CD.. Avatar answered Oct 18 '22 23:10

CD..


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]";
like image 75
Ben Gripka Avatar answered Oct 18 '22 22:10

Ben Gripka