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

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
jly
Posts: 89
Joined: 30 Sep 2020, 06:06

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

Post by jly » 05 Feb 2023, 21:55

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
}


User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

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

Post by kczx3 » 06 Feb 2023, 09:05

Please post your attempt at converting it to v2 so that we can provide feedback. I think that would provide a better learning opportunity.

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

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

Post by swagfag » 11 Feb 2023, 18:55

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

Post Reply

Return to “Ask for Help (v2)”