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

前端 未结 3 2147
无人及你
无人及你 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.
    0 讨论(0)
  • 2021-02-19 04:55

    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.

    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.

    0 讨论(0)
  • 2021-02-19 04:56

    As vividos said just use 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_SETFONT message:

    SendMessage(window, WM_SETFONT, hFont, TRUE);
    
    0 讨论(0)
提交回复
热议问题