windows 如何为通过 CreateWindow 创建的窗口指定字体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/221411/
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 can I specify a font for a window created through CreateWindow?
提问by Matthew Xavier
I'm creating window using pure Win32 API (RegisterClass and CreateWindow functions). How can I specify a font for the window instead of system defined one?
我正在使用纯 Win32 API(RegisterClass 和 CreateWindow 函数)创建窗口。如何为窗口指定字体而不是系统定义的字体?
回答by Matthew Xavier
When you create your own window class, you are responsible for managing the font yourself. This task will have four parts:
当您创建自己的窗口类时,您有责任自己管理字体。此任务将分为四个部分:
- When the window is created (i.e. when you handle WM_CREATE), use CreateFont()or CreateFontIndirect()to obtain an HFONT for the font you want to use in the window. You will need to store this HFONT along with the other data you keep for each instance of the window class. You may choose to have your window class handle WM_GETFONTand WM_SETFONTas well, but it is generally not required for top-level windows (if you are creating a control window class, you will want to handle WM_SETFONT, since the dialog manager sends that message).
- If your window has any child windows that contain text, send each of them a WM_SETFONT message whenever your window's font changes. All of the common Windows controls handle WM_SETFONT.
- When you draw the contents of your window (typically in response to a WM_PAINTmessage), select your HFONT into the device context with the SelectObject()function before drawing text (or using text functions such as or GetTextMetrics()).
- When the window is destroyed (i.e. when you handle WM_DESTROY), use DeleteObject()to release the font you created in step 1. Note that if you choose to handle WM_SETFONT in your window, do notdelete a font object you receive in your WM_SETFONT handler, as the code that sent the message expects to retain ownership of that handle.
- 当窗口被创建时(即当你处理WM_CREATE 时),使用CreateFont()或CreateFontIndirect()来获取你想在窗口中使用的字体的 HFONT。您需要将此 HFONT 与为窗口类的每个实例保留的其他数据一起存储。您也可以选择让您的窗口类处理WM_GETFONT和WM_SETFONT,但顶级窗口通常不需要它(如果您正在创建一个控制窗口类,您将需要处理 WM_SETFONT,因为对话框管理器发送该消息)。
- 如果您的窗口有任何包含文本的子窗口,请在窗口的字体更改时向每个子窗口发送 WM_SETFONT 消息。所有常见的 Windows 控件都处理 WM_SETFONT。
- 当您绘制窗口的内容时(通常是响应WM_PAINT消息),在绘制文本(或使用诸如 或GetTextMetrics() 之类的文本函数)之前,使用SelectObject()函数将 HFONT 选择到设备上下文中。
- 当窗口被销毁时(即当您处理WM_DESTROY 时),使用DeleteObject()释放您在步骤 1 中创建的字体。请注意,如果您选择在窗口中处理 WM_SETFONT,请不要删除您在 WM_SETFONT 中收到的字体对象处理程序,因为发送消息的代码希望保留该句柄的所有权。
回答by Bob Jones
As vividos said just use CreateFont()/CreateFontIndirect:
正如 vividos 所说,只需使用CreateFont()/CreateFontIndirect:
HFONT hFont = CreateFont (13, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET,
OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_DONTCARE, TEXT("Tahoma"));
And then set this font for your window/control with the WM_SETFONTmessage:
然后使用WM_SETFONT消息为您的窗口/控件设置此字体:
SendMessage(window, WM_SETFONT, hFont, TRUE);
回答by vividos
In case you superclass a standard common control that already has its own font handle, use this approach: Just create a font using CreateFont
or CreateFontIndirect
and set it using WM_SETFONT
message (in MFC and ATL there would be a corresponding SetFont
function). When the font is no longer needed, destroy the font using DeleteObject
. Be sure to not destroy the window's previously set font.
如果您超类一个已经有自己的字体句柄的标准公共控件,请使用这种方法:只需使用CreateFont
或创建字体CreateFontIndirect
并使用WM_SETFONT
消息设置它(在 MFC 和 ATL 中会有相应的SetFont
函数)。当不再需要字体时,使用DeleteObject
. 确保不要破坏窗口先前设置的字体。
In case you're writing a custom control that draws itself, just create a new font object using CreateFont
or CreateFontIndirect
and store it in your class somewhere. If you want to support third-party users, handle WM_SETFONT
and WM_GETFONT
to let the user set another font. When painting, use the current font object stored in your class.
如果您正在编写一个自行绘制的自定义控件,只需使用CreateFont
或创建一个新的字体对象CreateFontIndirect
并将其存储在您的类中的某个位置。如果要支持第三方用户,处理WM_SETFONT
并WM_GETFONT
让用户设置另一种字体。绘制时,使用存储在类中的当前字体对象。