http://www.mindcracker.com/mindcracker/c_cafe/atl/CAtlBmpBtn.asp
ATL/COM |
CAtlBitmapButton - ATL/WTL Ownerdraw Superclassed Bitmap Button
Submitted by | date of submission | user level |
Amit Dey | 06/07/2001 | Intermediate |
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 use
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
with
Now add a new IDL file to the project that contains a blank library definition block like
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 it looks like:
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
In the dialog's OnInitDialog(), add the following code :
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:
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:
That's it. Yippee!
Have fun.
Author
Amit Dey.
[email protected]
Acknowledgements
David Pizzolato.