5,443,978 members and growing! (20,138 online)
Email Password   helpLost your password?
Desktop Development » Button Controls » Owner-draw buttons     Intermediate License: The Code Project Open License (CPOL)

XEmphasisButton - bold and underlined text in a non-ownerdraw button

By Hans Dietrich

XEmphasisButton allows you to display buttons with bold and underlined text without requiring ownerdraw. This means that the button is perfectly compatible with XP themes, and also works on previous Windows versions.
VC6, C++Windows, Win2K, WinXP, MFC, VS6, Visual Studio, Dev

Posted: 16 Aug 2006
Updated: 23 May 2007
Views: 45,091
Bookmarked: 57 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
41 votes for this Article.
Popularity: 7.83 Rating: 4.86 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 2.4%
3
4 votes, 9.8%
4
36 votes, 87.8%
5

Introduction

Recently my new client wanted a custom button for the new consumer application that I am building for him. Oh oh, I thought; buttons are difficult, because of the many states and modes you have to handle. Even worse, ownerdraw buttons require special treatment to look right under themed XP. The button my client wanted was similar to what is displayed when you select Search... from Explorer right-click menu:

screenshot The text of checkboxes is displayed in bold when they are selected.

Mousing over one of these buttons produces another effect:

screenshot When the mouse cursor is moved over a checkbox, its text is underlined.

The same two effects occur with radio buttons that are displayed on the Search panel.

Now my only question was, "How did Microsoft implement these?" Is it possible these buttons are not really ownerdraw? To investigate further, I turned to one of my favorite programming tools, HPS HwndSpy. To my relief, HPS HwndSpy showed that buttons were not ownerdraw:

screenshot

Implementation Details

Now that I was sure I could implement this button without needing to make them ownerdraw, I decided that API could be very simple; the only option was display state: bold, underlined, or bold and underlined. So XEmphasisButton has two public APIs:

    Emphasis GetEmphasis()   { return m_eEmphasis; }

    void SetEmphasis(Emphasis eEmphasis) 
    { 
        m_eEmphasis = eEmphasis; 
        SetEmphasisFont();
    }

The choices for parameter are given by enum:

    enum Emphasis
    {
        NONE = 0,
        BOLD,         // button text will be bold if GetCheck() is TRUE
        UNDERLINE,    // button text will be underlined if mouse over
        BOLD_AND_UNDERLINE
    };

Since emphasis state is limited to four cases, programming is fairly simple. In PreSubclassWindow(), I create four fonts, and in DefWindowProc() I handle three messages:

  • BM_SETCHECK — this message is sent when the check state of radio button or check box has changed:
        if (message == BM_SETCHECK)
        {
            if (wParam == BST_CHECKED)
            {
                if (m_eEmphasis == BOLD || m_eEmphasis == BOLD_AND_UNDERLINE)
                {
                    // button should be bolded
                    if (!m_bBold)
                    {
                        if (m_bUnderline)
                            SetFont(&m_fontBoldAndUnderline);
                        else
                            SetFont(&m_fontBold);
                        m_bBold = TRUE;
                    }
                }
            }
    
            if (wParam == BST_UNCHECKED)
            {
                if (m_eEmphasis == BOLD || m_eEmphasis == BOLD_AND_UNDERLINE)
                {
                    // button should not be bolded
                    if (m_bBold)
                    {
                        if (m_bUnderline)
                            SetFont(&m_fontUnderline);
                        else
                            SetFont(&m_fontNormal);
                        m_bBold = FALSE;
                    }
                }
            }
        }
  • WM_MOUSEMOVE — this message is sent when cursor moves over button. I use it to detect when cursor has just entered button's client area, to enable underlining:
        else if (message == WM_MOUSEMOVE)
        {
            if (!m_bOverControl)        // cursor has just moved over control
            {
                m_bOverControl = TRUE;
                
                if (m_eEmphasis == UNDERLINE || 
                    m_eEmphasis == BOLD_AND_UNDERLINE)
                {
                    // button should be underlined
                    if (!m_bUnderline)
                    {
                        if (m_bBold)
                            SetFont(&m_fontBoldAndUnderline);
                        else
                            SetFont(&m_fontUnderline);
    
                        m_bUnderline = TRUE;
                    }
    
                    SetTimer(1, 100, NULL);    // use timer to detect when
                                               // mouse leaves
                }
            }
        }
  • WM_TIMER — timer is used to detect when mouse leaves client area of button:
        else if (message == WM_TIMER)
        {
            if (m_bOverControl)    // only do this if previously over button
            {
                CRect rect;
                GetWindowRect(&rect);
    
                CPoint point;
                GetCursorPos(&point);    // always screen coordinates
    
                if (!rect.PtInRect(point))
                {
                    // not over button, so remove underlining
                    if (m_bUnderline)
                    {
                        if (m_bBold)
                            SetFont(&m_fontBold);
                        else
                            SetFont(&m_fontNormal);
    
                        m_bUnderline = FALSE;
                    }
                    m_bOverControl = FALSE;
                    KillTimer(wParam);
                }
            }
        }

Demo App

The demo app allows you to try out various combinations of display attributes:

screenshot

How To Use

To integrate CXEmphasisButton class into your app, you first need to add following files to your project:

  • XEmphasisButton.cpp
  • XEmphasisButton.h

Next, include header file XEmphasisButton.h in appropriate project files (usually, this will be in header file for dialog class). Then replace declaration of button control with this:

    CXEmphasisButton   m_MyButton;
(use whatever variable name already exists).

Now you are ready to start using CXEmphasisButton. In dialog's OnInitDialog() function, insert line

    m_MyButton.SetEmphasis(CXEmphasisButton::BOLD_AND_UNDERLINE);
(use whatever display attributes you wish).

Revision History

Version 1.0 — 2006 August 15

  • Initial public release

Usage

This software is released into the public domain. You are free to use it in any way you like, except that you may not sell this source code. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.

License

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

About the Author

Hans Dietrich


Mvp
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.
Occupation: Software Developer (Senior)
Location: United States United States

Other popular Button Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 24 of 24 (Total in Forum: 24) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralMy 5 and thanksmemberFranc Morales22:20 26 Jun '08  
GeneralRe: My 5 and thanksmvpHans Dietrich22:39 26 Jun '08  
GeneralRe: My 5 and thanksmemberFranc Morales1:08 27 Jun '08  
GeneralNice articlememberTamal Saha11:21 23 Oct '07  
GeneralWM_MOUSELEAVE?memberPerry Zhu22:22 4 Jun '07  
GeneralRe: WM_MOUSELEAVE?mvpHans Dietrich0:09 5 Jun '07  
GeneralRe: WM_MOUSELEAVE?memberPerry Zhu23:35 5 Jun '07  
GeneralComplimentsupportervirtualnik9:59 30 May '07  
GeneralI've been meaning to thank you...memberTom Guidarelli6:56 29 May '07  
GeneralRe: I've been meaning to thank you...mvpHans Dietrich17:40 29 May '07  
GeneralNice control!memberJ Whattam17:03 23 Aug '06  
GeneralSimple, but very usefulmemberAnna-Jayne Metcalfe20:41 21 Aug '06  
GeneralVery nice!...and a question about menu items in boldmemberAlexandru Matei0:34 21 Aug '06  
GeneralRe: Very nice!...and a question about menu items in boldmemberHans Dietrich17:49 21 Aug '06  
GeneralRe: Very nice!...and a question about menu items in boldmemberAnna-Jayne Metcalfe20:39 21 Aug '06  
GeneralExcellent as alwaysmemberJohn R. Shaw7:25 20 Aug '06  
GeneralRe: Excellent as alwaysmemberHans Dietrich7:37 20 Aug '06  
AnswerRe: Excellent as alwaysmemberKarstenK4:17 24 Aug '06  
GeneralRe: Excellent as alwaysmemberHans Dietrich20:29 20 Aug '06  
GeneralRe: Excellent as alwaysmemberJohn R. Shaw7:41 22 Aug '06  
GeneralExcellent!supporterWes Aday7:07 18 Aug '06  
GeneralGreat job !!!memberLeopoldo Peralta14:36 17 Aug '06  
GeneralOther featuresmemberalexey N21:26 16 Aug '06  
GeneralRe: Other featuresmemberHans Dietrich4:12 18 Aug '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 23 May 2007
Editor: Sean Ewington
Copyright 2006 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2008
Web07 | Advertise on the Code Project