How can I specify a font for a window created through CreateWindow?

前端 未结 3 2165
无人及你
无人及你 2021-02-19 04:15

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?

3条回答
  •  误落风尘
    2021-02-19 04:50

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

    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.

提交回复
热议问题