Click here to Skip to main content
6,290,721 members and growing! (12,842 online)
Email Password   helpLost your password?
Desktop Development » Shell and IE programming » General     Intermediate

System Tray Icons - Minimize Your Application to the SysTray

By Daniel Zilcsak

Minimize your application to system tray instead of the taskbar
VC6Win2K, Dev
Posted:6 Nov 2000
Views:260,219
Bookmarked:112 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
97 votes for this article.
Popularity: 8.98 Rating: 4.52 out of 5
2 votes, 2.9%
1
2 votes, 2.9%
2
3 votes, 4.3%
3
16 votes, 23.2%
4
46 votes, 66.7%
5

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Daniel Zilcsak


Member

Occupation: Software Developer
Location: United States United States

Other popular Shell and IE programming articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 126 (Total in Forum: 126) (Refresh)FirstPrevNext
GeneralRight Click Menu PinmemberShup2:20 15 May '09  
GeneralThank you! PinmemberDevMan779:20 12 May '09  
GeneralChanging tray icon while minimized Pinmembereight3:21 26 Mar '09  
GeneralThanks!!! PinmemberIAM...6:56 15 Feb '09  
GeneralOnly minimizing to tray in my application? PinmemberMember 19211926:03 31 Oct '08  
GeneralBalloon Tip?? Pinmemberthanasis_rassp4:16 12 Aug '08  
GeneralRe: Balloon Tip?? Pinmembervarandas796:25 24 Sep '08  
GeneralBug in CTrayDialog::OnSysCommand preventing sizing PinmemberEricGen0:02 25 Mar '08  
GeneralSystem get slow... PinmemberMember 385006722:52 28 Feb '08  
Generalsmall but useful demo Pinmemberlilesh23:33 10 Oct '07  
GeneralHow can I get my app on the tray at the very beginning? Pinmemberpjvv18:31 26 Sep '07  
GeneralSolution Pinmemberlilesh0:01 11 Oct '07  
Questionsystray without window Pinmemberchergui2:36 3 Apr '07  
GeneralIn VC++ 2003, there's error Pinmembertlbf3:46 10 Jan '07  
GeneralRe: In VC++ 2003, there's error Pinmemberchild00718:32 28 Jan '08  
GeneralRe: In VC++ 2003, there's error PinmemberJerry Peterson4:15 14 May '09  
Generaltray disappear after mouse over PinmembergeoPsyc14:05 4 Dec '06  
GeneralRe: tray disappear after mouse over PinmembergeoPsyc14:24 4 Dec '06  
GeneralRe: tray disappear after mouse over Pinmemberlilesh0:16 11 Oct '07  
QuestionRe: tray disappear after mouse over PinmemberAmbiguities18:53 23 Oct '07  
Generalcan you help me? about tray icon PinmemberDavid121518:08 19 Jun '06  
GeneralCompiling on VIsual C++ 7.0 PinmemberSychad8:34 19 Apr '06  
GeneralRe: Compiling on VIsual C++ 7.0 Pinmemberchild00718:31 28 Jan '08  
GeneralDisplaying the menu in the icon tray problem Pinmemberchris1759:01 14 Mar '06  
GeneralRe: Displaying the menu in the icon tray problem PinmemberDean Michaud5:30 24 Mar '06  

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

PermaLink | Privacy | Terms of Use
Last Updated: 6 Nov 2000
Editor: James Spibey
Copyright 2000 by Daniel Zilcsak
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project