![]() |
Desktop Development »
Button Controls »
General
Intermediate
License: The Code Project Open License (CPOL)
XButtonXP - an XP-aware pushbutton that can also toggle and display an iconBy Hans DietrichXButtonXP is an XP theme-aware pushbutton that can optionally be used as a toggle button. It can also display an icon with or without text. |
VC6, VC8.0Win2K, WinXP, MFC, Dev
|
||||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
In this version I have corrected (I believe) all the outstanding bugs that have been reported (see list), and added several new features:
In this version they look like:
No hover![]() |
Hover![]() |
BS_MULTILINE:
| Icon on Left |
Icon on Right |
| No Icon |
No Text |
| Toggled |
Disabled |
And here is what XButtonXP looks like without themes:
| Icon on Left |
Icon on Right |
| No Icon |
No Text |
| Toggled |
Disabled |
XButtonXP can also be drawn using toolbar style:
No Hover![]() |
Hover![]() |
CXThemeHelper class to handle
loading UXTHEME.DLL and getting theme function pointers via GetProcAddress().
To make CXThemeHelper
lightweight and easy to reuse, I designed it as singleton class, so that there is only one copy of
DLL's module handle and function pointers per process.
The first time that CXButtonXP::DrawItem()
is called, the theme is opened for Button class:
if (ThemeHelper.IsThemeLibAvailable()) { m_hTheme = ThemeHelper.OpenThemeData(m_hWnd, _T("Button")); }
Aside from using theme functions for drawing the controls, one other thing CXButtonXP
must do is handle WM_THEMECHANGED message, which is
broadcast to every window following a theme change event:
case WM_THEMECHANGED: { if (IsThemed()) { if (m_hTheme) { // when user changes themes, close current theme and re-open ThemeHelper.CloseThemeData(m_hTheme); m_hTheme = NULL; m_bFirstTime = TRUE; } } }
CXThemeHelper API includes:
CXButtonXP will use themes to draw button in the following circumstances:
CXButtonXP::EnableTheming() function.
CXThemeHelper to enable theme support.
The next step was to fix default button problem.
WM_KEYDOWN
and WM_KEYUP, to catch ENTER and SPACE
key presses. I converted these to WM_LBUTTONDOWN and
WM_LBUTTONUP messages, to emulate mouse click.
Here are functions in CXButtonXP:
| Function | Description |
|---|---|
| CXButtonXP& EnableTheming(BOOL bEnable) | Enable visual themes for button. |
| COLORREF GetBackgroundColor() | Retrieve button custom background color (XBUTTONXP_NO_COLOR if none). |
| BOOL GetDrawToolbar() | Get toolbar style (TRUE = use toolbar style). |
| COLORREF GetTextColor() | Retrieve button custom text color (XBUTTONXP_NO_COLOR if none). |
| BOOL GetToggle() | Get toggle style (TRUE = button acts like toggle). |
| BOOL GetToggleState() | Get toggle state (TRUE = button is pressed). |
| BOOL IsThemed() | Get theming state (TRUE = visual theme is being used). |
| CXButtonXP& SetBackgroundColor(COLORREF rgb = XBUTTONXP_NO_COLOR) | Set button custom background color. Default is XBUTTONXP_NO_COLOR. |
| CXButtonXP& SetDrawToolbar(BOOL bDrawToolbar) | Set toolbar style. Default is FALSE. |
| CXButtonXP& SetIcon(UINT nIDResource, ICON_ALIGNMENT ia = LEFT) | Set icon from resource id. |
| CXButtonXP& SetIcon(HICON hIcon, ICON_ALIGNMENT ia = LEFT) | Set icon from HICON. |
| CXButtonXP& SetIconAlignment(ICON_ALIGNMENT ia) | Set icon alignment on button. Default is LEFT. |
| CXButtonXP& SetTextColor(COLORREF rgb = XBUTTONXP_NO_COLOR) | Set button custom text color. Default is XBUTTONXP_NO_COLOR. |
| CXButtonXP& SetToggle(BOOL bIsToggle) | Set toggle style. Default is FALSE. |
| CXButtonXP& SetToggleState(BOOL bToggled) | Set toggle state. Default is FALSE. |
To integrate CXButtonXP into your app, you first need to add following files to your project:
Next, include header file XButtonXP.h in appropriate project files
(typically the dialog header file).
Now you are ready to start using CXButtonXP.
The demo app shows how to call XButtonXP:
m_XButtonXP.SetIcon(IDI_MFC, CXButtonXP::LEFT);
You can combine XButtonXP function calls like this:
m_XButtonXP.SetIcon(IDI_MFC, CXButtonXP::LEFT)
.SetToggle(FALSE)
.EnableTheming(TRUE)
.SetDrawToolbar(FALSE);
BS_OWNERDRAW property
in dialog template.
DFCS_HOT, even though you have installed
Platform SDK.
To fix this, you need to put this line in
stdafx.h, before any includes:
#define WINVER 0x0500
#include "XButtonXPTest.h" from XButtonXP.cpp
This software is released under
The Code Project Open
License (CPOL). You are free to use this
software 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.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 19 Jan 2008 Editor: |
Copyright 2005 by Hans Dietrich Everything else Copyright © CodeProject, 1999-2009 Web16 | Advertise on the Code Project |