How to get the current Gui font w/o creating a control?

Get help with using AutoHotkey and its commands and hotkeys
just me
Posts: 6464
Joined: 02 Oct 2013, 08:51
Location: Germany

How to get the current Gui font w/o creating a control?

06 Oct 2013, 04:22

If you want to create owner-drawn controls, you have to process the WM_MEASUREITEM message and set the item height.
MSDN wrote:The system sends the WM_MEASUREITEM message to the owner window of combo boxes and list boxes created with the OWNERDRAWFIXED style before sending the WM_INITDIALOG message. As a result, when the owner receives this message, the system has not yet determined the height and width of the font used in the control; function calls and calculations requiring these values should occur in the main function of the application or library.
Does anybody know a way to get the current Gui control font without creating a control, as this might interfere with relative control positioning? (I've been asking this in 2009 already, but without success).
User avatar
SKAN
Posts: 379
Joined: 29 Sep 2013, 16:58

Re: How to get the current Gui font w/o creating a control?

06 Oct 2013, 04:54

Code: Select all

#NoEnv  
#SingleInstance, Force

GuiDefaultFont() {        ; By SKAN www.autohotkey.com/forum/viewtopic.php?p=465438#465438
 hFont := DllCall( "GetStockObject", UInt,17 ) ; DEFAULT_GUI_FONT
 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 )
Return DllCall( "MulDiv",Int,&LF+28, Int,1,Int,1, Str )
     , DllCall( "SetLastError", UInt,S )
}

A_GuiFont     := GuiDefaultFont()
A_GuiFontSize := A_LastError

MsgBox, %A_GuiFont%`t%A_GuiFontSize%
:)
just me
Posts: 6464
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to get the current Gui font w/o creating a control?

06 Oct 2013, 06:00

THX SKAN, but it seems I have to be more clear:

I want to retrieve the current Gui control font as set with Gui, Font, ..., ....

BTW: Would someone please increase the font size of inline code?
User avatar
SKAN
Posts: 379
Joined: 29 Sep 2013, 16:58

Re: How to get the current Gui font w/o creating a control?

06 Oct 2013, 10:31

just me wrote:retrieve the current Gui control font as set with Gui, Font, ..., ....
That is not easy, I guess. Here is an idea:

Fetch a list of all controls in GUI and reverse it so that latest created control is on top.
Find the font used for each control and if none is found use default Gui font.

Code: Select all

#NoEnv  
SetWorkingDir %A_ScriptDir%  
#SingleInstance, Force
#Persistent

Gui, Font, S10, Arial
Gui, Add, Text,, Line1
Gui, Font, S9, Tahoma
Gui, Add, Text,, Line2
Gui, Font, S8, Courier New
Gui, Add, Text,, Line3
Gui, Font, S7, Times New Roman
Gui, Add, Picture, wp hp, C:\NonExistentFile
Gui, Font, S6, NonExistentFont
Gui, Add, Text,, Line4

Gui, Show, w100 

Gui +LastFound
hWnd := WinExist()

WinGet, CHL, ControlListHwnd, ahk_id %hWnd% ; List of all Controls in GUI
Loop, Parse, CHL, `n                        ; Reverse the order of Controls so that
 CH := A_LoopField "`n" CH                  ; the lastest created control is on top    
StringTrimRight, CH, CH, 1


Loop, Parse, CH, `n
 {
  hFont := DllCall( "SendMessage", UInt,A_LoopField, UInt,0x31, UInt,0, UInt,0, UInt )
  If ( hFont )
     MsgBox % GetFontname( hFont ) "`t" A_LastError
 }
Return


GetFontname( hFont ) {
 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 )
Return DllCall( "MulDiv",Int,&LF+28, Int,1,Int,1, Str )
     , DllCall( "SetLastError", UInt,S )
}
User avatar
SKAN
Posts: 379
Joined: 29 Sep 2013, 16:58

Re: How to get the current Gui font w/o creating a control?

06 Oct 2013, 10:44

Ahh! This method would not work if Gui,Font was called but no control was created! :(
User avatar
jballi
Posts: 568
Joined: 29 Sep 2013, 17:34

Re: How to get the current Gui font w/o creating a control?

14 Oct 2013, 09:11

just me wrote:I want to retrieve the current Gui control font as set with Gui, Font, ..., ....
This information is not available. As SKAN showed, the font information can be derived if a control was created after the font was set and you know which control it was. Otherwise, there is no way to know.

This sounds like something to add to the Wish List forum. I'm thinking a request for a A_LastFont system variable that would return 0 if no fonts have been created, otherwise the handle to the last logical font that was created or referred to via the gui Font command would be returned.
HotKeyIt
Posts: 1866
Joined: 29 Sep 2013, 18:35
Contact:

Re: How to get the current Gui font w/o creating a control?

14 Oct 2013, 09:47

just me wrote:BTW: Would someone please increase the font size of inline code?
How about that?

Return to “Ask For Help”

Who is online

Users browsing this forum: Enlprck, Google [Bot], potroveio, sinkfaze and 147 guests