Page 1 of 1

how to get a gui control's font in v2 (Control_GetFont)?

Posted: 05 Feb 2023, 21:55
by jly
I found this function "Control_GetFont" in v1 to get a gui control's font name and size.

I tried to convert it to v2 but it didn't work!

How to contvert it to v2?


viewtopic.php?t=33049&start=20

Code: Select all


Control_GetFont(hwnd, byref FontName, byref FontSize) {
	SendMessage 0x31, 0, 0, , ahk_id %hwnd% ; WM_GETFONT
	IfEqual, ErrorLevel, FAIL, Return
	hFont := Errorlevel, VarSetCapacity(LF, szLF := 60 * (A_IsUnicode ? 2 : 1))
	DllCall("GetObject", UInt, hFont, Int, szLF, UInt, &LF)
	hDC := DllCall("GetDC", UInt,hwnd ), DPI := DllCall("GetDeviceCaps", UInt, hDC, Int, 90)
	DllCall("ReleaseDC", Int, 0, UInt, hDC), S := Round((-NumGet(LF, 0, "Int") * 72) / DPI)
	FontName := DllCall("MulDiv", Int, &LF + 28, Int, 1, Int, 1, Str)
	DllCall("SetLastError", UInt, S), FontSize := A_LastError
}


Re: how to get a gui control's font in v2 (Control_GetFont)?

Posted: 06 Feb 2023, 09:05
by kczx3
Please post your attempt at converting it to v2 so that we can provide feedback. I think that would provide a better learning opportunity.

Re: how to get a gui control's font in v2 (Control_GetFont)?

Posted: 11 Feb 2023, 18:55
by swagfag

Code: Select all

#Requires AutoHotkey v2.0.2

Control_GetFont(hwnd, &FontName, &FontSize) {
	hFont := SendMessage(0x31, 0, 0, hwnd) ; WM_GETFONT
	
	LOGFONTW := Buffer(92) ; sizeof LOGFONTW
	
	if !DllCall("GetObject", 'Ptr', hFont, 'Int', LOGFONTW.Size, 'Ptr', LOGFONTW)
		throw OSError('GetObject LOGFONTW failed')

	hDC := DllCall("GetDC", 'Ptr', hwnd, 'Ptr')
	DPI := DllCall("GetDeviceCaps", 'Ptr', hDC, 'Int', 90) ; LOGPIXELSY
	DllCall("ReleaseDC", 'Ptr', hwnd, 'Ptr', hDC)
	
	lfHeight := NumGet(LOGFONTW, 0, "Int")
	FontSize := Round((-lfHeight * 72) / DPI) ; its some documented BS on MSDN: https://learn.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-logfontw#members
	FontName := StrGet(NumGet(LOGFONTW, 28, 'Ptr')) ; LOGFONTW.lfFaceName
}
untested