简体   繁体   中英

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));

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM