I have searched this many times, but everything I find is MFC. I want it in C++ WinAPI. I know how to change the style of a button control, but I cannot find out how to make a button a different color. So, how can I change the background color of a WinAPI button control using C++? I don't want to do this with a resource file.
Thank you!
You need an owner-drawn button for that. For some reason, unlike other controls, regular buttons don't react to changes made in
WM_CTLCOLORBTN
message handler.I do not remember the link for the original code, but code bellow helped me in the past to solve the problem you currently face.
Notice that it doesn't have resource file, as you have requested, and is in plain Win32 API.
Study it carefully, everything is commented by the original author.
Hopefully it will help you, as it helped me in the past.
If you have any questions, ask, I will try to answer them.
There are 4 ways, as far as I know, to change the color of the button:
Owner draw ( obvious solution ).
Custom draw ( in my opinion the best solution ).
Subclassing the control ( I do not like it, but it is possible ).
Use bitmaps as your buttons background.
Handling
WM_CTLCOLORBTN
:Emphasis is mine. If you plan to use this option read the Remarks section carefully.
Code bellow demonstrates cases 1, 2 and 4.
You can edit a button(which has the flag BS_OWNERDRAW) in the message WM_DRAWITEM on the DialogProc(MSDN About WM_DRAWITEM), heres a simple example of how to draw a simple button:
Instead of a link I'll just post a copy from my other post using custom-drawing, similar to
alwayslearningnewstuff
example:First picture shows when nothing is selected, second shows when first button is selected and was pushed and the last one shows when second button was pushed and the mouse is over it (notice the increase of brightness - cutom hilight). In order to do this, you must catch NM_CUSTOMDRAW message and paint button yourself. And this is how you do it. Also added gradient brush function and some comments.