TimRude Posted February 23 Share Posted February 23 (edited) When using the GUICtrlSetFont (or GUISetFont), different results occur if you use Default vs "" vs omitting the fontname parameter. When a control is created and no GUICtrlSetFont command is issued for it (and no GUISetFont setting is in place), the font used is the logical font 'MS Shell Dlg'. This translates (on my Windows 10 system) to the 'Microsoft Sans Serif' font, although this can evidently be changed via the registry settings at 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontSubstitutes'. If GUICtrlSetFont is used and the Default keyword is used for the fontname parameter, the same thing occurs (i.e. 'MS Shell Dlg' getting translated into 'Microsoft Sans Serif'). However, if GUICtrlSetFont is used and you specify "" as the fontname parameter, the fontname is set to blank. That gets translated by the system to a different font than 'Microsoft Sans Serif'. On my system, it evidently translates to 'Arial' and changes the Quality setting to 255. (I determined it was "Arial" by trying different fonts in the fontname parameter until I got the one that matched the appearance.) If you use GUICtrlSetFont and you don't specify a fontname parameter at all [i.e. you only specify the font size for example, as in GUICtrlSetFont(-1, 10)], it also evidently translates to 'Arial' but it changes the Quality setting to 2. Here's a test script illustrating this, followed by a screenshot of the results: expandcollapse popup#NoTrayIcon #include <APIConstants.au3> #include <WinAPIGdiDC.au3> #include <SendMessage.au3> #include <Array.au3> Local $sText = "AutoIt v3 is a freeware BASIC-like scripting language designed for " & _ "automating the Windows GUI and general scripting. It uses a combination of " & _ "simulated keystrokes, mouse movement and window/control manipulation in order " & _ "to automate tasks in a way not possible or reliable with other languages (e.g. " & _ "VBScript and SendKeys). AutoIt is also very small, self-contained and will run " & _ "on all versions of Windows out-of-the-box with no annoying 'runtimes' required!" & _ @CRLF & @CRLF & _ "AutoIt was initially designed for PC 'roll out' situations to reliably automate " & _ "and configure thousands of PCs. Over time it has become a powerful language that " & _ "supports complex expressions, user functions, loops and everything else that " & _ "veteran scripters would expect." GUICreate("Test GUI", 400, 550) GUICtrlCreateLabel($sText, 10, 10, 380, 125) GUICtrlSetBkColor(-1, 0xDDDDFF) ConsoleWrite("Label 1 (no GUICtrlSetFont used at all):" & @TAB) Local $aFont = _GUICtrlGetFont(-1) Local $iSize = $aFont ; remember the size for use in the rest of the labels GUICtrlCreateLabel($sText, 10, 145, 380, 125) GUICtrlSetBkColor(-1, 0xFFDDFF) GUICtrlSetFont(-1, $iSize, Default, Default, Default, Default) ConsoleWrite("Label 2 (using 'Default' for font name):" & @TAB) _GUICtrlGetFont(-1) GUICtrlCreateLabel($sText, 10, 280, 380, 125) GUICtrlSetBkColor(-1, 0xFFFFDD) GUICtrlSetFont(-1, $iSize, Default, Default, "", Default) ConsoleWrite('Label 3 (specifying "" for font name): ' & @TAB) _GUICtrlGetFont(-1) GUICtrlCreateLabel($sText, 10, 415, 380, 125) GUICtrlSetBkColor(-1, 0xDDFFDD) GUICtrlSetFont(-1, $iSize) ConsoleWrite("Label 4 (omitting font name parameter): " & @TAB) _GUICtrlGetFont(-1) GUISetState(@SW_SHOW) While GUIGetMsg() <> $GUI_EVENT_CLOSE WEnd Exit Func _GUICtrlGetFont($id_or_hWnd) ; accepts either Control ID or hWnd ; If successful, returns 1D array consisting of: ; ;  - Size ;  - Weight ;  - Attribute ;  - Name ;  - Quality ; ; If unsuccessful, returns 0 with @error set to non-zero If IsHWnd($id_or_hWnd) Then Local $hWnd = $id_or_hWnd Else Local $hWnd = GUICtrlGetHandle($id_or_hWnd) EndIf If $hWnd = 0 Then Return SetError(1, 1, 0) Local $hDC = _WinAPI_GetDC($hWnd) Local $iLPY = _WinAPI_GetDeviceCaps($hDC, $LOGPIXELSY) _WinAPI_ReleaseDC($hWnd, $hDC) Local $tFont = DllStructCreate($tagLOGFONT) Local $hFont = _SendMessage($hWnd, $WM_GETFONT) If @error Then Return SetError(@error, 2, 0) Local $iRet = _WinAPI_GetObject($hFont, DllStructGetSize($tFont), $tFont) If $iRet <> 0 Then Local $aFont $aFont = Abs(Round(DllStructGetData($tFont, 'Height') / $iLPY * 72, 1)) $aFont = DllStructGetData($tFont, 'Weight') $aFont = BitOR( _ ((DllStructGetData($tFont, "Italic")) ? $GUI_FONTITALIC : 0), _ ((DllStructGetData($tFont, "Underline")) ? $GUI_FONTUNDER : 0), _ ((DllStructGetData($tFont, "Strikeout")) ? $GUI_FONTSTRIKE : 0)) $aFont = StringStripWS(DllStructGetData($tFont, 'FaceName'), BitOR($STR_STRIPLEADING, $STR_STRIPTRAILING)) $aFont = DllStructGetData($tFont, 'Quality') ConsoleWrite("Size = " & $aFont & @TAB & _ "Weight = " & $aFont & @TAB & _ "Attrib = " & $aFont & @TAB & _ "Font Name = " & $aFont & @TAB & _ "Quality = " & $aFont & @CRLF) Return $aFont Else Return SetError(1, 3, 0) EndIf EndFunc ;==>_GUICtrlGetFont Why is this an issue? Well, consider someone creating a GUI and using the standard font settings. But on some of the controls they want to use bold, underlining, italics, etc. If they have controls created with no GUISetFont or GUICtrlSetFont modifying them, the font on those controls will be Microsoft Sans Serif. But if they then intend to change a control's text to bold print using something like GUICtrlSetFont(-1, Default, $FW_BOLD) they just unwittingly also changed the font on that control to the Arial typeface. Anyone else ever noticed this? Edited February 23 by TimRude Link to comment Share on other sites More sharing options...
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!Register a new account
Already have an account? Sign in here.Sign In Now