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_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).
- 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_PAINT message), 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 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.