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

CAtlBitmapButton - ATL/WTL Ownerdraw Superclassed Bitmap Button

, 11 Jun 2001
Rate this:
Please Sign up or sign in to vote.
An owner drawn ATL/WTL bitmap button
<!-- Download Links -->

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 Smile | :) .

The 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

 #include &ltwindows.h>
with
#define RICHEDIT_VER 0x0100
#include &ltatlbase.h> 
extern CComModule _Module;
#include &ltatlcom.h> 
#include &ltatlwin.h>
#include &ltatlapp.h>
#include &ltatlctrls.h> 

Now add a new IDL file to the project that contains a blank library definition block like

library <Project Name>
{

};  

Now, in the 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:

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 message map as shown in above code for the OK and Cancel button. OnCancel looks like:

LRESULT OnCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	EndDialog(wID);
	return 0;
}

That's it. Yippee!
Have fun.

Acknowledgements

David Pizzolato.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Amit Dey
Web Developer
India India
Amit Dey is a freelance programmer from Bangalore,India. Chiefly programming VC++/MFC, ATL/COM and PocketPC and Palm platforms. Apart from programming and CP, he is a self-taught guitar and keyboard player.
 
He can be contacted at visualcdev@hotmail.com
 

Comments and Discussions

 
GeneralNeed Help Pinmemberclement Lam20-Jan-06 5:06 
QuestionWhy not use the CBitmapButton class? PinmemberClaus20-Jun-01 22:13 

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 | Mobile
Web04 | 2.8.141015.1 | Last Updated 12 Jun 2001
Article Copyright 2001 by Amit Dey
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid