Solved

Button Text Color

Posted on 1998-08-14
12
620 Views
Last Modified: 2013-12-03
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!
0
Comment
Question by:PhilC
12 Comments
 
LVL 1

Expert Comment

by:marvinm
ID: 1413475
I'm pretty sure that you'll have to draw the button yourself to accomplish this.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1413476
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 : - )  )
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1413477
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
or
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1413478
>> 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
0
 
LVL 1

Expert Comment

by:marvinm
ID: 1413479
Drawing the button yourself is not as difficult as you might think. Some things to remember:
Process WM_LBUTTONDBLCLK,WM_LBUTTONDOWN,WM_LBUTTONUP,WM_MOUSEMOVE,and WM_PAINT.
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:
.
GetClientRect(hwnd,&rectButton);
hDC = BeginPaint(hwnd,&ps);
GetWindowText(hwnd,szBuf,iBufLen);
SetBkColor(hDC,GetSysColor(COLOR_3DFACE)); // if you want it grey
SetTextColor(hDC,/** some RGB **/);
SetBkMode(hDC,TRANSPARENT);
DrawEdge(hDC,&bRect,EDGE_RAISED,BF_RECT);
GetTextExtentPoint32(hDC,szBuf,strlen(szBuf),&tSize);
iTextPos = max(0,((rectButton.right - rectButton.left - tSize.cx)/2));
TextOut(hDC,iTextPos,0,szBuf,strlen(szBuf));
EndPaint(hwnd,&ps);
.

0
 

Author Comment

by:PhilC
ID: 1413480
Thanks marvin, lock this for the points.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Accepted Solution

by:
marvinm earned 100 total points
ID: 1413481
Thanks -mm
0
 
LVL 22

Expert Comment

by:nietod
ID: 1413482
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.
0
 
LVL 1

Expert Comment

by:marvinm
ID: 1413483
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
0
 
LVL 22

Expert Comment

by:nietod
ID: 1413484
>> 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.
0
 
LVL 1

Expert Comment

by:marvinm
ID: 1413485
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1413486
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.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now