http://www.mindcracker.com/mindcracker/c_cafe/atl/catlbmpbtn.asp
ATL / COM
CatlbitmapButton - ATL / WTL OWNERDRAW Superclassed Bitmap Button
Submitted byDate of SubmissionUser Levelamit Dey06 / 07 / 2001InterMediate
Source Code: CATLBMPBTN.ZIP 83 KB
Introduction
Recently, in one of my projects, I needed to build a simple user interface consisting of a series of bitmap buttons in a dialog. Something simple and probably easy to use. About the same time, I came across David Pizzolato's very nice article on skinned button at codeproject.com, that got me thinking What came out of the whole endevour was CAtlBitmapButton -... an ATL / WTL ownerdrawn superclassed bitmap button The class is not really complete and represents work in progress I'll be glad if any of you find this useful :-). CAtlBitmapButton class is very friendly and you can learn to use it in no time. The hardest part might be drawing the bitmaps (if you are as artistically challenged as I am!). Now let's get down to The Basics. We'll Be Building An Atl / WTL Dialog-based Application SO I Assume You Are Slightly Familiar with atl / wtl and atl windowing.
How to us
To build the client, fire up Visual C and create a new Win32 application. Next we shall rig up ATL support to the project. Since we'd like to have ATL Wizard support, just follow the instructions step-by-step. If you Already Know How To Do This, You Can Skip this Part. First, in Project's Stdafx.h File, Replace
#include
WITH
#define richedit_VER 0x0100
#include
EXTERN CCOMMODULE _MODULE;
#include
#include
#include
#include
Now Add a new idl file to the project what Contains a Blank Library Definition Block Likelibrary
{
}
NOW, IN CLASSVIEW, RIGHT-CLICK THE IDL FILE You Just Added, And Choose Settings. In The General Tab of The Project Settings Dialog, Check The Exclude File from Build Option.
Next Modify Your Projects .cpp File So That Looks Like:
CCOMMODULE _MODULE;
Begin_Object_map (ObjectMap)
END_OBJECT_MAP ()
Int apientry Winmain (Hinstance Hinstance,
Hinstance Hprevinstance,
LPSTR LPCMDLINE,
INT ncmdshow)
{
// Todo: Place Code Here.
_Module.init (0, hinstance);
_Module.Term ();
Return 0;
}
Having Rigged Up ATL / WTL Support, Goto INSERT-> New ATL Object. In The Miscellaneous Category, Choose Dialog and Click on Next.Enter The Short Name As Dialog.
In the dialog resource, add 4 buttons (IDC_BUTTON1, IDC_BUTTON2, IDC_BUTTON3 and IDC_BUTTON4) and set the Ownerdraw properties of these buttons to true. You would also need to add a few bitmaps to the project such that each button has three state bitmaps (Selected Down and over.
Add The file, CatlbitmapButton.h to the project. In ClassView, Right Click The Dialog Class and Add Four Member Variables of Type CatlbitmapButton To It Like
CatlbitmapButton M_Button1, m_button2, m_button3, m_button4;
In the dialog's oninitdialog (), Add the Following Code:
m_button1.subclassWindow (getdlgitem (idc_button1));
M_Button1.LoadStateBitmaps (idb_loadu, idb_loadd, idb_load);
m_button2.subclasswindow (getdlgitem (idc_button2));
M_Button2.LoadStateBitmaps (idb_playu, idb_playd, idb_play);
m_button3.subclasswindow (getdlgitem (iDC_Button3));
M_Button3.LoadStateBitmaps (idb_saveu, idb_saved, idb_save);
m_button4.subclasswindow (getdlgitem (id_button4));
m_button4.LoadStateBitmaps (IDB_QUITU, IDB_QUITD, IDB_QUIT); CAtlBitmapButton has a method LoadStateBitmaps () to load the state bitmaps The last thing to do is to add the ATL macro REFLECT_NOTIFICATIONS () to the dialog's message map like.:
Begin_MSG_MAP (CDIALOG)
Message_handler (WM_INITDIALOG, OnInitdialog)
Command_id_handler (IDOK, ONOK)
Command_id_handler (IDCANCEL, ONCANCEL)
Command_id_handler (id_quit, ONQUIT)
Reflect_notifications ()
END_MSG_MAP ()
. Build the project and run it Check that the buttons are displaying the correct state bitmap To handle button-clicks use ATL macro COMMAND_ID_HANDLER () in the messagemap as shown in above code for the OK and Cancel button OnCancel looks like...:
Lresult Oncancel (Word WordiCode, Word WID, HWND HWNDCTL, BOOL & BHANDED)
{
Enddialog (WID);
Return 0;
}
That's it. Yippee! Has fun.
Author
Amit dey.amitdey@joymail.com
Acknowledgements
David pizzolato.