Skip to content

Instantly share code, notes, and snippets.

@maul-esel
Created October 4, 2012 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maul-esel/3835024 to your computer and use it in GitHub Desktop.
Save maul-esel/3835024 to your computer and use it in GitHub Desktop.
A function to retrieve the font used by a control (if the control supports WM_GETFONT)
GetFont(hwnd, byRef font, byRef options)
{
static WM_GETFONT := 0x0031
, HWND_DESKTOP := 0
, HDC_DESKTOP := 0
, sizeof_LOGFONT := 28 + 32 * (A_IsUnicode ? 2 : 1)
, LOGPIXELSY := 90
SendMessage WM_GETFONT, 0, 0,, ahk_id %hwnd%
if (!ErrorLevel || ErrorLevel = "FAIL")
return false
hFont := ErrorLevel
VarSetCapacity(LOGFONT, sizeof_LOGFONT, 0)
if (!DllCall("Gdi32\GetObject", "Ptr", hFont, "Int", sizeof_LOGFONT, "Ptr", &LOGFONT, "Int"))
return false
font := StrGet(&LOGFONT + 28, 32)
, options := ""
if (NumGet(LOGFONT, 20, "Char"))
options .= " italic"
if (NumGet(LOGFONT, 21, "Char"))
options .= " underline"
if (NumGet(LOGFONT, 22, "Char"))
options .= " strike"
weight := NumGet(LOGFONT, 16, "Int")
if (weight == 700)
options .= " bold"
else if (weight != 400) ; ignore normal font weight
options .= " w" weight
quality := NumGet(LOGFONT, 26, "Char")
, options .= " q" quality
lfHeight := NumGet(LOGFONT, 0, "Int")
if (!HDC_DESKTOP)
HDC_DESKTOP := DllCall("GetDC", "Ptr", HWND_DESKTOP, "Ptr")
if (!HDC_DESKTOP)
return false
pixels := DllCall("Gdi32\GetDeviceCaps", "Ptr", HDC_DESKTOP, "Int", LOGPIXELSY, "Int")
if (!pixels)
return false
size := -DllCall("MulDiv", "Int", lfHeight, "Int", 72, "Int", pixels, "Int")
options .= " s" size
options := Trim(options)
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment