Button no longer looks like a button...


I am trying to customise the background colour of the buttons on my Win32 dialog (e.g. 1 red and 1 yellow). I have been investigating WM_CTLCOLORBTN and it only works with owner drawn buttons.

I have a MESSAGE_HANDLER() for the WM_CTLCOLORBTN and it is called before every button is drawn and if it is an owner drawn button the button will change its background colour to the HBRUSH that the message returns.

The only problem is, when the dialog displays, the button just looks like a flat rectangle. It is no longer raised like a button and I have lost the text on the button face.

Can anyone help me with this.. I have searched the archives and can't figure out why this has happened.. Do I have to supply the button images myself?!?!?!

Thanks in advance..

Who is Participating?

Improve company productivity with a Business Account.Sign Up

NickRepinConnect With a Mentor Commented:
MS SDK says:

Buttons with the BS_PUSHBUTTON, BS_DEFPUSHBUTTON, or BS_PUSHLIKE styles do not use the returned brush. Buttons with these styles are always drawn with the default system colors. Drawing push buttons requires several different brushes-face, highlight and shadow-but the WM_CTLCOLORBTN message allows only one brush to be returned. To provide a custom appearance for push buttons, use an owner-drawn button.
you must overwrite OnDrawItem() and draw your button itself.
It is ownerdrawn!
middlelAuthor Commented:
My button is an owner-drawn button.. So I have to handle the OnDraw message and provide 2 bitmaps.. One for the 3D look and one for how it looks when it has been pressed...? How do I make sure that the button is displayed properly when it is clicked and not clicked?

Would I have to send some kind of draw message when the user clicks..?
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

In the WM_DRAWITEM handler, you have to check DRAWITEMSTRUCT.itemState member and draw the button accordingly.

ODS_DISABLED - button is disabled
(if your button can be disabled)

ODS_FOCUS - draw the focus rectangle (if you need it)

ODS_SELECTED - the button is pressed.
middlelAuthor Commented:
I have added a WM_DRAWITEM message for the dialog and it necer gets called.. I was hoping that the message would be on the parent control and it would just tell me which button the message was for.. Do I need to specifically handle the WM_DRAWITEM for each control..?

Also how do I tell the dialog to draw the button with my selected bitmap or default bitmap etc...

I was originally just coloring the button in with the following code when I handled the WM_CTLCOLORBTN message:

/*      HBRUSH hBrush = 0;

      ::DefWindowProc(m_hWnd, uMsg, wParam, lParam);

      hBrush = CreateSolidBrush(RGB(255,0,0));  
      return (LRESULT)hBrush;
middlelAuthor Commented:
I have managed to get the button to work now.. I did have to create 3 images and I had to handle the item action in the LPDRAWITEMSTRUCT and if it was a selected action, I then had to check the item state to see if it was being selected or if it had just obtained the focus.. It was a real long winded process though..

Just one more question though... Why have I lost my button text.? Is there any way to get it back.? I tried calling TextOut() on the hDC but it didn't do anything..

thanks in advance

You have to draw the text yourself every time when you draw the button.
Do it after drawing the bitmap.

TextOut must work fine, but it's better to use DrawText().
middlelAuthor Commented:
Ok, using DrawText() has displayed the title but the area immediately behind the text is now grey again...? I have tried to make it transparent but can't find anything to do that..?


SetBkColor/SetTextColor or SetBkMode/SetTextColor
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.