|
-
Dec 19th, 2000, 03:39 AM
#1
Thread Starter
New Member
how do i change the text color in C++
-
Dec 19th, 2000, 07:30 AM
#2
Frenzied Member
What do you mean? When you're writing text in a window in code? The text in your development environment? Selected text? Clarify please.
Harry.
"From one thing, know ten thousand things."
-
Dec 19th, 2000, 12:52 PM
#3
Frenzied Member
I presume you mean the text of an edit or label control. Then here it is.
Code:
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).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|