5,427,303 members and growing! (15,533 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
VC6, C++Windows, NT4, Win2KVS6, Visual Studio, Dev

Posted: 6 Nov 2000
Updated: 6 Nov 2000
Views: 232,341
Bookmarked: 100 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
94 votes for this Article.
Popularity: 8.88 Rating: 4.50 out of 5
2 votes, 3.0%
1
2 votes, 3.0%
2
3 votes, 4.5%
3
16 votes, 24.2%
4
43 votes, 65.2%
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



Occupation: Web Developer
Location: United States United States

Other popular Shell and IE programming articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 117 (Total in Forum: 117) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralBalloon Tip??memberthanasis_rassp4:16 12 Aug '08  
GeneralBug in CTrayDialog::OnSysCommand preventing sizingmemberEricGen0:02 25 Mar '08  
GeneralSystem get slow...memberMember 385006722:52 28 Feb '08  
Generalsmall but useful demomemberlilesh23:33 10 Oct '07  
GeneralHow can I get my app on the tray at the very beginning?memberpjvv18:31 26 Sep '07  
GeneralSolutionmemberlilesh0:01 11 Oct '07  
Questionsystray without windowmemberchergui2:36 3 Apr '07  
GeneralIn VC++ 2003, there's errormembertlbf3:46 10 Jan '07  
GeneralRe: In VC++ 2003, there's errormemberchild00718:32 28 Jan '08  
Generaltray disappear after mouse overmembergeoPsyc14:05 4 Dec '06  
GeneralRe: tray disappear after mouse overmembergeoPsyc14:24 4 Dec '06  
GeneralRe: tray disappear after mouse overmemberlilesh0:16 11 Oct '07  
QuestionRe: tray disappear after mouse overmemberAmbiguities18:53 23 Oct '07  
Generalcan you help me? about tray iconmemberDavid121518:08 19 Jun '06  
GeneralCompiling on VIsual C++ 7.0memberSychad8:34 19 Apr '06  
GeneralRe: Compiling on VIsual C++ 7.0memberchild00718:31 28 Jan '08  
GeneralDisplaying the menu in the icon tray problemmemberchris1759:01 14 Mar '06  
GeneralRe: Displaying the menu in the icon tray problemmemberDean Michaud5:30 24 Mar '06  
GeneralItem1=ShowWindow?memberAm0k5:12 30 Jan '06  
GeneralIssue With CEmemberoilos2:23 16 Aug '05  
GeneralProblemmemberharpreetsbamrah21:24 23 Jun '05  
GeneralRe: Problemmemberharpreetsbamrah21:44 23 Jun '05  
Generalsorrymembersicks7:42 18 Jun '05  
Generalproblemmembersicks7:29 18 Jun '05  
GeneralRe: problemmemberAleJ6:36 15 Jun '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-2008
Web17 | Advertise on the Code Project