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

前端 未结 3 2151
无人及你
无人及你 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: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.

提交回复
热议问题