Home > Mobile >  Is there a way to change the color of the text in a ChooseFont() dialog in the Win32 API?
Is there a way to change the color of the text in a ChooseFont() dialog in the Win32 API?

Time:11-11

I am developing a basic text editor program with the Win32 API's file editor example, using Dev-C from bloodshed.net.

How can I change the text color when I select it on the ChooseFont() dialog? In that dialog, everything works except the color changing option.

Below is my code. Choose font dialog, and in the switch case I called that function from the menu format and font:

CHOOSEFONT cf = {sizeof(CHOOSEFONT)};
LOGFONT lf;
GetObject(g_hFont, sizeof(LOGFONT), &lf);
cf.Flags = CF_EFFECTS | CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
cf.hwndOwner = g_hwnd;
cf.lpLogFont = &lf;
cf.rgbColors = g_editcolor;
if (!ChooseFont(&cf))
    return;
HFONT hf = CreateFontIndirect(&lf);
if (hf)
{
    g_hFont = hf;
    SendMessage(g_hEdit, WM_SETFONT, (WPARAM)g_hFont, TRUE);
}
g_editcolor = cf.rgbColors;

CodePudding user response:

A font doesn't have a color. A device context has a color assigned which it uses when rendering text using a font.

To set a text color for a standard EDIT control, have the parent window handle the WM_CTLCOLOREDIT and WM_CTLCOLORSTATIC window messages:

An edit control that is not read-only or disabled sends the WM_CTLCOLOREDIT message to its parent window when the control is about to be drawn. By responding to this message, the parent window can use the specified device context handle to set the text and background colors of the edit control.

A static control, or an edit control that is read-only or disabled, sends the WM_CTLCOLORSTATIC message to its parent window when the control is about to be drawn. By responding to this message, the parent window can use the specified device context handle to set the text foreground and background colors of the static control.

Use SelectObject() to assign your HFONT to the provided HDC, and set the HDC's text color using SetTextColor().

  • Related