我如何为通过CreateWindow创建的窗口指定字体? [英] How can I specify a font for a window created through CreateWindow?

查看:196
本文介绍了我如何为通过CreateWindow创建的窗口指定字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用纯Win32 API(RegisterClass和CreateWindow函数)创建窗口。如何为窗口指定一个字体而不是系统定义的字体?

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?

推荐答案

当您创建自己的窗口类时,负责自己管理字体。这个任务将有四个部分:

When you create your own window class, you are responsible for managing the font yourself. This task will have four parts:


  1. 当窗口被创建时(即当你处理 WM_CREATE ),请使用 CreateFont() CreateFontIndirect()获取你想在窗口中使用的字体的HFONT。您将需要存储此HFONT以及您为窗口类的每个实例保留的其他数据。您可以选择让窗口类处理 WM_GETFONT WM_SETFONT ,但它通常不是(如果你正在创建一个控件窗口类,你将需要处理WM_SETFONT,因为对话管理器发送该消息)。
  2. 如果你的窗口有任何子窗口包含文本,每当您的窗口的字体更改发送每个WM_SETFONT消息。所有常见的Windows控件都处理WM_SETFONT。
  3. 当您绘制窗口的内容时(通常是响应 WM_PAINT 消息),选择你的HFONT到 SelectObject /msdn.microsoft.com/en-us/library/ms534026.aspxrel =noreferrer> GetTextMetrics())。

  4. 当窗口被销毁时当您处理 WM_DESTROY )时,请使用 DeleteObject()释放您在步骤1中创建的字体。请注意,如果您选择在窗口中处理WM_SETFONT,请不要删除字体对象y您可以在您的WM_SETFONT处理程序中接收,因为发送该消息的代码希望保留该句柄的所有权。

  1. 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_GETFONT and WM_SETFONT as 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).
  2. 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.
  3. When you draw the contents of your window (typically in response to a WM_PAINT message), select your HFONT into the device context with the SelectObject() function before drawing text (or using text functions such as or GetTextMetrics()).
  4. 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 not delete a font object you receive in your WM_SETFONT handler, as the code that sent the message expects to retain ownership of that handle.

这篇关于我如何为通过CreateWindow创建的窗口指定字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