How to set default font for all the windows in a W

2019-01-27 15:43发布

I want all the controls (edit,list control, etc) in my application to be having the same font which is not the system default. How do i do this? Is there any Win32 API that sets the application default font?

6条回答
男人必须洒脱
2楼-- · 2019-01-27 15:55

Windows does not provide any mechanism for an application-wide font. Each window class may have its own behavior for choosing a font to use by default. It may try to select the font used by Windows shell dialogs, or it may simply draw its text using the horrid bitmap 'system' font automatically selected into new DCs.

The Windows common control window classes all respond to WM_SETFONT, which is the standard window message for telling a window what font you want it to use. When you implement your own window classes (especially new child control window classes), you should also write a handler for WM_SETFONT:

  1. If your window class has any child windows, your WM_SETFONT handler should forward the message to each of them.
  2. If your window class does any custom drawing, make sure to save the HFONT you receive in your WM_SETFONT handler and select it into the DC you use when drawing your window.
  3. If your window class is used as a top-level window, it will need logic to choose its own font, since it will have no parent window to send it a WM_SETFONT message.

Note that the dialog manager does some of this for you; when instantiating a dialog template, the new dialog's font is set to the font named in the template, and the dialog sends WM_SETFONT all of its child controls.

查看更多
戒情不戒烟
3楼-- · 2019-01-27 15:55

Yes you can !

HFONT defaultFont;
defaultFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
SendMessage(handlerControl, WM_SETFONT, WPARAM (defaultFont), TRUE); // Send this to each control
查看更多
爷的心禁止访问
4楼-- · 2019-01-27 15:58

Implement this:

    bool CALLBACK SetFont(HWND child, LPARAM font){
        SendMessage(child, WM_SETFONT, font, true);
        return true;
    }

inside a separate file or just in the main.cpp and then just run:

EnumChildWindows(hwnd, (WNDENUMPROC)SetFont, (LPARAM)GetStockObject(DEFAULT_GUI_FONT));

whenever you want, for example in the WM_CREATE message, after you've created all your child windows!

I always have a SetFont.cpp and a SetFont.h in my win32 GUI application solutions.

查看更多
你好瞎i
5楼-- · 2019-01-27 16:07

You can set the font for each Dialog box through the resources view. Right click on a dialog (not on other control), select properties and the font option.

查看更多
一纸荒年 Trace。
6楼-- · 2019-01-27 16:15

A handy method for setting the font for all child windows in one call:

SendMessageToDescendants( WM_SETFONT, 
                          (WPARAM)m_fntDialogFont.GetSafeHandle(), 
                          0 ); 
查看更多
Emotional °昔
7楼-- · 2019-01-27 16:15

You can't, there is no way to do this for all controls at the same time. You'll need to set it through the resource editor, as was suggested before, or call SetFont() manually on every control.

查看更多
登录 后发表回答