BWHazel
BWHazel

Reputation: 1474

In Win32, how can the colour of STATIC text be changed?

I have some static text on a window which is black, and I would like to change it at runtime using a Colour Chooser dialog. I have successfully obtained the colour, now I just need to change the text! I have read that the WM_CTLCOLORSTATIC message is the one to handle, although this appears to only be for when the control is initially drawn to the screen. Could WM_PAINT be used?

The code for setting the colour I have so far is (in WM_CTLCOLORSTATIC case):

if ((HWND)lParam == GetDlgItem(hWnd, uLabelId)
{
    SetBkMode((HDC)wParam, TRANSPARENT);
    SetTextColor((HDC)wParam, crLabelTextColour);
    return (BOOL)CreateSolidBrush(GetSysColor(COLOR_MENU));
}

I may have got entirely the wrong end of the stick approaching this issue, so any assistance would be greatly appreciated!

Upvotes: 2

Views: 3005

Answers (2)

RichieHindle
RichieHindle

Reputation: 281485

A few suggestions:

  • Are you aware that it's the parent dialog that receives WM_CTLCOLORSTATIC, not the control itself?

  • The return (BOOL)... looks weird - in which function does this code live? You should be returning an LRESULT from your window proc.

  • You're leaking a brush every time - you should maintain a single brush and return that brush each time. Windows doesn't clean up the brush.

Upvotes: 0

Adrian McCarthy
Adrian McCarthy

Reputation: 47962

You're on the right track with WM_CTLCOLORSTATIC. If you want to change the color, then invalidate the control (probably by using RedrawWindow). That will cause it to be repainted. When it's repainted, your WM_CTLCOLORSTATIC handler will be invoked again.

Upvotes: 6

Related Questions