Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Adding Icons to the System Tray

, 2 Aug 2003 CPOL
A class for adding icons to the system tray
hatrsystemtray.zip
systemtraysdk_demo.zip
SystemTraySDK_demo
Release
TaskbarDemo.exe
res
icon1.ico
icon2.ico
icon3.ico
icon4.ico
TaskbarDemo.ICO
TaskbarDemo.dsp
TaskbarDemo.dsw
systemtray_demo.zip
systemtray_demo
Release
SystemTrayDemo.exe
RES
Cmdbar.bmp
icon1.ico
icon2.ico
icon3.ico
icon4.ico
SystemTrayDemo.ico
SystemTrayDemoDoc.ico
Toolbar.bmp
SystemTrayDemo.dsp
SystemTrayDemo.dsw
SystemTrayDemoCE.dsp
SystemTrayDemoCE.dsw
systemtray_src.zip
#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);
        dc.Attach(hDc);

        // 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
        dc.SelectObject(old);

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

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

        // return the width and height
        lpMi->itemWidth = size.cx;
        lpMi->itemHeight = size.cy;
    }
} // 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
        ::DeleteObject(bgb);

        // 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);
        ::DeleteObject(font);

        // 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.

License

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

Share

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.
Follow on   Twitter   Google+   LinkedIn

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