Solved

Button Text Color

Posted on 1998-08-14
12
631 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…

738 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