raptor31
Dec 19th, 2000, 02:39 AM
how do i change the text color in C++
HarryW
Dec 19th, 2000, 06:30 AM
What do you mean? When you're writing text in a window in code? The text in your development environment? Selected text? Clarify please.
Vlatko
Dec 19th, 2000, 11:52 AM
I presume you mean the text of an edit or label control. Then here it is.
This can easily be accomplished by handling (NOT sending) the messages WM_CTLCOLOREDIT and WM_CTLCOLORSTATIC. Why the STATIC one? Well if the edit control is disabled then it sends WM_CTLCOLORSTATIC instead, so you need to be aware of which you are going to use or both.
To change the background colour you return the handle to a brush, which windows will use to paint the background of the control. To change the text color, you can use SetTextColor() on the HDC which is passed to you in the wParam parameter. You may also want to use SetBkMode() to set the text drawing mode to TRANSPARENT, otherwise your background color won't show through if you are changing it.
For example, to make the background black, you would do:
case WM_CTLCOLOREDIT:
return (LRESULT)GetStockObject(BLACK_BRUSH);
If your edit control is disabled, substitute WM_CTLCOLORSTATIC for WM_CTLCOLOREDIT.
But if your background is black, you probably don't want your text to be black. So we'll make it white:
case WM_CTLCOLOREDIT:
SetBkMode((HDC)wParam, TRANSPARENT);
SetTextColor((HDC)wParam, RGB(255, 255, 255));
return (LRESULT)GetStockObject(BLACK_BRUSH);
If you only want to change the color for a single control, you can match the ID of the control you want to change to the ID of the hwnd you get in lParam:
case WM_CTLCOLOREDIT:
if(IDC_OF_THE_CONTROL == GetDlgCtrlID((HWND)lParam)){
SetBkMode((HDC)wParam), TRANSPARENT);
SetTextColor((HDC)wParam, RGB(255, 255, 255));
return (LRESULT)GetStockObject(BLACK_BRUSH);
}
break;
If you haven't guessed by now, you can also use this to change the colors of STATIC controls, and a few others. (look up the WM_CTLCOLOR* messages in MSDN).