|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionLooking around for a theme-aware toggle button that could display text and an icon, I could not find one, so XButtonXP is the result: a pushbutton that can also be a toggle button, and can display icon and/or text.
What's New in v1.3In this version I have corrected (I believe) all the outstanding bugs that have been reported (see list), and added several new features:
XButtonXP in ActionHere is what XButtonXP looks like with themes:
And here is what XButtonXP looks like without themes:
XButtonXP can also be drawn using toolbar style:
CXButtonXP Implementation Notes
Theme SupportNormally, when you add manifest file to exe, all controls used by exe will be displayed as themed. However, this is not true of ownerdraw controls - the XP theming engine just ignores them. Since I needed an ownerdraw button, it meant that XButtonXP had to handle the XP theming support. I created theCXThemeHelper 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 if (ThemeHelper.IsThemeLibAvailable()) { m_hTheme = ThemeHelper.OpenThemeData(m_hWnd, _T("Button")); }
Aside from using theme functions for drawing the controls, one other thing 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 APITheCXThemeHelper API includes:
Controlling XButtonXP ThemingCXButtonXP will use themes to draw button in the following circumstances:
CXButtonXP Starting PointI used Ewan Ward's article Native Win32 Theme aware Owner-draw Controls without MFC as starting point for XButtonXP. First, I adapted his code for MFC, and then usedCXThemeHelper to enable theme support.
The next step was to fix default button problem.
Default Button ProblemThis problem plagues all ownerdraw buttons. Left uncorrected, it can give the visual appearance that there are multiple default buttons on dialog, and improperly handles ENTER key. Fortunately, Paolo Messina describes this problem in detail, and has written very nice class to transparently fix this problem for any ownerdraw button.
Keyboard InputThe final task was to ensure that SPACE and ENTER keys worked properly. On standard pushbutton, these keys produce button-click event. In order for XButtonXP to handle them in same way, I had to intercept WM_GETDLGCODE message, and ask for all keys. At the same time, I did not want to interfere with TAB key processing. The WM_GETDLGCODE message provides mechanism to do this, by returning appropriate code. Then, I added handlers forWM_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.
CXButtonXP Functions
Here are functions in
How to use
To integrate
Next, include header file XButtonXP.h in appropriate project files
(typically the dialog header file).
Now you are ready to start using 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);
Tips & Tricks
Acknowledgments
Revision HistoryVersion 1.3 - 2008 January 19
Version 1.2 - 2005 April 20
Version 1.1 - 2005 March 30
Version 1.0 - 2005 March 22
UsageThis 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.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||