Share via


How To Set Font Style Of An Edit Control ? (Win32 API)

Question

Monday, April 28, 2014 4:06 PM

How To Set Font Style And Color Of An Edit Control ? (Win32 API)

All replies (1)

Monday, April 28, 2014 6:37 PM âś…Answered | 1 vote

You can change the font of a control by sending the WM_SETFONT message to that control:

SendMessage(hEdit, WM_SETFONT, (WPARAM)hFont, TRUE);

As for color, that's a bit tricky. There's nothing like WM_SETCOLOR, to change the color of a edit control you have to handle the WM_CTLCOLOREDIT message in the parent of the edit control. The wParam parameter contains the HDC used by the edit control and you can change the color with SetTextColor:

case WM_CTLCOLOREDIT:
    SetTextColor((HDC)wParam, RGB(255, 0, 0));
    break;

The tricky part is that this will affect all child edit controls of the window. If you want to change the color of a specific edit control then you'll need to use the lParam parameter, it contains the HWND of the control. That should allow you to distinguish a particular edit control from all others.