windows 如何为 Win32 应用程序中的所有窗口设置默认字体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/938216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set default font for all the windows in a Win32 Application?
提问by Canopus
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?
我希望我的应用程序中的所有控件(编辑、列表控件等)都具有相同的字体,这不是系统默认的字体。我该怎么做呢?是否有任何设置应用程序默认字体的 Win32 API?
采纳答案by Matthew Xavier
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.
Windows 不为应用程序范围的字体提供任何机制。每个窗口类可能有自己的行为来选择默认使用的字体。它可能会尝试选择 Windows shell 对话框使用的字体,或者它可能只是使用自动选择到新 DC 中的可怕位图“系统”字体来绘制其文本。
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
:
Windows 公共控件窗口类都响应WM_SETFONT
,这是告诉窗口您希望它使用什么字体的标准窗口消息。当您实现自己的窗口类(尤其是新的子控件窗口类)时,您还应该为 编写一个处理程序WM_SETFONT
:
- If your window class has any child windows, your
WM_SETFONT
handler should forward the message to each of them. - 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. - 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.
- 如果您的窗口类有任何子窗口,您的
WM_SETFONT
处理程序应该将消息转发给它们中的每一个。 - 如果您的窗口类进行任何自定义绘图,请确保保存您在
WM_SETFONT
处理程序中收到的 HFONT,并将其选择到您在绘制窗口时使用的 DC 中。 - 如果您的窗口类用作顶级窗口,则需要选择自己的字体的逻辑,因为它没有父窗口来向其发送
WM_SETFONT
消息。
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.
请注意,对话管理器会为您完成其中的一些工作;实例化对话框模板时,新对话框的字体设置为模板中命名的字体,对话框发送WM_SETFONT
其所有子控件。
回答by Christopher Janzon
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:
在单独的文件中或仅在 main.cpp 中,然后运行:
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!
无论何时,例如在WM_CREATE
消息中,在您创建了所有子窗口之后!
I always have a SetFont.cpp
and a SetFont.h
in my win32 GUI application solutions.
我的 win32 GUI 应用程序解决方案中总是有一个SetFont.cpp
和一个SetFont.h
。
回答by Péa
Yes you can !
是的你可以 !
HFONT defaultFont;
defaultFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
SendMessage(handlerControl, WM_SETFONT, WPARAM (defaultFont), TRUE); // Send this to each control
回答by Alan
A handy method for setting the font for all child windows in one call:
一种在一次调用中为所有子窗口设置字体的便捷方法:
SendMessageToDescendants( WM_SETFONT,
(WPARAM)m_fntDialogFont.GetSafeHandle(),
0 );
回答by Roel
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.
您不能,无法同时对所有控件执行此操作。您需要通过资源编辑器设置它,就像之前建议的那样,或者在每个控件上手动调用 SetFont()。
回答by Nick Dandoulakis
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.
您可以通过资源视图为每个对话框设置字体。右键单击对话框(而不是其他控件),选择属性和字体选项。