65.9K
CodeProject is changing. Read more.
Home

Badges for Windows 7

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Mar 9, 2012

CPOL
viewsIcon

12693

downloadIcon

494

Short description how to create a sexy badge for windows 7 taskbar

Introduction 

How to use ITaskbarList3 in C++ to add overlay icon into taskbar. 

Using the code

Before using ITaskbarList3, we must receive TaskbarButtonCreated message from system. First we must register this message.

s_uTBBC = RegisterWindowMessage(L"TaskbarButtonCreated");

In our message loop we can catch this message and call our function:

if( message == s_uTBBC ) {
    onTaskbarButtonCreated(wParam, lParam);
}

Create instance of ITaskbarList3 and set the overlay icon.

static LRESULT onTaskbarButtonCreated(WPARAM wParam, LPARAM lParam) {

    HRESULT result = CoCreateInstance(CLSID_TaskbarList, NULL, 
                        CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&m_pTaskBarlist));

    if (SUCCEEDED(result)) { 
        result = m_pTaskBarlist->HrInit(); 
        if (SUCCEEDED(result)) {

            HICON icon = createBadgeIcon(_T("78"));
            m_pTaskBarlist->SetOverlayIcon(m_hWnd, icon, _T("Status"));
            DeleteObject(icon);

        }
    }

    return 0;
}

I use GDI+ for creating badge icon. For create cool small ellipse I use circle (letter 'l') from Wingdings font :)

static HICON createBadgeIcon(const TCHAR *value) {

    Color bagdeColor = Color(233, 0, 0);
    Color textColor = Color(244, 244, 244);
//
    Bitmap *bitmap = new Bitmap(24, 24, PixelFormat32bppARGB);
    m_Graphics = Graphics::FromImage(bitmap);
//
    StringFormat format(StringFormatFlagsNoWrap);
    format.SetAlignment(StringAlignmentCenter); 
    format.SetLineAlignment(StringAlignmentCenter);
//
    m_Graphics->DrawString(_T("l"), 1, 
                            new Font(_T("Wingdings"), 24), 
                            RectF(1, 2, 24, 24), 
                            &format, 
                            new SolidBrush(bagdeColor));
    m_Graphics->DrawString(value, wcslen(value), 
                            new Font(_T("Calibri"), 8), 
                            RectF(1, 2, 24, 20), 
                            &format, new SolidBrush(textColor));
//
    HICON icon;
    bitmap->GetHICON(&icon);
    // Cleanup
    delete bitmap;
    return icon;
}

History

Version 1.0.