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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top