Click here to Skip to main content
Click here to Skip to main content

XP Style CBitmapButton (CHoverBitmapButton)

, 22 Feb 2003 CPOL
Rate this:
Please Sign up or sign in to vote.
Style and theme aware bitmaps, give your old apps a new look under Windows XP.

Sample Image - CHoverBitmapButton.jpg

Introduction

I was in the middle of updating a program that I’d developed under NT and Win2K and I’d used CBitmapButton throughout the program.  When porting over a new version of the program to run under XP, I noticed that my "old" bitmap buttons in VC7 weren’t style aware, and made the app look a tad dated.  A search on the web brought my attention to a couple of other examples of hover buttons, but none which took my investment with CBitmapButton into consideration.  I wanted a CBitmapButton derived class that I could just plug into my older app.  This is the first article I’ve submitted here, so excuse my prose.

What’s my Theme?

Using UXTHEME.DLL, as I found in an example project by Ewan Ward.  I created a new CTheme class.  If you prefer, you can use the WTL CTheme class.  However,  I just created one that’s suitable for my purpose.

If you’re creating an MFC Doc/View application, then in CMainFrame declare a CTheme member m_theme, if it’s a dialog based app – in the CDialog derived main window declare a CTheme member m_theme :

CTheme m_theme;

In a Doc/View app, in CMainFrame::OnCreate(), or in a Dialog based app in OnInitDialog() add:

m_theme.Init(m_hWnd);

The function CTheme::Init() calls a function GetAppearance() to see if the style is XP Style or Windows Classic Style.  Actually, it just checks if the current OS is XP, and if the Classic Style is selected.

BOOL CTheme::GetAppearance(void)
{
    // For XP - Detect if the Window Style is Classic or XP
    OSVERSIONINFO osvi;
    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

    GetVersionEx(&osvi); 
    if (osvi.dwMajorVersion < 5)      // Earlier than XP
        return FALSE;
    /////////////////////////////////////////////////////

    HKEY   hKey;
    CString szSubKey = _T("Control Panel\\Appearance");
    CString    szCurrent = _T("Current");
    DWORD  dwSize = 200;

    unsigned char * pBuffer = new unsigned char[dwSize];
    memset(pBuffer, 0, dwSize);
    if (RegOpenKeyEx(HKEY_CURRENT_USER, (LPCSTR)szSubKey, 0L, KEY_READ, &hKey) 
        != ERROR_SUCCESS)
    {
        // Can't find it
        delete []pBuffer; 
        return FALSE;
    }
    RegQueryValueEx(hKey, szCurrent, NULL, NULL, pBuffer, &dwSize); 
    RegCloseKey(hKey);

    szCurrent = pBuffer;
    delete []pBuffer;

    if (szCurrent == _T("Windows Standard"))
        return FALSE;
    return TRUE;
}

If CTheme::Init() is never called, then CHoverBitmapButton will default to using the older button style.  If the style is XP Style, then the DLL is loaded.

What about the bitmaps?

You should start with the same bitmaps as you used in the stock CBitmapButton class, which includes drawing the edge.   For the XP style buttons, create (or copy) your old button bitmaps with a white background – you don’t have to draw the edges as you did with the old CBitmapButton .  For example, I had a button:  (directU.bmp), and for the XP style, I created a new bitmap: (xpdirectU.bmp).  For the older style I had F, X and D bitmaps as well; for the XP style, I only needed the U and X bitmaps.  I also named the resources the same – except I prefixed the new XP style resource names with the letters XP.  This is important for the CHoverBitmapButton class to differentiate between the XP and Classic style bitmaps.  You may set the prefix yourself using the public function CHoverBitmapButton::SetPrefix() – or just use the default.

The bitmaps are loaded the same as before with either CHoverBitmapButton::Load() or CHoverBitmapButton::AutoLoad().

In the example Dialog application – in OnInitDialog():

m_CtrlButton.Load(IDC_DIRECT, this, &m_theme);

In a Doc/View application you could use the normal:

m_CtrlButton.AutoLoad(IDC_DIRECT, this);

and in the CHoverBitmapButton constructor, m_pTheme can be initialized using a pointer to the variable m_theme via CHoverBitmapButton::GetFrame().

So for the 2 styles – the bitmap resources would be named:

State Classic Windows Style   XP Style  
UP "DIRECTU" "XPDIRECTU"
DOWN "DIRECTD"    
FOCUSED "DIRECTF"    
DISABLED "DIRECTX" "XPDIRECTX"

The new XP Style buttons will be drawn using the DLL function DrawThemeBackground that draws the button texture and edges.

Change my style

To handle the message WM_THEMECHANGED, you have to set WINVER, _WIN32_WINNT, _WIN32_WINDOWS and _WIN32_IE all equal to 0x0501 in stdafx.h 

