简体   繁体   中英

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 (ie to use in WM_COMMAND , SendMessage() with WM_SETFONT ).
On changing text color I think that I need an interaction between WM_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.

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;

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.

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