Click here to Skip to main content
Licence CPOL
First Posted 8 Jul 2008
Views 49,457
Downloads 1,368
Bookmarked 49 times

Minimize window to system tray

By Elroy Dsilva | 8 Jul 2008
This article describes how you can write some simple code for minimizing a window to the system tray.
7 votes, 31.8%
1

2
1 vote, 4.5%
3
2 votes, 9.1%
4
12 votes, 54.5%
5
3.00/5 - 22 votes
μ 3.06, σa 3.26 [?]

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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThank you! PinmemberCosmin Serban23:34 20 Jan '12  
QuestionThank you! +5 Pinmemberjclark239b910:32 6 Dec '11  
QuestionI follow exactly all your coding, but it still does not minimize to tray, Any reason why? PinmemberKen Than22:01 13 Jul '11  
GeneralMy vote of 5 PinmemberMember 806384110:13 8 Jul '11  
GeneralMy vote of 5 Pinmembersaba_aravind6:51 1 Nov '10  
GeneralRestoring maximized state, PinmemberVozzie25:41 6 Sep '09  
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

Generalquestion Pinmembermaditude16:23 29 Mar '09  
GeneralRe: question PinmemberElroy Dsilva5:52 6 Sep '09  
GeneralRe: question Pinmemberjclark239b910:31 6 Dec '11  
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    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

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