Click here to Skip to main content
11,790,021 members (49,688 online)
Click here to Skip to main content

System Tray Icons - Adding to your dialog application

, 15 Apr 2002 186.1K 2.2K 53
Rate this:
Please Sign up or sign in to vote.
Easy to implement system tray icon!

Sample Image - systray.jpg


I write applications for my site and also to make things easier for myself at home. In one of my apps, I have wanted to include a system tray icon. Only problem being, the sys tray icon samples people wrote were confusing and didn't do what I want. I'm young and not exactly pro at programming yet. This tray icon code is different simply because upon starting the program, the icon is placed into the system tray. When you click hide, the whole program is minimized to tray.

Right, now down the code.

Let's do this!

Okay, on the main dialog you must make a button for the hiding command. Name this button IDC_HIDEAPP.

Add files traynot.cpp and traynot.h to your project.

Paste this into yourDlg.cpp:

void CYourDlg::OnHide()
  // Load icon onto taskbar tray
  m_pTray = new CTrayNot (this,WM_TRAY_NOTIFY,
  m_bHidden = TRUE;

void CYourDlg::OnUnHide()
  ShowWindow (SW_RESTORE) ;
  m_bHidden = FALSE ;
  // Remove icon from taskbar tray
  if (m_pTray)
     delete m_pTray ;
     m_pTray = NULL ;

LONG CYourDlg::OnTrayNotify ( WPARAM wParam, LPARAM lParam )
  switch (lParam)
       CMenu menu ;
       // Load and Verify Menu
       CMenu* pPopup = menu.GetSubMenu (0) ;
       ASSERT(pPopup != NULL);

       // Get the cursor position
       POINT pt ;
       GetCursorPos (&pt) ;

       // Fix Microsofts' BUG!!!!

       // Display The Menu
       pPopup->TrackPopupMenu(TPM_LEFTALIGN |
       TPM_RIGHTBUTTON,pt.x, pt.y, AfxGetMainWnd());
       break ;
       // Unhide our Window
       if (m_bHidden)
          ShowWindow (SW_RESTORE);
       //OnUnHide() ;
       break ;
  return (0) ;

void CYourDlg::OnDestroy() 

  // Remove Icon from Tray
  if (m_pTray)
    delete m_pTray ;
    m_pTray = NULL ;

void CYourDlg::OnTrayRestore() 
  // UnHide Application
  if (m_bHidden)
    ShowWindow (SW_RESTORE) ;
  m_bHidden = FALSE ;

void CYourDlg::OnHideapp() 
  //This will be the onclick for the hide button
  //in order to call that the app is minimised.

Add these to yourDlgs message map:


Add #include "TrayNot.h" to yourDlg.h. Add:

CTrayNot* m_pTray;
BOOL m_bHidden;

to your public call in yourDlg.h before yourDlgs standard constructor.

Add the following to your dialog's afx message maps.

afx_msg LONG OnTrayNotify ( WPARAM wParam, LPARAM lParam ) ;
afx_msg void OnTrayRestore();
afx_msg void OnHideapp();
afx_msg void OnHide();
afx_msg void OnUnHide();
afx_msg void OnDestroy();

In yourapp.cpp (Not yourdlg.cpp) add this just after #endif:

// Load Icons for Tray 

In the yourapp.h (not yourdlg.h) add this to the public callback:

HICON m_pIconList[3];

After the last } down the bottom of this file, add this:

extern CYourApp theApp;

Open stdafx.h and add #define WM_TRAY_NOTIFY WM_APP+1000 after AfxCmn.h's include.

Now just add a menu using the resource add and name it IDR_TRAY. Add a restore command (ID_TRAY_RESTORE) and then you're done....

You may find that when you quit, the icon doesn't disappear. Use the destroy function and add delete m_pTray ; to it... Or you could just download my demo project and rip all the code from that?

Anyways, I hope this helped some of you. Vote for this, so I get a T-Shirt haha!


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

