25 November 2008

C# System Tray Minimize To Tray With NotifyIcon

Minimize application form to system tray is done
with the NotifyIcon control in Visual Studio.

NotifyIcon is in the System.Windows.Forms namespace.
Drag and drop a NotifyIcon control to your form.
To send your application form into the system tray,
we simple handle the form re-size event.
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;
}
}
We can also set BallonTipIcon, BallonTipText, BallonTipTitle and Icon ,
for our NotifyIcon control.
 mynotifyicon.BallonTipText = "My application still working...";
mynotifyicon.BallonTipTitle = "My Sample Application";
mynotifyicon.BallonTipIcon = ToolTipIcon.Info;

12 comments:

  1. Norbert Willhelm9/22/2010 6:17 AM

    Thank you.

    ReplyDelete
  2. Thank You!!!

    ReplyDelete
  3. Thank you, really it's helpful

    ReplyDelete
  4. Thank you very much,
    but I think there is a little mistake in the second code block:
    You wrote balloon just with one "o" ;).

    ReplyDelete
  5. Keep in mind you may need

    //
    // frmMain
    //
    this.Resize += new System.EventHandler(this.frmMain_Resize);




    in your Designer.cs

    ReplyDelete
  6. Dominik Ras
    THANK YOU for advise
    i need that to minimize

    ReplyDelete
  7. when the applicarion are running, in windows xp i can´t shut down the pc, i need close the aplication and after shut down the pc. Anity idea??????

    ReplyDelete
  8. For a form that minimizes to the tray to be brought back up by a click of the NotifyIcon, I use

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

    private void notifyIconGT_Click(object sender, EventArgs e)
    {
    notifyIconGT.Visible = false;
    this.Show();
    this.WindowState = FormWindowState.Normal;
    }


    Make sure the NotifyIcon is marked as not visible from within the form designer.

    ReplyDelete

Note: Only a member of this blog may post a comment.