[comp.windows.ms.programmer] Owner Draw buttons

whisd@sersun1.essex.ac.uk (Whiteside S D B) (06/09/91)

Someone recently posted a question about owner-draw buttons .

I had a debate about this going a month or so back and summarized the
replies as follows. I got the buttons to work as described below. Hope
this helps!

"Many thanks to all who replied to my recent query regarding putting bitmaps on buttons. I've managed to do this thanks to their help!

One way to do this is:

i)	Create the button with the style WS_OWNERDRAW, which means you have to draw the item 
ii)	The window procedure associated with the button (or the dialog box function of the dialog with the control in) will receive 3 kinds of message which it will have to deal with:

Each message is of type WM_DRAWITEM. This message has the following components:

wParam	-	child control ID
lParam	-	long pointer to DRAWITEMSTRUCT

It's the DRAWITEMSTRUCT that tells you what to do.

When the control needs to be first drawn the itemAction field of the structure contains ODA_DRAWENTIRE.

When the control is pushed the itemAction is ODA_SELECT and the is also received when the button is released. I keep a BOOL variable to alternate two bitmaps: one for button up, one for button down.

When focus is given to the button itemAction is ODA_FOCUS

Also in the structure you get: the button window handle, a device context and a rectangle structure showing the size of the button.

I used LoadBitmap to get a handle to the bitmap and called Petzold's DrawBitmap() function to put it into the device context.

Remember to delete the bitmap once you've done this. (The bitmap is selected into a memory device context, thence into the actual device context by DrawBitmap, so it's okay to delete the bitmap).

I use C++ so for speed I load the bitmaps in the class constructor and delete them in the class destructor.

I hope this info (which isn't all in one place in the literature I've got) helps others make a very pretty interface!
"

Simon Whiteside