If you don’t want to detect the changes, then you could have an app that runs under previous versions of Windows, but if your only target is XP, then the example includes the code to handle the WM_THEMECHANGED message.

Includes

Add the line 

#include "ThemeLib.h"

to stdafx.h

Using the class

As has been mentioned, the use is slightly different between a Dialog type app vs. a Doc/View type app.

Dialog type app:

In the main CDialog derived class’s header file:

#include "theme.h"
#include "HoverBitmapButton.h"

and add a public class member:

CTheme      m_theme;

Change the CButton controls that you added with ClassWizard to:

CHoverBitmapButton m_CtrlButton;

And in OnInitDialog:

m_theme.Init(m_hWnd);
m_CtrlButton.Load(IDC_DIRECT, this, &m_theme);

Doc/View type app:

In MainFrame.h:

#include "theme.h"

and add a public class member:

CTheme m_theme;

And in CMainFrame::OnCreate():

m_theme.Init(m_hWnd);

In a CDialog derived class which has a button, in the header file:

#include "HoverBitmapButton.h"

Change the CButton controls that you added with ClassWizard to:

CHoverBitmapButton m_CtrlButton;

And in OnInitDialog:

m_ CtrlButton.AutoLoad(IDC_DIRECTORY, this);

Code changes required:

For Doc/View, change the code in HoverBitmapButton.cpp where the comments show Doc/View.

In the creator:

CHoverBitmapButton::CHoverBitmapButton()
{
      m_bHovering = FALSE;
     // remove the comments from these 2 lines:

     m_pTheme = &(GetFrame()->m_theme);     // If Doc/View m_theme is 
                                            // a member of CMainFrame
     m_bXPTheme = m_pTheme->m_bXPTheme;

     m_szXPPrefix = _T("XP");

     m_bThemeChanging = FALSE;
}

In the Header and the code file, uncomment the function

CMainFrame* CHoverBitmapButton::GetFrame(void)

Credits

Thanks to Ewan Ward for the work in his Native Win32 Theme aware Owner-draw Controls without MFC.

License

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

Share

About the Author

Rail Jon Rogut
Web Developer
United States United States
Rail Jon Rogut is a Grammy nominated recording engineer who writes software in his spare time.

Comments and Discussions

 
Generalbug with xp-styles Pinmemberarkebuzy22-Nov-05 22:40 
GeneralRe: bug with xp-styles PinmemberRail Jon Rogut22-Nov-05 22:51 
GeneralRe: bug with xp-styles Pinmemberarkebuzy22-Nov-05 23:03 
QuestionXP-styled CFileDialog PinmemberOleksa Dovbush11-Sep-05 11:50 
AnswerRe: XP-styled CFileDialog PinmemberRail Jon Rogut11-Sep-05 14:08 
QuestionRe: XP-styled CFileDialog PinmemberOleksa Dovbush12-Sep-05 1:20 
AnswerRe: XP-styled CFileDialog PinmemberRail Jon Rogut12-Sep-05 8:16 
GeneralDefault button PinmemberOleksa Dovbush11-Sep-05 10:48 
GeneralRe: Default button PinmemberRail Jon Rogut11-Sep-05 10:57 
QuestionRe: Default button PinmemberOleksa Dovbush11-Sep-05 12:02 
GeneralA more proper way to see if themes are activated PinmemberPEK3-Sep-04 5:22 
GeneralA minor Unicode fix PinmemberPEK6-Sep-04 10:41 
QuestionHow to use as a checkbox PinmemberErwin Davidse17-Jun-04 5:08 
GeneralWindows Version detection BUG (CTheme) Pinmembercyril.jean1-Jun-04 23:27 
GeneralTurning into a control Pinmembert9mike12-May-03 6:14 
Generalfew drawing problems PinmemberTibor Blazko26-Mar-03 23:37 
GeneralRe: few drawing problems PinmemberRail Jon Rogut27-Mar-03 9:36 
GeneralRe: few drawing problems PinmemberTibor Blazko28-Mar-03 0:16 
GeneralRe: few drawing problems PinmemberRail Jon Rogut28-Mar-03 8:58 
GeneralRe: few drawing problems PinmemberRail Jon Rogut28-Mar-03 10:30 
GeneralRe: few drawing problems PinmemberTibor Blazko30-Mar-03 21:23 
GeneralHow to implement this button in oracle developer. Pinmembersrkhan2119-Apr-04 11:27 
GeneralRe: few drawing problems PinmemberStephen C. Steel13-Dec-05 7:45 
GeneralRe: few drawing problems PinmemberRail Jon Rogut13-Dec-05 7:52 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 23 Feb 2003
Article Copyright 2003 by Rail Jon Rogut
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid