Thread: Setting static text color -- I know I'm doing something wrong

  1. #1
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584

    Setting static text color -- I know I'm doing something wrong

    What I want to do is set the static control's text color to red. What I did was capture the WM_CTLCOLORSTATIC message like this:
    Code:
    case WM_CTLCOLORSTATIC:
        SetTextColor((HDC) wParam, RGB(255,0,0));
        return 0;
    So, shouldn't this change the text color of the static to red? Am I missing something or doing something wrong? Thanks

    Garfield
    1978 Silver Anniversary Corvette

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    You have to return a valid brush handle (which you are responsible for freeing). Don't ask my why it wouldn't work as written because your code looked just fine to me.

    Code:
    //In wndproc
    static HBRUSH hbr=CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
    .
    .
    .
    case WM_CTLCOLORSTATIC:
            {
            SetBkMode((HDC) wParam,TRANSPARENT);
            SetTextColor((HDC) wParam, RGB(255,0,0));
            return (LRESULT)hbr;
            }
    case WM_DESTROY:
        {
        DeleteObject(hbr);
        PostQuitMessage(0);
        return 0;
        }
    Hope that helps.

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    The code is still not working. I'm even returning a handle to a HBRUSH type. It's not setting the text color of the static control to red. Would you like to see the code and what I'm doing?
    1978 Silver Anniversary Corvette

  4. #4
    When I change the color of a static control for a hyperlink or something like that, I always do it like this:
    Code:
    HWND hControl = HWND(lParam);//handle to the control
    
    /*-------------------------------------------------*/
    case WM_CTLCOLORSTATIC: // Draw the hyperlink in blue
           switch (GetDlgCtrlID(hControl))
           {
            case IDC_HYPERLINK:
                 SetBkMode((HDC)wParam, TRANSPARENT);
                 SetTextColor((HDC)wParam, RGB(255, 0,0 ));
    		 return (LRESULT)GetStockObject(HOLLOW_BRUSH);
           }
    		break;
    I think the problem is that he doesn't know wich static control to change the color of

  5. #5
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    this is MFC from the MSDN i believe but it might give you some idea of how

    Code:
    HBRUSH CStaticLink::CtlColor(CDC* pDC, UINT nCtlColor) 
    { 
        ASSERT(nCtlColor == CTLCOLOR_STATIC); 
        DWORD dwStyle = GetStyle(); 
         
        HBRUSH hbr = NULL; 
        if ((dwStyle & 0xFF) <= SS_RIGHT) { 
    
            // this is a text control: set up font and colors 
            if (!(HFONT)m_font) { 
                // first time init: create font 
                LOGFONT lf; 
                GetFont()->GetObject(sizeof(lf), &lf); 
                lf.lfUnderline = TRUE; 
                m_font.CreateFontIndirect(&lf); 
            } 
    
            // use underline font and visited/unvisited colors 
            pDC->SelectObject(&m_font); 
            pDC->SetTextColor(m_color); 
            pDC->SetBkMode(TRANSPARENT); 
    
            // return hollow brush to preserve parent background color 
            hbr = (HBRUSH)::GetStockObject(HOLLOW_BRUSH); 
        } 
        return hbr; 
    }
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  2. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Problem with static text in dialog boxes
    By Clyde in forum Windows Programming
    Replies: 11
    Last Post: 05-28-2002, 12:51 PM
  5. changing static text
    By codec in forum Windows Programming
    Replies: 2
    Last Post: 03-23-2002, 09:45 PM