Web Developer
Australia Australia
Name: Ash Rowe
From: Canberra, Australia
Qualifications: Diploma in Game Programming
Age: 19 (Getting old and feeling it)

Tutorials I have written in the past, I have done so in a fashion that would enable even the newest of programmers to get a grasp on what is actually happening with the code I submit. I am looking at writing even more in the near future to further assist others as well as gain a better understanding myself of the code I end up with.

You may also be interested in...

Comments and Discussions

Generalnice Pin
Donsw28-Jun-09 14:37
memberDonsw28-Jun-09 14:37 
GeneralCorrection to make the icons delete correctly from the system tray. Pin
shazzababs19-Dec-08 0:53
membershazzababs19-Dec-08 0:53 
GeneralIcons in System Tray not loaded when my application is run as service Pin
JLEExtdrs16-Oct-08 23:25
memberJLEExtdrs16-Oct-08 23:25 
GeneralSimple and Good one Pin
Yogesh Dhongade22-Aug-07 4:32
memberYogesh Dhongade22-Aug-07 4:32 
QuestionPopup menu by Right-click overlaps with Taskbar menus Pin
klee77721-Nov-05 4:35
memberklee77721-Nov-05 4:35 
AnswerRe: Popup menu by Right-click overlaps with Taskbar menus Pin
klee77721-Nov-05 5:07
memberklee77721-Nov-05 5:07 
AnswerSame problem here Pin
Bruno Scopinho3-Nov-06 2:18
memberBruno Scopinho3-Nov-06 2:18 
AnswerRe: Popup menu by Right-click overlaps with Taskbar menus Pin
ChrisHarlow19-Dec-06 1:34
memberChrisHarlow19-Dec-06 1:34 
I managed to avoid this problem by handling the WM_RBUTTONUP message instead of the WM_RBUTTONDOWN
GeneralGREAT code - two minor improvements Pin
Douglas R. Keesler13-May-05 15:19
memberDouglas R. Keesler13-May-05 15:19 
Generaltwo problems... Pin
smarty907116-Feb-05 20:51
susssmarty907116-Feb-05 20:51 
GeneralRe: two problems... Pin
Ashman17-Feb-05 1:47
memberAshman17-Feb-05 1:47 
GeneralRe: two problems... Pin
smarty907121-Feb-05 16:15
susssmarty907121-Feb-05 16:15 
GeneralRe: two problems... Pin
Ashman21-Feb-05 18:22
memberAshman21-Feb-05 18:22 
GeneralRe: two problems... Pin
smarty907124-Feb-05 4:07
membersmarty907124-Feb-05 4:07 
Questionhow to start a dialog app hidden Pin
k-nar13-Feb-05 5:30
memberk-nar13-Feb-05 5:30 
AnswerRe: how to start a dialog app hidden Pin
smarty907116-Feb-05 20:56
susssmarty907116-Feb-05 20:56 
GeneralRe: how to start a dialog app hidden Pin
Leo Huf2-May-06 11:39
memberLeo Huf2-May-06 11:39 
QuestionIs it has a bug? Pin
smallbarrow4-Aug-04 14:39
membersmallbarrow4-Aug-04 14:39 
GeneralAWESOME Pin
shultas6-Mar-04 12:48
membershultas6-Mar-04 12:48 
GeneralAfxGetApp()->HideApplication(); when initialising main wnd Pin
IndrekSnt22-Dec-03 1:59
memberIndrekSnt22-Dec-03 1:59 
GeneralRe: AfxGetApp()->HideApplication(); when initialising main wnd Pin
Ashman6-Feb-04 20:10
memberAshman6-Feb-04 20:10 
GeneralGreat Work!! But Log in problem... Pin
Rohit Divas20-Sep-03 18:19
memberRohit Divas20-Sep-03 18:19 
GeneralRe: Great Work!! But Log in problem... Pin
Ashman6-Feb-04 20:13
memberAshman6-Feb-04 20:13 
GeneralRe: Great Work!! But Log in problem... Pin
Rohit Divas6-Feb-04 20:37
memberRohit Divas6-Feb-04 20:37 
GeneralSmall Fixes & Improvements to the code.. Pin
OhmegaStar31-Aug-03 1:09
memberOhmegaStar31-Aug-03 1:09 
GeneralRe: Small Fixes & Improvements to the code.. Pin
OhmegaStar31-Aug-03 2:16
memberOhmegaStar31-Aug-03 2:16 
GeneralTrayNot Pin
BlueWhite31-May-03 1:16
sussBlueWhite31-May-03 1:16 
Generalstart minimized Pin
iffi99228-May-03 23:44
memberiffi99228-May-03 23:44 
GeneralRe: start minimized Pin
Ashman29-May-03 15:28
memberAshman29-May-03 15:28 
GeneralRe: start minimized Pin
Member 25243835-Jun-08 4:59
memberMember 25243835-Jun-08 4:59 
GeneralFix: Remove icon on leaving application Pin
ScorpioMidget19-May-03 3:18
memberScorpioMidget19-May-03 3:18 
Generalapp lost Pin
hiho21-Mar-03 18:20
susshiho21-Mar-03 18:20 
GeneralRe: app lost Pin
Ashman29-May-03 15:34
memberAshman29-May-03 15:34 
AnswerRe: app lost Pin
shazzababs9-Aug-07 0:14
membershazzababs9-Aug-07 0:14 
GeneralRe: app lost Pin
Member 56468673-Nov-08 22:31
memberMember 56468673-Nov-08 22:31 
GeneralGreat trick that fixes lingering icon on tray Pin
hoc9610-Dec-02 17:18
memberhoc9610-Dec-02 17:18 
GeneralWell done! Pin
Anonymous26-Aug-02 4:49
sussAnonymous26-Aug-02 4:49 
GeneralSDI Pin
termi15-Apr-02 23:57
membertermi15-Apr-02 23:57 
GeneralJust what I was looking for! :) Pin
Selevercin12-Apr-02 15:23
memberSelevercin12-Apr-02 15:23 
GeneralIt does not work properly Pin
Paresh3-Feb-02 19:07
memberParesh3-Feb-02 19:07 
GeneralRe: It does not work properly Pin
Anil Kumar T27-Feb-02 22:01
memberAnil Kumar T27-Feb-02 22:01 
GeneralRe: It does not work properly Pin
MS28-Feb-02 4:40
memberMS28-Feb-02 4:40 
GeneralHi Ashman Pin
Thomas Freudenberg3-Dec-01 16:06
memberThomas Freudenberg3-Dec-01 16:06 
GeneralRe: Hi Ashman Pin
Ashman3-Dec-01 19:49
memberAshman3-Dec-01 19:49 
GeneralRe: Hi Ashman Pin
swinefeaster10-Dec-01 14:42
memberswinefeaster10-Dec-01 14:42 
GeneralRe: Hi Ashman Pin
Yeepeey!11-Dec-01 16:59
memberYeepeey!11-Dec-01 16:59 
GeneralRe: Hi Ashman Pin
Jean-Marc Molina4-Mar-02 23:48
memberJean-Marc Molina4-Mar-02 23:48 
GeneralRe: Hi Ashman Pin
Jamesj3k13-Mar-02 8:52
memberJamesj3k13-Mar-02 8:52 
GeneralRe: Hi Ashman Pin
Thomas Freudenberg14-Mar-02 3:22
memberThomas Freudenberg14-Mar-02 3:22 
GeneralRe: Hi Ashman Pin
xufeisjtu8-Dec-02 4:54
memberxufeisjtu8-Dec-02 4:54 

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
Web04 | 2.8.1509028.1 | Last Updated 16 Apr 2002
Article Copyright 2001 by Ashman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid