Click here to Skip to main content
11,500,413 members (68,376 online)
Click here to Skip to main content

System Tray Icons - Minimize Your Application to the SysTray

, 6 Nov 2000 CPOL 418.9K 13K 142
Rate this:
Please Sign up or sign in to vote.
Minimize your application to system tray instead of the taskbar
<!-- Link to source file download --> <!-- Article image -->

Sample Image - TrayIcons.jpg

<!-- Add the rest of your HTML here -->

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)

Share

About the Author

Daniel Zilcsak
Software Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralThankYou !! Modification !! Pin
harpreetsbamrah9-Jun-05 22:58
memberharpreetsbamrah9-Jun-05 22:58 
GeneralMenu will not disapper Pin
_bko22-May-05 23:43
member_bko22-May-05 23:43 
JokeRe: Menu will not disapper Pin
Harry Lau6-Apr-06 4:33
memberHarry Lau6-Apr-06 4:33 
GeneralRe: Menu will not disapper Pin
fherlan2-Dec-06 5:42
memberfherlan2-Dec-06 5:42 
GeneralThanks Daniel ! Pin
ganesh_nv@yahoo.com18-Apr-05 19:53
memberganesh_nv@yahoo.com18-Apr-05 19:53 
GeneralSome bugs Pin
rrrado7-Mar-05 0:43
memberrrrado7-Mar-05 0:43 
It seems that when I've started to use this class, 2 bugs occured in my application:
1. the menu is shown by gray color when app starts (it does not look like disabled menu, text does not have disabled shadow effect), but when I move the window or force it to redraw the menu by other way it's drawn properly
(the bug fix posted below didn't help).

2. main dialog contains tab control which contains child dialog with edit control. When the cursor is placed in the text, I switch to another application and then back to my application, but edit box lose the focus and cursor position is not remebered.
How to solve this ?

thanks


rrrado
GeneralRe: Some bugs Pin
rrrado7-Mar-05 2:26
memberrrrado7-Mar-05 2:26 
GeneralGreat job -- small improvement Pin
Hard Hat23-Feb-05 1:21
memberHard Hat23-Feb-05 1:21 
GeneralTerminating from TaskManager Pin
Alan Buchanan23-Feb-05 0:02
memberAlan Buchanan23-Feb-05 0:02 
GeneralRe: Terminating from TaskManager Pin
Anonymous23-Feb-05 14:36
sussAnonymous23-Feb-05 14:36 
GeneralRe: Terminating from TaskManager Pin
Anonymous18-Apr-05 20:45
sussAnonymous18-Apr-05 20:45 
GeneralRe: Terminating from TaskManager Pin
TaLF25-Jun-05 13:17
memberTaLF25-Jun-05 13:17 
GeneralRe: Solution - Terminating from TaskManager Pin
IVarshilov24-May-09 22:53
memberIVarshilov24-May-09 22:53 
GeneralStart Dialog Minimized to SysTray Pin
codehar13-Feb-05 19:04
membercodehar13-Feb-05 19:04 
GeneralRe: Start Dialog Minimized to SysTray Pin
mysticwolf9-Mar-05 6:18
membermysticwolf9-Mar-05 6:18 
GeneralRe: Start Dialog Minimized to SysTray Pin
bubis14-Sep-05 2:57
memberbubis14-Sep-05 2:57 
AnswerRe: Start Dialog Minimized to SysTray Pin
CodeWatcher27-Oct-05 13:28
memberCodeWatcher27-Oct-05 13:28 
GeneralRe: Start Dialog Minimized to SysTray Pin
binyo6618-May-09 16:55
memberbinyo6618-May-09 16:55 
GeneralGreat Code - Read if using Checkboxes on Popup Menus Pin
slinsell21-Jan-05 22:49
memberslinsell21-Jan-05 22:49 
GeneralRe: Great Code - Read if using Checkboxes on Popup Menus Pin
Long Truong15-Jun-05 22:25
sussLong Truong15-Jun-05 22:25 
GeneralNice work Pin
Alex Farber26-Dec-04 1:47
memberAlex Farber26-Dec-04 1:47 
GeneralOne more problem Pin
Mich261-Dec-04 19:46
memberMich261-Dec-04 19:46 
GeneralRe: One more problem Pin
_bko22-May-05 22:23
member_bko22-May-05 22:23 
General&quot;but it can easily be modified to work with a CFrameWnd or CWnd based application&quot; Pin
d00_ape21-Nov-04 7:14
memberd00_ape21-Nov-04 7:14 
GeneralOpening another application Pin
Ritcho26-Sep-04 3:53
memberRitcho26-Sep-04 3:53 
GeneralRe: Opening another application Pin
AnonExerc29-Oct-04 0:23
memberAnonExerc29-Oct-04 0:23 
GeneralRe: Opening another application Pin
EpicBoy28-Jan-05 0:40
sussEpicBoy28-Jan-05 0:40 
GeneralRe: Opening another application Pin
EpicBoy28-Jan-05 0:47
memberEpicBoy28-Jan-05 0:47 
QuestionWhat's the problem???? Pin
casper23418-May-04 2:41
membercasper23418-May-04 2:41 
AnswerRe: What's the problem???? Pin
Hesterumpe20-May-04 0:25
memberHesterumpe20-May-04 0:25 
GeneralPLEASE HELP!!!! Pin
waszka2-May-04 5:41
memberwaszka2-May-04 5:41 
GeneralRe: PLEASE HELP!!!! Pin
KCUS29-Jun-04 8:17
memberKCUS29-Jun-04 8:17 
GeneralRe: PLEASE HELP!!!! Pin
KCUS29-Jun-04 16:29
memberKCUS29-Jun-04 16:29 
GeneralRe: PLEASE HELP!!!! Pin
ganesh_nv@yahoo.com18-Apr-05 19:56
memberganesh_nv@yahoo.com18-Apr-05 19:56 
QuestionBug?!? Pin
Bassam Abdul-Baki11-Mar-04 3:13
memberBassam Abdul-Baki11-Mar-04 3:13 
GeneralThank you Pin
chifon9-Mar-04 23:43
memberchifon9-Mar-04 23:43 
Generali can't take it anymore! Pin
Roey C5-Mar-04 0:46
memberRoey C5-Mar-04 0:46 
GeneralRe: i can't take it anymore! Pin
Daniel Turini5-Mar-04 1:20
memberDaniel Turini5-Mar-04 1:20 
GeneralRe: i can't take it anymore! Pin
Michael P Butler5-Mar-04 1:27
memberMichael P Butler5-Mar-04 1:27 
GeneralStrange access violation Pin
mmagnani7117-Jan-04 1:51
membermmagnani7117-Jan-04 1:51 
Questionwhat's wrong with this code? Pin
crias7-Jan-04 20:48
membercrias7-Jan-04 20:48 
GeneralGreat start but a couple bugs Pin
waxwing31-Dec-03 8:23
memberwaxwing31-Dec-03 8:23 
Generalplease Help Pin
smabed20-Dec-03 15:35
membersmabed20-Dec-03 15:35 
GeneralRe: please Help Pin
Eyes`Only23-Mar-04 17:33
memberEyes`Only23-Mar-04 17:33 
GeneralCool code Pin
John Simmons / outlaw programmer27-Nov-03 0:37
memberJohn Simmons / outlaw programmer27-Nov-03 0:37 
GeneralExcellent! - Easy to adapt for CDHtmlDialog Pin
Jim Howard27-Oct-03 6:12
memberJim Howard27-Oct-03 6:12 
GeneralExcellent! One bug... Pin
alexblahblah18-Oct-03 13:38
sussalexblahblah18-Oct-03 13:38 
QuestionHelp Please?? Pin
xxhimanshu30-Sep-03 18:40
memberxxhimanshu30-Sep-03 18:40 
QuestionHow can I change the default button Pin
Geo.23-Jul-03 13:18
memberGeo.23-Jul-03 13:18 
GeneralMenu stays if you make no choice Pin
hneel21-Jun-03 3:18
memberhneel21-Jun-03 3:18 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 7 Nov 2000
Article Copyright 2000 by Daniel Zilcsak
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid