Click here to Skip to main content
Licence CPOL
First Posted 6 Nov 2000
Views 357,542
Bookmarked 134 times

System Tray Icons - Minimize Your Application to the SysTray

By | 6 Nov 2000 | Article
Minimize your application to system tray instead of the taskbar

Sample Image - TrayIcons.jpg

Introduction

This article illustrates the use of Shell_NotifyIcon to create and manage System Tray icons. The article explains the basics of this operation and assists you in creating your own 'Minimize To Tray' applications. The source code provided with this article is designed to work with dialog-based applications, but it can easily be modified to work with a CFrameWnd or CWnd based application.

Creating and Using Tray Icons

To create a Tray Icon, you need to call the following shell function: -

BOOL Shell_NotifyIcon( DWORD dwMessage, PNOTIFYICONDATA pnid );

The dwMessage parameter specifies the action to be taken - NIM_ADD, NIM_DELETE, NIM_MODIFY adds, deletes and modifies tray icons respectively.

The pnid parameter is used to customize, create, delete and obtain data from the Tray Icon. (See the MSDN Library for more details about this structure.)

Creating The Application

1. Create a new VC++ dialog based project. For this example, I will call this project MyTray which will contain the CMyTrayApp and CMyTrayDlg classes.

2. Download and extract the DialogTray source code to the root of the project folder

3. From the Project->Add To Project menu, select Files and then select TrayDialog.h and TrayDialog.cpp. This will add a new class to your project named CTrayDialog.

4. Replace the CMyTrayDlg base class with CTrayDialog.

class CMyTrayDlg : public CDialog

becomes

#include "TrayDialog.h"

class CMyTrayDlg : public CTrayDialog

5. Replace the other occurrences of CDialog in the MyTrayDlg.cpp file as follows :-

CMyTrayDlg::CMyTrayDlg(CWnd* pParent /*=NULL*/)
  : CDialog(CMyTrayDlg::IDD, pParent)

becomes

CMyTrayDlg::CMyTrayDlg(CWnd* pParent /*=NULL*/)	
    : CTrayDialog(CMyTrayDlg::IDD, pParent)

6. Create a menu resource and name it IDR_MENU1

7. In the InitDialog member function, enter the following:

    TraySetIcon(IDR_MAINFRAME);
    TraySetToolTip("ToolTip for tray icon");
    TraySetMenu(IDR_MENU1);

8. Modify the IDD_MYTRAY _DIALOG resource to have a minimize box.

9. Build and run the application

NB : To add tray menu item handlers use the class wizard.

Displaying the tray icon all the time

Simply add a TrayShow() statement to InitDialog() in CMyTrayDlg.cpp, and call TraySetMinimizeToTray(FALSE) to disable minimizing to the tray.

The events that occur in the tray are captured through the following functions:

    virtual void OnTrayLButtonDown(CPoint pt);
    virtual void OnTrayLButtonDblClk(CPoint pt);

	virtual void OnTrayRButtonDown(CPoint pt);
	virtual void OnTrayRButtonDblClk(CPoint pt);
  
	virtual void OnTrayMouseMove(CPoint pt);

Feel free to add more events or to improve on these ones.

License

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

About the Author

Daniel Zilcsak

Software Developer

United States United States

Member



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
QuestionHow to do If I want it be Tray Mode while begging? Pinmembervannes1:12 23 Dec '11  
Questionprogramatically minimizing does not work on VS 2005 PinmemberErik19:01 17 Oct '11  
QuestionCWinApp PinmemberMember 806538823:52 25 Aug '11  
AnswerRe: CWinApp PinmemberMember 80653880:13 26 Aug '11  
QuestionLicencing Pinmemberrainer.hochdorfer22:06 27 Apr '10  
AnswerRe: Licencing PinmemberDaniel Zilcsak3:29 28 Apr '10  
AnswerRe: Licencing Pinmemberrainer.hochdorfer6:02 28 Apr '10  
GeneralThanks just perfect Pinmemberasierra019:14 11 Jan '10  
GeneralRight Click Menu PinmemberShup1:20 15 May '09  
GeneralThank you! PinmemberDevMan778:20 12 May '09  
GeneralChanging tray icon while minimized Pinmembereight2:21 26 Mar '09  
GeneralThanks!!! PinmemberIAM...5:56 15 Feb '09  
QuestionOnly minimizing to tray in my application? PinmemberMember 19211925:03 31 Oct '08  
QuestionBalloon Tip?? Pinmemberthanasis_rassp3:16 12 Aug '08  
AnswerRe: Balloon Tip?? Pinmembervarandas795:25 24 Sep '08  
GeneralBug in CTrayDialog::OnSysCommand preventing sizing PinmemberEricGen23:02 24 Mar '08  
GeneralSystem get slow... PinmemberMember 385006721:52 28 Feb '08  
Generalsmall but useful demo Pinmemberlilesh22:33 10 Oct '07  
QuestionHow can I get my app on the tray at the very beginning? Pinmemberpjvv17:31 26 Sep '07  
AnswerSolution Pinmemberlilesh23:01 10 Oct '07  
Questionsystray without window Pinmemberchergui1:36 3 Apr '07  
GeneralIn VC++ 2003, there's error Pinmembertlbf2:46 10 Jan '07  
GeneralRe: In VC++ 2003, there's error Pinmemberchild00717:32 28 Jan '08  
GeneralRe: In VC++ 2003, there's error PinmemberJerry Peterson3:15 14 May '09  
Generaltray disappear after mouse over PinmembergeoPsyc13:05 4 Dec '06  
I got the program compiled and running, I see the icon in the tray and when I put my mouse over the icon it disappears, but the dialog and the program is still open and running, any help thanks!

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
Web02 | 2.5.120529.1 | Last Updated 7 Nov 2000
Article Copyright 2000 by Daniel Zilcsak
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid