Skip to content

Instantly share code, notes, and snippets.

@rossy
Last active April 29, 2016 23:14
Show Gist options
  • Save rossy/8a723e80c032dde741b6 to your computer and use it in GitHub Desktop.
Save rossy/8a723e80c032dde741b6 to your computer and use it in GitHub Desktop.
#include <windows.h>
#include <uxtheme.h>
#include <vssym32.h>
// window is a top-level HWND
static HFONT get_control_font_textstyle(void)
{
HTHEME textstyle = OpenThemeData(window, VSCLASS_TEXTSTYLE);
if (!textstyle)
return NULL;
LOGFONTW logfont;
HRESULT res = GetThemeFont(textstyle, NULL, TEXT_CONTROLLABEL, 0, TMT_FONT,
&logfont);
CloseThemeData(textstyle);
if (res != S_OK)
return NULL;
return CreateFontIndirectW(&logfont);
}
static HFONT get_control_font_sys(void)
{
HTHEME theme = OpenThemeData(window, VSCLASS_WINDOW);
if (!theme)
return NULL;
LOGFONTW logfont;
HRESULT res = GetThemeSysFont(theme, TMT_MENUFONT, &logfont);
CloseThemeData(theme);
if (res != S_OK)
return NULL;
return CreateFontIndirectW(&logfont);
}
static HFONT get_control_font_ncm(void)
{
NONCLIENTMETRICSW ncm = {
.cbSize = sizeof(NONCLIENTMETRICSW),
};
if (!SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0))
return NULL;
return CreateFontIndirectW(&ncm.lfMenuFont);
}
/* get_control_font - nothing is trivial in Windows, even (especially?) getting
the system font */
static HFONT get_control_font(void)
{
HFONT font;
/* Only Windows Vista and up have the TextStyle class. If the class is
missing, use the system menu font instead. */
if ((font = get_control_font_textstyle())) return font;
if ((font = get_control_font_sys())) return font;
/* If both UXTheme methods fail, try the old fashioned NONCLIENTMETRICS
structure */
if ((font = get_control_font_ncm())) return font;
return NULL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment