Button Text Color

I create a standard windows button using CreateWindow().
How can I change the color of the text in the button. (preferably without drawing the whole button myself).
I have tried trapping the WM_CTLCOLORBTN message and setting the text color, but according to MS documentation
"The text color of a push button applies only to its focus rectangle; it does not affect the color of the text"
So is there a SetFocusRectangleColor() function that will set the color of the text and not affect to color of the focus rectangle? It seems like this should be an easy thing to do!
Who is Participating?
marvinmConnect With a Mentor Commented:
Thanks -mm
I'm pretty sure that you'll have to draw the button yourself to accomplish this.
I've been thinking about this, I suspect it is because windows can draw the text disabled (etched).  Thus you can't just give it a single color to draw with.  Thus they didn't provide a way to do this because it would be too complex (And they wanted to keep windows programming simple : - )  )
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

The problem is the DrawFocusRect API uses the current text color.  If you change the text color you change both the text and the focus rectangle (but then you know that <g>)

My suggestions are :-
1. Make the button ownerdraw and paint it yourself
2. Make a little bitmap with the text in whatever color you like, and make the button display that.  This changes the "text" color.  Meanwhile use SetTextColor in WM_CTLCOLORBTN to change the focus rectangle color.
>> The problem is the DrawFocusRect API uses the current text color.  If you
>> change the text color you change both the text and the focus rectangle
>> (but then you know that <g>)

It DOESN'T change the text color.  Unfortunately, that is what he wants to change.  If it changed the focus color as well, I don't think we would mind, but it ONLY changes the focus color
Drawing the button yourself is not as difficult as you might think. Some things to remember:
You'll have to set capture on WM_LBUTTONDOWN, and release capture on WM_LBUTTONUP.
You'll also need a way to keep track of whether or not your button was clicked on. That way you will know how to draw it when you have capture and receive WM_MOUSEMOVE.
Here's an example of drawing a button:
hDC = BeginPaint(hwnd,&ps);
SetBkColor(hDC,GetSysColor(COLOR_3DFACE)); // if you want it grey
SetTextColor(hDC,/** some RGB **/);
iTextPos = max(0,((rectButton.right - rectButton.left - tSize.cx)/2));

PhilCAuthor Commented:
Thanks marvin, lock this for the points.
marvinm, why do you need to process any messages other than WM_PAINT?  If the mouse messages etc cause the button to change appearance, I would think they would invalidate the button and cause a paint message.  Thus you should only need to process the paint message.
That was my initial expectation; however, it is my experience that you do NOT receive a paint message when you click on a button. Windows draws the button as 'pressed' on the keydown message. I will test this out though. - mm
>> Windows draws the button as 'pressed' on the keydown message
No intelligent programmer would design it like that---You're probably right, that's probably how it is done.
I haven't written a test program yet, but 'spy++ing' on several apps, it looks like push-buttons do not recieve paint messages as a result of mouse activity. I may be way off base though. If this truely is the case I would REALLY like to know the reasoning.
No, I confirmed that.  There are no paint messages when the button is pressed.  Why do assume there any reasoning about it?

Unfortunately, Philc, that's going to make it tougher to do, but I'm sure it still is doable.
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.