Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static control set text color

I have a static control:

HWND hLabelControl=CreateWindowEx(WS_EX_CLIENTEDGE,"STATIC","",
            WS_TABSTOP|WS_VISIBLE|WS_CHILD|SS_CENTER,0,0,24,24,
        hwnd,(HMENU)hS1,GetModuleHandle(NULL),NULL);

I want when a button is pressed the color of the text in the static label to change to red for example.

How can I do this?

I know there is a

SetTextColor(
  _In_  HDC hdc,
  _In_  COLORREF crColor
);

function but I can't figure out how to get the HDC of the static control.

Thanks in advance.

EDIT:

This doesn't work:

        HDC hDC=GetDC(hLabelControl);
        SetTextColor(hDC,RGB(255,0,0));
like image 946
Johnny Mnemonic Avatar asked Apr 11 '13 19:04

Johnny Mnemonic


1 Answers

Static controls send their parent a WM_CTLCOLORSTATIC message just before they paint themselves. You can alter the DC by handling this message.

case WM_CTLCOLORSTATIC:
  if (the_button_was_clicked) {
    HDC hdc = reinterpret_cast<HDC>(wParam);
    SetTextColor(hdc, COLORREF(0xFF, 0x00, 0x00));
  }
  return ::GetSysColorBrush(COLOR_WINDOW);  // example color, adjust for your circumstance

So the trick is to get the static control to repaint itself when the button is clicked. You can do this several different ways, but the simplest is probably to invalidate the window with InvalidateRect.

like image 74
Adrian McCarthy Avatar answered Nov 18 '22 22:11

Adrian McCarthy