Results 1 to 3 of 3

Thread: text color

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Location
    youngstown, oh
    Posts
    11
    how do i change the text color in C++

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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."

  3. #3
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    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).
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width