Click here to Skip to main content
Click here to Skip to main content

SDI/MDI MFC application in the Windows System Tray

By , 11 Mar 2003
Rate this:
Please Sign up or sign in to vote.


This article describes the process of creating an application that is startup-hidden and is opened when clicking on the system tray icon. The SDI VC++ v6.0 demo project is attached. The project demonstrates the use of a hidden window, a system tray icon and a startup flipping prevention technique.


The right part of the Windows Task Bar is the system tray. It includes icons of hidden applications such as the system clock dialog, the sound volume dialog, some antivirus programs etc.

I created a SDI MFC project to demonstrate the technique of creating such an application. Why SDI? I needed an application with a graphic output (see link). Dialog based applications could be created the same way. It is easier because dialog applications don't have the flipping problem that is described below.

Icon control

Do you want to put in the system tray an icon of your program? No problem. SDK Shell_NotifyIcon function lets you put, remove or change an icon in the system tray. I created a method CMainFrame::TrayMessage( DWORD dwMessage) for convenient use of this function. The dwMessage parameter defines what we need to do with the system tray icon.

BOOL CMainFrame::TrayMessage( DWORD dwMessage)

    CString sTip(_T("BrigSoft Example")); 
    tnd.cbSize = sizeof(NOTIFYICONDATA);
    tnd.hWnd = m_hWnd;
    tnd.uID = IDR_TRAYICON;
    tnd.uFlags = NIF_MESSAGE|NIF_ICON;
    tnd.uCallbackMessage = MYWM_NOTIFYICON;
        tnd.hIcon = LoadIcon(AfxGetInstanceHandle(), 
                        MAKEINTRESOURCE (IDR_TRAYICON)) 
    lstrcpyn(tnd.szTip, (LPCTSTR)sTip, sizeof(tnd.szTip));

    return Shell_NotifyIcon(dwMessage, &tnd);


I call TrayMessage(NIM_ADD) from CMainFrame::OnCreate. It shows an icon after the program is started. Calling TrayMessage( NIM_DELETE ) from CMainFrame::OnClose removes the icon when application is finished.

You can easily change this function to use the NIM_MODIFY message, if you want to change the icon or the tooltip.

Show and hide the main window

Shell_NotifyIcon function receives NOTIFYICONDATA structure. It includes a handle of a window and a window message number. If some event takes place on the icon, the system tray sends the message to the window.

I defined user message MYWM_NOTIFYICON for this purpose.


To receive this message I overloaded the virtual function WindowProc (using MFC Wizard).

LRESULT CMainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
    // Open window when double click to the Systray Icon
    if(message == MYWM_NOTIFYICON){ 
        switch (lParam){

            case WM_LBUTTONDBLCLK: 
                switch (wParam) {

                    case IDR_TRAYICON:

                        return TRUE; 
    return CFrameWnd::WindowProc(message, wParam, lParam);


I catch only the WM_LBUTTONDBLCLK message. Other mouse messages can be caught too. After the WM_LBUTTONDBLCLK occur, I open the window using ShowWindow(SW_NORMAL).

Most of the system tray programs hide their main window instead of minimizing it. To do so I use OnSize message handle in such a way:

void CMainFrame::OnSize(UINT nType, int cx, int cy) 
     if(nType == SIZE_MINIMIZED){
          CFrameWnd::OnSize(nType, cx, cy); 

Hide on start

MFC SDI/MDI projects show the main applications window immediately after the start. If you want to start an application in a hidden mode, you must change InitInstanse() function of the App class. Change the strings:




The application will start as hidden but you will see a flipping window. It opens and immediately closes. The problem occurs in Microsoft's CDocTemplate class. There is only one solution: to overload CSingleDocTemplate (CMultiDocTemplate) class. I created BsDocTemplate class (child of CSingleDocTemplate) to fix this problem. Microsoft can solve this problem easier:

by changing

virtual CDocument* OpenDocumentFile(LPCTSTR lpszPathName, 
                                   BOOL bMakeVisible = TRUE);


virtual CDocument* OpenDocumentFile(LPCTSTR lpszPathName, 
                                   BOOL bMakeVisible = FALSE);

in the CDocTemplate class definition. But they have not fixed it yet.

Demo project

MFCStartUp demo project is a SDI MFC application. It hides while you start it. So you will see no window after the start, only a small green icon in the system tray. To open the application, double click this icon. To hide the application, click the minimize button of the main window.


The technique described in this article is used in the project Activity Counter.


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


Ukraine Ukraine
No Biography provided

Comments and Discussions

GeneralThis is tray for SDI/MDI but i want for dailog Pinmemberashu_om19-Apr-07 0:22 
Generalhide the icon of another process than the mine PinsussNewMan45646412-May-05 20:35 
Questionhow to get the handle of icons displayed in system tray Pinmembernarasimhavarmap12-Apr-05 19:54 
GeneralHelp me Pinmembersionie5-May-04 7:10 
GeneralHIDE ON START, BUT DIALOG BASED MFC APP Pinmemberandresfecas3-Apr-04 3:49 
GeneralRe: HIDE ON START, BUT DIALOG BASED MFC APP PinmemberBrigsoft3-Apr-04 21:15 
GeneralRe: HIDE ON START, BUT DIALOG BASED MFC APP Pinsussmatasko7-Nov-04 8:01 
GeneralRe: HIDE ON START, BUT DIALOG BASED MFC APP PinmembermaddyMathan21-Nov-06 17:12 
GeneralRe: HIDE ON START, BUT DIALOG BASED MFC APP PinmembermaddyMathan21-Nov-06 23:43 
AnswerRe: HIDE ON START, BUT DIALOG BASED MFC APP PinmembershritiParashars4-Jan-07 20:01 
AnswerRe: HIDE ON START, BUT DIALOG BASED MFC APP PinmembershritiParashars4-Jan-07 20:12 
GeneralThis may interest you.... Pinmemberbrucetp14-Jan-04 5:03 
GeneralOnInitialUpdate Pinmemberdeafpat13-Jan-04 21:35 
GeneralRe: OnInitialUpdate PinmemberHawks20-Nov-04 1:26 
GeneralDialog Size PinmemberMealMate13-Oct-03 17:01 
Generalhelp please Pinmemberxxhimanshu30-Sep-03 18:41 
GeneralRe: help please PinmemberRickyC2-Oct-03 4:12 
Questionvery nice; what about a contextmenu? PinmemberIvanhoeJugger18-Mar-03 21:00 

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 | Mobile
Web02 | 2.8.140415.2 | Last Updated 12 Mar 2003
Article Copyright 2003 by Brigsoft
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid