dimmg
dimmg

Reputation: 3815

Change text color on button push WIN32

How do I change the text color from an edit box on button push? (Win32/C++). I know how to change the text font (i.e. to use in WM_COMMAND, SendMessage() with WM_SETFONT).
On changing text color I think that I need an interaction betweenWM_COMMAND, WM_CTLCOLOREDIT, and SendMessage() but don't know with what kind of parameter . Thank you.

I've figured how to do this on single button. One more question please. If I use the code above for 3 different buttons, it doesn't behave as expected . There is a snippet :

case IDC_BUTTON3:
{
    textFlagRed = textFlagRed;
    textFlagBlue = !textFlagBlue;
    textFlagGreen = !textFlagGreen;
    InvalidateRect(textArea2, NULL, TRUE);
    break;
}

case IDC_BUTTON4:
{
    textFlagGreen = textFlagGreen;
    textFlagBlue = !textFlagBlue;
    textFlagRed = !textFlagRed;
    InvalidateRect(textArea2, NULL, TRUE);
    break;
}

case IDC_BUTTON5:
{
    textFlagBlue = textFlagBlue;
    textFlagRed = !textFlagRed;
    textFlagGreen = !textFlagGreen;
    InvalidateRect(textArea2, NULL, TRUE);
    break;
}

and in WM_CTLCOLORSTATIC

if (textFlagRed && (HWND)lParam == textArea2)
{   
    HBRUSH hbr = (HBRUSH) DefWindowProc(hwnd, message, wParam, lParam);
    SetTextColor((HDC) wParam, RGB(255, 0, 0));
    return (BOOL) hbr;
}
else if (textFlagBlue && (HWND)lParam == textArea2)
{   
    HBRUSH hbr = (HBRUSH) DefWindowProc(hwnd, message, wParam, lParam);
    SetTextColor((HDC) wParam, RGB(0, 0, 255));
    return (BOOL) hbr;
}
else if (textFlagGreen && (HWND)lParam == textArea2)
{   
    HBRUSH hbr = (HBRUSH) DefWindowProc(hwnd, message, wParam, lParam);
    SetTextColor((HDC) wParam, RGB(0, 255, 0));
    return (BOOL) hbr;
} 
break;

Always is the blue color.

Upvotes: 3

Views: 5947

Answers (2)

Edward Clements
Edward Clements

Reputation: 5132

You need to

a) a global boolean to indicate if the colour needs to be chaanged (say bEditRed)

b) on button push: set/toggle bEditRed and invalidate the edit box InvalidateRect(hWndEdit, NULL, TRUE)

c) handle the `WM_CTLCOLOREDIT' message in your dialog proc:

case WM_CTLCOLOREDIT:
{   if (bEditRed && (HWND)lParam == hWndEdit)
    {   HBRUSH hbr = (HBRUSH) DefWindowProc(hDlg, iMessage, wParam, lParam);
        SetTextColor((HDC) wParam, RGB(255, 0, 0));
        return (BOOL) hbr;
    }
    return FALSE;
}

Upvotes: 3

aquirdturtle
aquirdturtle

Reputation: 2195

An alternative to Edward's answer is to use

RedrawWindow(windowHandle, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);

instead of

InvalidateRect(windowHandle, NULL, TRUE)

The former will immediately redraw your window, while the latter will not redraw it until the main window is available again.

Upvotes: 0

Related Questions