Click here to Skip to main content
13,251,605 members (71,096 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 1 Apr 2004

Some Mix-in classes that give your ATL/WTL program a better appearance

, 1 Apr 2004
Rate this:
Please Sign up or sign in to vote.
This article discuss some Mix-In classes for WTL. These Mix-In classes provide some features that WTL/ATL framework does not support, such as owner-draw menu, skin dialog's button and static text control.

Sample Image - maindlg.gif



I have been a MFC programmer for a long time, it is a very nice framework. But MFC also has some disadvantages especially on the plan and development for little memory-resident programs. For that kind of programs, we need the program has less size, occupies less resource and is without resource leak. Obviously, that's not the advantage of MFC, so I began to study WTL. Not like MFC, WTL has no built-in support for Owner-Drawn Menu. I had wrote the CMenuHelp class for the plain WTL window to skin the window's popup menu. CWzButtonImpl is a simple owner-drawn Button class, it co-operates with CButtonHelp which uses CWzButtonImpl to subclass all buttons in a dialog. CButtonHelp can also cooperate with other Owner-Drawn Button classes. CCtrlColor processes WM_CTLCOLORDLG and WM_CTLCOLORSTATIC messages of dialog window, uses the template parameter to change the background color of dialog and controls' text color.

Since I'm not an expert on WTL/ATL framework, I can't ensure the code can work for everyone. Any advice are welcomed. You may notice that this article has many syntax errors or is even total wrong sometimes, that's because I am not an English speaker. I hope everyone can understand this article.


As we know that when a menu pops up, Windows system sends the WM_INITMENU and WM_ENTERMENULOOP messages to the owner window of this menu, and when it disappears, Windows system sends the WM_EXITMENULOOP message to the owner window. If the menu items have MFT_OWNERDRAW attribute, Windows will also send WM_MEASUREITEM to measure the menu items and send WM_DRAWITEM message to let the owner window draw the menu items.

CMenuHelp is a template class, it is also a MixIn class. It handles the 5 messages of the owner window. In WM_INITMENU message handle, it gets the handle of the active menu (the menu is not shown out yet) and calls AttachActiveMenu() to add MFT_OWNERDRAW attribute to all menu items in this menu. When the menu disappears, DetachActiveMenu() will be invoked, this method removes the MFT_OWNERDRAW attribute that is added by AttachActiveMenu().

Properties of CMenuHelp

  • HMENU CMenuHelp::m_hActiveMenu

    Stores the handle of the menu that pops-up, OnMeasureItem and OnDrawItem uses this handle to take some operation on the pop-up menu.

  • COLORREF CMenuHelp::m_crText

    Color be used to draw the normal menu item text.

    COLORREF CMenuHelp::m_crHiText

    Color be used to draw the selected menu item text.

  • COLORREF CMenuHelp::m_crBkGnd

    Color be used to draw the background of menu item.

  • COLORREF CMenuHelp::m_crHiBkGnd

    Color be used to draw the background of selected menu item.

Methods of CMenuHelp

  • HMENU CMenuHelp::AttachActiveMenu(HMENU hActiveMenu)

    Attach the menu handle which will pop-up to m_hActiveMenu. hActiveMenu is the handle of the new menu. AttachActiveMenu() adds MFT_OWNERDRAW attribute to all menu items in this menu, so the system will send WM_MEASUREITEM and WM_DRAWITEM messages to our window. CMenuHelp handles the two messages to draw the menu. AttachActiveMenu() returns the last popped up menu's handle.

  • HMENU CMenuHelp::DetachActiveMenu()

    Remove MFT_OWNERDRAW attribute from all menu items of m_hActiveMenu and set m_hActiveMenu to NULL. It returns the handle of the current pop-up menu.

Use CMenuHelp in your code

It's easy to use this class in your code. Include MenuHelp.h to your project, add CMenuHelp to the derivation chain of your window, like this:

class CMainDlg : public CDialogImpl<CMainDlg>,public CMenuHelp<CMainDlg>

and lastly, add declaration information to message chain:


When it works, all popup menu of this window will be drawn by our CMenuHelp instead of Windows normal menu appearance.


CButtonHelp is a helper class to change the appearance of button controls in a dialog. It modifies and adds BS_OWNERDRAW property to all pushbuttons when a dialog initializes itself, link these button controls to CWzButtonImpl class. CWzButtonImpl is the real Owner-Draw button class. CWzButtonImpl uses SubclassWindow to take over the measure and draw behaviors of standard Windows buttons, like most other MFC's CButton derived class.


CWzButtonImpl is a Owner-Draw button class derived from CButton (ATL class). It is easy to use like most MFC's Owner-Draw button classes. Declare a CWzButtonImpl object first and then call SubclassWindow method to attach it to a pushbutton control. You can modify the drawing code in this class to change the appearance of the button control if you don't like the current style. You can also replace this class by other Owner-Draw button classes. You can find many just in CodeProject. You can also specify several different Owner-Draw button classes (style) in your apps, but can only use one class in a dialog.

Properties of CButtonHelp

  • CSimpleArray<t_ButtonClass *> CButtonHelp::m_arButtons

    Stores all t_ButtonClass objects in the dialog, and every t_ButtonClass object attaches to a pushbutton. t_ButtonClass is a template parameter, it should be CWzButtonImpl in our example code.

Methods of CButtonHelp

  • BOOL CButtonHelp::SubclassAllButtons()

    Attach pushbuttons in dialog to t_ButtonClass class objects. It is usually called in the initial function of a window, such as CDialog::OnInitDialog.

  • BOOL CButtonHelp::SubclassButton(HWND hBtnWnd)

    Creates a t_ButtonClass object and attaches it to a normal pushbutton control. hBtnWnd is the HWND of the button control.

  • BOOL CButtonHelp::UnSubclassButton(HWND hBtnWnd)

    Detach a t_ButtonClass object from pushbutton control. hBtnWnd is the HWND of the button control. The t_ButtonClass object will be deleted from CMenuHelp::m_arButtons.

  • int CButtonHelp::FindButtons(HWND hBtnWnd)

    To make sure whether a pushbutton control had been sub-classed already.

Use CButtonHelp in your code

Include ButtonHelp.h to your project first, and then add CButtonHelp class to the derivation chain of the dialog, like this:

class CMainDlg : public CDialogImpl<CMainDlg>, 
    public CButtonHelp<CMainDlg,CWzButtonImpl>

Finally, don't forget to call SubclassAllButtons() in CDialog::OnInitDialog.


CCtrlColor class handles some of WM_CTLCOLOR* messages (currently supports only WM_CTLCOLORDLG and WM_CTLCOLORSTATIC) and draws the controls in dialog using special text color and background color (specified by template parameter). It is a quite simple class, just 34 lines C++ code.

Properties of CCtrlColor

  • HBRUSH CCtrlColor::m_brBkgnd

    HBRUSH GDI object: CCtrlColor class uses this brush to paint the background of dialog and controls. CCtrlColor creates this object in construct function, uses color specified by template parameter t_crBkgnd, and deletes this object in destruct function.

Use CCtrlColor in your code

CCtrlColor has three template parameters: T, t_crTextColor and t_crBkgnd. The last two are optional, they have default values as RGB(64,64,255) and RGB(222,222,222). So you can add CCtrlColor to the derivation chain of your dialog in two methods:

class CMainDlg : public CDialogImpl<CMainDlg>, public CCtrlColor<CMainDlg>


class CAboutDlg : public CDialogImpl<CAboutDlg>, 
    public CCtrlColor<CAboutDlg,RGB(64,128,64)>

The same as CMenuHelp, adding declaration information to message chain:


    About the demo code

    All source code are organized in a VC6 project, it is not tested with VC7 compiler. Since this article is talking about ATL/WTL, you should also install WTL for compiling the source code. There is no limit on using this code, it can be used for any purpose.


    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

    Software Developer (Senior)
    China China
    I had graduated from HuaZhong University of Science & Technology. Now, I'm working in ZTE.

    You may also be interested in...


    Comments and Discussions

    Generalvery good Pin
    Member #380910511-Mar-07 0:50
    memberMember #380910511-Mar-07 0:50 
    GeneralProject Help wanted Pin
    Prafulla Tekawade11-Sep-06 21:45
    memberPrafulla Tekawade11-Sep-06 21:45 
    Generalno better appearance Pin
    Anonymous3-Apr-04 11:02
    sussAnonymous3-Apr-04 11:02 
    GeneralRe: no better appearance Pin
    XSimon3-Apr-04 20:17
    memberXSimon3-Apr-04 20:17 
    GeneralRe: no better appearance Pin
    Uwe Keim3-Apr-04 23:12
    sitebuilderUwe Keim3-Apr-04 23:12 
    GeneralRe: no better appearance Pin
    Frank De prins4-Apr-04 0:26
    memberFrank De prins4-Apr-04 0:26 
    GeneralRe: no better appearance Pin
    saltynuts20024-Apr-04 8:21
    membersaltynuts20024-Apr-04 8:21 
    GeneralMissing image Pin
    emilio_g2-Apr-04 6:10
    memberemilio_g2-Apr-04 6:10 
    GeneralRe: Missing image Pin
    Frank De prins3-Apr-04 0:35
    memberFrank De prins3-Apr-04 0:35 
    GeneralRe: Missing image Pin
    XSimon3-Apr-04 5:11
    memberXSimon3-Apr-04 5:11 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Terms of Use | Mobile
    Web03 | 2.8.171114.1 | Last Updated 2 Apr 2004
    Article Copyright 2004 by Simon.W
    Everything else Copyright © CodeProject, 1999-2017
    Layout: fixed | fluid