3

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.

2
  • I can't really see anything blatantly wong with the logic -- try putting in debug statements and/or breakpoints to see the values of variables and why it is always showing blue? Commented Mar 24, 2014 at 12:16
  • That was my mess , variables were not declared as static. Thanks for reply. Commented Mar 24, 2014 at 22:36

2 Answers 2

3

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; } 
Sign up to request clarification or add additional context in comments.

2 Comments

Actually the text is red from the begining.
@rhauder_ - indeed. Just create a variable that will serve as a boolean flag. If flag set and if ((HWND)lParam == hWndEdit) then you run the code. Just set/toggle/clear the flag in your button handler, then invalidate the edit control.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.