Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 638
  • Last Modified:

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!
0
PhilC
Asked:
PhilC
1 Solution
 
marvinmCommented:
I'm pretty sure that you'll have to draw the button yourself to accomplish this.
0
 
nietodCommented:
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
 
Answers2000Commented:
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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
nietodCommented:
>> 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
 
marvinmCommented:
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
 
PhilCAuthor Commented:
Thanks marvin, lock this for the points.
0
 
marvinmCommented:
Thanks -mm
0
 
nietodCommented:
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
 
marvinmCommented:
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
 
nietodCommented:
>> 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
 
marvinmCommented:
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
 
nietodCommented:
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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Tackle projects and never again get stuck behind a technical roadblock.
Join Now