Click here to Skip to main content
Click here to Skip to main content

Minimize window to system tray

By , 8 Jul 2008
 

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
Software Developer
United States United States
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberAbhishek Pant24 Oct '12 - 6:56 
Great!
QuestionThis code is not working in VS2010. How to make it work ?memberMember 915755722 Jun '12 - 18:30 
Hi,
If I upgraded the demo project to work in VS2010, it's working successfully. But if I try to create a new project using this code in VS2010, it's not working. Can anyone help me please ...
AnswerRe: This code is not working in VS2010. How to make it work ?memberHBeaudry13 Jul '12 - 3:17 
Bizarre. I just converted (with errors) to VS2010 but the application works. I can minimize and see only an icon in the system tray. I double-click on the icon and the form comes back again.
 
Microsoft Visual Studio 2010
Version 10.0.40219.1 SP1Rel
Microsoft .NET Framework
Version 4.0.30319 SP1Rel
Installed Version: Premium
 
Languages, Hot fixes...etc.
GeneralMy vote of 5memberMd. Humayun Rashed8 Jun '12 - 8:43 
excellent..
QuestionRe: My vote of 5memberMember 915755722 Jun '12 - 18:49 
Hi,
I'm new to C#. I tried this code in VS2010 but not working. Can you pl. send me a sample application which works in VS2010. I upgraded the demo project to VS2010 and it's working fine. But if try to make a new project in VS2010 using this code, it's not working.
Kindly help me out.
Thanks & Regards
Rakesh
Email: rakesh.ieee1@gmail.com
GeneralMy vote of 5membersixthplanets4 Apr '12 - 22:29 
This Program is a symbol of excellence... Thank You..
GeneralThank you!memberCosmin Serban20 Jan '12 - 22:34 
Concise and simple, all that I needed.
Thanks!
QuestionThank you! +5memberjclark239b96 Dec '11 - 9:32 
Very helpful, got my app working like I want it to now.
QuestionI follow exactly all your coding, but it still does not minimize to tray, Any reason why?memberKen Than13 Jul '11 - 21:01 
First I drag a notifyicon from toolbox to form, and set the icon in property. Then I paste your code to the form as below:
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace minimizeTray
{
public partial class TrayMinimizerForm : Form
{
 

 

public TrayMinimizerForm()
{
InitializeComponent();
}
 
private void TrayMinimizerForm_Load(object sender, EventArgs e)
{
 
}
 
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;
}
}
}
GeneralRe: I follow exactly all your coding, but it still does not minimize to tray, Any reason why?memberMember 915755722 Jun '12 - 18:40 
I'm also facing the same problem while creating a new project using VS2010. Have you resolved this problem ? If yes, please let me know how to make this code work in VS2010.
SuggestionRe: I follow exactly all your coding, but it still does not minimize to tray, Any reason why?memberAbhishek Pant24 Oct '12 - 6:55 
You can review you icon notification settings i also got this same problem first time but i'm still Suspicious | :suss: what was happened after reviewing my setting?
 
good luck.
overall Article was great
GeneralMy vote of 5memberMember 80638418 Jul '11 - 9:13 
Simple and very efficient..
QuestionRe: My vote of 5memberMember 915755722 Jun '12 - 18:42 
Hi, I tried this code in VS2010. It's not working. Can you pl. let me know how to make it work ?
GeneralMy vote of 5membersaba_aravind1 Nov '10 - 5:51 
Thank you for sharing
GeneralRestoring maximized state,memberVozzie26 Sep '09 - 4:41 
It works fine but doesn't restore a window to maximized when it came from a maximized state.
 
I made some changes to solve this,... in VB.Net but it should't be a problem to translate it to C#...
 
    Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
        Me.Show()
        ' restore SendMessage WM_SYSCOMMAND, SC_RESTORE,...?
        Const WM_SYSCOMMAND As UInt32 = &H112UI
        Const SC_RESTORE As Integer = &HF120
        SendMessage(Me.Handle, WM_SYSCOMMAND, SC_RESTORE, 0)
        'Me.WindowState = FormWindowState.Normal
    End Sub
 
    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        If Me.WindowState = FormWindowState.Minimized Then
            NotifyIcon1.Visible = True
            Me.Hide()
        Else
            NotifyIcon1.Visible = False
        End If
    End Sub
 
    <System.Runtime.InteropServices.DllImport("user32.dll")> _
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal uMsg As UInt32, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    End Function
 

With regards,...
 
It feels good to learn and achieve

Generalquestionmembermaditude29 Mar '09 - 15:23 
First, thanks for your article - was just what I was looking for!
Here's a minor touch to your example, so that the control-box [X] button also minimizes-to-tray:
 
1) add a "bool keep_running = true;" line to the form's code.
2) add a "keep_running = false;" line to the exitToolStripMenuItem_Click() handler.
3) wire up a handler for the form's FormClosing event.
 

private void TrayMinimizerForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (keep_running)
{
WindowState = FormWindowState.Minimized;
e.Cancel = true;
}
}

GeneralRe: questionmemberElroy Dsilva6 Sep '09 - 4:52 
Yeah. Thanks. That's another way to do it.
 

Judge a man by his questions rather than his answers. - Voltaire





GeneralRe: questionmemberjclark239b96 Dec '11 - 9:31 
Thanks, great addition.
GeneralGood jobmemberdanzar19 Dec '08 - 17:27 
Very good job. Its good clean and simple.
GeneralSwitchmemberPIEBALDconsult8 Jul '08 - 6:42 
Why not use switch ( this.WindowState ) ?
GeneralRe: SwitchmemberElroy Dsilva8 Jul '08 - 6:52 
Thanks for the question.
Yes, you can very well use switches instead of "If-else" if you feel if-else looks ugly. However, they boil down to the same thing(condition check). Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 8 Jul 2008
Article Copyright 2008 by Elroy Dsilva
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid