Click here to Skip to main content
13,447,264 members (37,858 online)
Click here to Skip to main content
Add your own
alternative version


54 bookmarked
Posted 2 Dec 2001

System Tray Icons - Adding to your dialog application

, 15 Apr 2002
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

GeneralSmall Fixes & Improvements to the code.. Pin
OhmegaStar31-Aug-03 1:09
memberOhmegaStar31-Aug-03 1:09 
Hi Ashley,

Thanks for a good tutorial.. There are some small additions/corrections to pieces of the code that would make it more usefull for future readers..

1) SetState... Shell_Notify Works Ok, but change like this an we will not refresh unless the icon is changing..
void CTrayNot::SetState(int id)
if (!m_bEnabled)
if (m_tnd.hIcon != m_pIconList[id]) //OHM: 20030831 Only if The Icon is Changing
m_tnd.hIcon = m_pIconList[id];
Shell_NotifyIcon(NIM_MODIFY, &m_tnd);

2) New function (declare as appropriate..)
void CTrayNot::SetTip(LPCTSTR szTip)
if (m_bEnabled) //OHM: 20030831 Tray is Active
strcpy ( m_tnd.szTip, szTip); //OHM: 20030831 Tool Tip Text

3) Calling SetState should not be like this;
m_pTray->SetState(IDR_MAINFRAME); //IDR_MAINFRAME is defined like 128 in integer..
//But rather Like This
m_pTray->SetState(0); //Consistent with the first IDR_MAINFRAME resource loaded in position 0... (will also fix the dissapearing icon in SetState)..

Beyond That I think its Great..

Finally you may want to override WM_WINDOWPOSCHANGING Message(OnWindowPosChanging(WINDOWPOS* lpwndpos);) in your dialog like this;
void CYourDialogClass::OnWindowPosChanging(WINDOWPOS* lpwndpos)
if(!bVisible) //OHM: If Dialog is Hidden
lpwndpos->flags &= ~SWP_SHOWWINDOW;
Remember to Set;
bVisible = false; //OHM: Used to Control if Dialog is Visible
in the Dialogs Constructor..

Now Call Like this to show / Hide the Dialog;
bVisible = true;
bVisible = false;

Best Regards,

Henrik Ohm
GeneralRe: Small Fixes & Improvements to the code.. Pin
OhmegaStar31-Aug-03 2:16
memberOhmegaStar31-Aug-03 2:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web03 | 2.8.180318.3 | Last Updated 16 Apr 2002
Article Copyright 2001 by Ashman
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid