Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Button Text Color

Posted on 1998-08-14
12
Medium Priority
?
634 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
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
 
LVL 1

Accepted Solution

by:
marvinm earned 400 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

715 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