Click here to Skip to main content
6,822,613 members and growing! (20,573 online)
Email Password   helpLost your password?
Desktop Development » Dialogs and Windows » General     Intermediate License: The Code Project Open License (CPOL)

Minimize window to system tray

By Elroy Dsilva

This article describes how you can write some simple code for minimizing a window to the system tray.
C# (C#2.0), Windows (WinXP), Dev
Posted:8 Jul 2008
Views:21,002
Bookmarked:36 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
13 votes for this article.
Popularity: 2.63 Rating: 2.36 out of 5
7 votes, 53.8%
1

2
1 vote, 7.7%
3
2 votes, 15.4%
4
3 votes, 23.1%
5

Introduction

Minimizing a window to the system tray makes your application somewhat cool, isn’t it? This functionality can be really useful when you want to run an application in the background, for example, chat applications, anti-viruses, and programs that monitor the state of your machine.

Background

This article requires you to have basic knowledge of programming Windows Forms in C#.

Using the Code

Now, let’s see what we need to include this functionality in any Windows application. There are a few very simple steps towards achieving this.

  1. Create a new Windows Application project using Visual Studio.
  2. You’ll have a default Form opened up for you. From the Toolbox, add a NotifyIcon control to your form.
  3. 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.
  4. 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.

Remember to set the notifyIcon1.Icon property to a valid Icon resource object; otherwise, the system tray will not show any icon, and your window will never return to the foreground.

I have added some code below to get things started:

//  The NotifyIcon object
private System.Windows.Forms.NotifyIcon notifyIcon1;
this.notifyIcon1.Icon = 
  ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));


private void TrayMinimizerForm_Resize(object sender, EventArgs e)
{
     notifyIcon1.BalloonTipTitle = "Minimize to Tray App";
     notifyIcon1.BalloonTipText = "You have successfully minimized your form.";

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

Points of Interest

Some window applications need to stay running as long as the computer is on; for example, Microsoft Outlook, Yahoo chat etc., need to run all the time unless they are explicitly terminated. This concept of minimizing windows to the system tray is very helpful when too many open windows crowd up the taskbar.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Elroy Dsilva


Member
Elroy is a software developer whose technical experitise and learning include programming languages such as C, C++ and C#. He loves writing simple technical articles and blogging whenever He finds some time for himself.
Occupation: Software Developer
Location: United States United States

Other popular Dialogs and Windows articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
GeneralRestoring maximized state, PinmemberVozzie25:41 6 Sep '09  
Generalquestion Pinmembermaditude16:23 29 Mar '09  
GeneralRe: question PinmemberElroy Dsilva5:52 6 Sep '09  
GeneralGood job Pinmemberdanzar18:27 19 Dec '08  
GeneralSwitch PinmemberPIEBALDconsult7:42 8 Jul '08  
GeneralRe: Switch PinmemberElroy Dsilva7:52 8 Jul '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 8 Jul 2008
Editor: Smitha Vijayan
Copyright 2008 by Elroy Dsilva
Everything else Copyright © CodeProject, 1999-2010
Web17 | Advertise on the Code Project