Click here to Skip to main content
13,452,088 members (50,094 online)
Click here to Skip to main content


495 bookmarked
Posted 29 Nov 1999

Adding Icons to the System Tray

, 2 Aug 2003
A class for adding icons to the system tray
#include "stdafx.h"
#include "TitledPopupMenu.h"

void AddMenuTitle(CMenu* popup, LPCTSTR lpszTitle)
    // insert a separator item at the top
    popup->InsertMenu(0, MF_BYPOSITION | MF_SEPARATOR, 0, (LPSTR)0);

    // insert an empty owner-draw item at top to serve as the title
    // note: item is not selectable (disabled) but not grayed
    popup->InsertMenu(0, MF_BYPOSITION | MF_OWNERDRAW | MF_STRING | MF_DISABLED, 0, lpszTitle);
} // AddMenuTitle(CMenu*, LPCSTR)

static HFONT CreatePopupMenuTitleFont()
    // start by getting the stock menu font
    HFONT font = (HFONT)::GetStockObject(ANSI_VAR_FONT);
    if (font)
        // now, get the complete LOGFONT describing this font
        LOGFONT lf;
        if (::GetObject(font, sizeof(LOGFONT), &lf))
            // set the weight to bold
            lf.lfWeight = FW_BOLD;

            // recreate this font with just the weight changed
            font = ::CreateFontIndirect(&lf);

    // return the new font - Note: Caller now owns this GDI object
    return font;
} // static HFONT CreatePopupMenuTitleFont()

void MeasurePopupMenuTitle(LPMEASUREITEMSTRUCT lpMi)
    LPCSTR lpszMenuTitle = (LPCSTR)lpMi->itemData;

    // create the font we will use for the title
    HFONT font = CreatePopupMenuTitleFont();
    if (font)
        // get the screen dc to use for retrieving size information
        CDC dc;
        HDC hDc = ::GetDC(NULL);

        // select the title font
        CFont* old = (CFont*)dc.SelectObject(CFont::FromHandle(font));

        // compute the size of the title
        CSize size = dc.GetTextExtent(lpszMenuTitle);
        // deselect the title font

        // delete the title font
        ::ReleaseDC(NULL, hDc);

        // add in the left margin for the menu item += GetSystemMetrics(SM_CXMENUCHECK)+8;

        // return the width and height
        lpMi->itemWidth =;
        lpMi->itemHeight =;
} // MeasurePopupMenuTitle(LPMEASUREITEMSTRUCT)

// This is a helper function to draw the popup menu's title item
void DrawPopupMenuTitle(LPDRAWITEMSTRUCT lpDi)
    LPCSTR lpszMenuTitle = (LPCSTR)lpDi->itemData;

    // create the font we will use for the title
    HFONT font = CreatePopupMenuTitleFont();
    if (font)
        // create the background menu brush
        HBRUSH bgb = CreateSolidBrush(GetSysColor(COLOR_MENU));

        // fill the rectangle with this brush
        FillRect(lpDi->hDC, &lpDi->rcItem, bgb);

        // delete the brush

        // set text mode to transparent
        int mode = SetBkMode(lpDi->hDC, TRANSPARENT);

        // set text color to menu text color
        COLORREF color = SetTextColor(lpDi->hDC, GetSysColor(COLOR_MENUTEXT));

        // select this font into the dc
        HFONT old = (HFONT)SelectObject(lpDi->hDC, font);

        // add the menu margin offset
        lpDi->rcItem.left += GetSystemMetrics(SM_CXMENUCHECK)+8;

        // draw the text left aligned and vertically centered
        DrawText(lpDi->hDC, lpszMenuTitle, -1, &lpDi->rcItem, DT_SINGLELINE|DT_VCENTER|DT_LEFT);

        // deselect and delete the font 
        SelectObject(lpDi->hDC, old);

        // restore the text background mode
        SetBkMode(lpDi->hDC, mode);

        // restore the text color
        SetTextColor(lpDi->hDC, color);
} // void DrawPopupMenuTitle()

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Chris Maunder
Founder CodeProject
Canada Canada
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.

His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.

He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.

Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180318.3 | Last Updated 3 Aug 2003
Article Copyright 1999 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid