Click here to Skip to main content
15,896,557 members
Articles / Desktop Programming / ATL

Shell Extensibility - Explorer Desk Band, Tray Notification Icon et al.

Rate me:
Please Sign up or sign in to vote.
4.97/5 (21 votes)
28 Aug 2009CPOL12 min read 139.4K   3.2K   71  
A simple Calendar utility that demonstrates basic Shell extensibility techniques: desk band, tray notification icon, locales.
////////////////////////////////////////////////////////////////////////////////
// 
#include "StdAfx.h"
#include "VisualStyle.h"
#include "Utils.h"

////////////////////////////////////////////////////////////////////////////////
// Classic visual style

class CClassicVisualStyle : public CVisualStyle
{
public:
    CClassicVisualStyle()
    {
        CreateFont();
        CreateTextColor();
    }

    virtual void DrawBackground(HWND /*hWnd*/, HDC hdc, const RECT& rcPaint) const
    {
        ::FillRect(hdc, &rcPaint, ::GetSysColorBrush(CTLCOLOR_DLG));
    }

private:
    bool CreateFont()
    {
        NONCLIENTMETRICS ncm = { 0 };
        ncm.cbSize = sizeof(NONCLIENTMETRICS) -
            (::IsVistaOrHigher() ? 0 : sizeof(ncm.iPaddedBorderWidth));

        if(::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0))
            m_hFont = ::CreateFontIndirect(&ncm.lfMessageFont);

        ATLASSERT(m_hFont);

        return (m_hFont != NULL);
    }

    bool CreateTextColor()
    {
        m_clrText = ::GetSysColor(COLOR_MENUTEXT);

        return true;
    }
};

////////////////////////////////////////////////////////////////////////////////
// Themed visual style

class CThemedVisualStyle : public CVisualStyle
{
public:
    CThemedVisualStyle()
    {
        CreateFont();
        CreateTextColor();
    }

    virtual void DrawBackground(HWND hWnd, HDC hdc, const RECT& rcPaint) const
    {
        ::DrawThemeParentBackground(hWnd, hdc, &rcPaint);
    }

private:
    bool CreateFont()
    {
        HTHEME hTheme = ::OpenThemeData(NULL, VSCLASS_REBAR);
        ATLASSERT(hTheme);

        if(hTheme)
        {
            LOGFONT lf = { 0 };
            const HRESULT hr = ::GetThemeFont(hTheme,
                NULL, RP_BAND, 0, TMT_FONT, &lf);
            ATLASSERT(SUCCEEDED(hr));

            if(SUCCEEDED(hr))
            {
                m_hFont = ::CreateFontIndirect(&lf);
                ATLASSERT(m_hFont);
            }

            ::CloseThemeData(hTheme);
        }

        return (m_hFont != NULL);
    }

    bool CreateTextColor()
    {
        bool bRet = false;
        HTHEME hTheme = ::OpenThemeData(NULL, VSCLASS_TASKBAND);
        ATLASSERT(hTheme);

        if(hTheme)
        {
            const HRESULT hr = ::GetThemeColor(hTheme,
                TDP_GROUPCOUNT, 0, TMT_TEXTCOLOR, &m_clrText);
            ATLASSERT(SUCCEEDED(hr));

            ::CloseThemeData(hTheme);
            bRet = SUCCEEDED(hr);
        }

        return bRet;
    }
};


////////////////////////////////////////////////////////////////////////////////
// CVisualStyle

CVisualStyle::~CVisualStyle()
{
    if(m_hFont) ::DeleteObject(m_hFont);
}

// static
CVisualStyle* CVisualStyle::Create(VisualStyle vs)
{
    CVisualStyle* pVS = NULL;

    if(vs == Auto) vs = (::IsAppThemed() ? Themed : Classic);

    switch(vs)
    {
    case Classic:
        pVS = new CClassicVisualStyle;
        break;
    case Themed:
        pVS = new CThemedVisualStyle;
        break;
    default:
        ATLASSERT(FALSE);
    }

    return pVS;
}

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)


Written By
Software Developer
Australia Australia
More than ten years of C++ native development, and counting.

Smile | :)

Comments and Discussions