Jump to content

_ChooseFontEx()


argumentum
 Share

Recommended Posts

This was born in a help topic but then I thought I'd be a good UDF to have.
The difference between _ChooseFont() and this _ChooseFontEx() , is that it adds:
  The use of keyword "Default", as default value.
  Hide parts of the font choosing GUI
  Uses the proper default color if no color is declared (default)

I tested this UDF to work with AutoIt from v3.2.12.1 and the current v3.3.16.1, so it should work in every version.
This also copied the Dll calls from other UDFs to this one, to give it independence from otherwise needed additional includes. 

The Example:

#include <_ChooseFontEx.au3>
#include <Array.au3> ; for the example

Example()

Func Example()
    Local $hParent = GUICreate("Test GUI", 300, 300, @DesktopWidth / 3)
    For $i = 1 To 7
        GUICtrlCreateLabel("label " & $i, 10, $i * 25)
    Next
    GUISetState()
    Sleep(1000) ; used "Arial" as it is found from WinXP to Win11
    Local $aFont = _ChooseFont("Arial", 8, 0, 0, False, False, False, $hParent)
    _ArrayDisplay($aFont, "Example original")
    $aFont = _ChooseFontEx("Arial", 8, Default, Default, Default, Default, Default, $hParent)
    _ArrayDisplay($aFont, "Example # 1")
    $aFont = _ChooseFontEx("Arial", 8, Default, 600, Default, Default, Default, $hParent, 63, "Please choose a font:")
    _ArrayDisplay($aFont, "Example # 2")
    Sleep(1000)
    GUIDelete()
EndFunc   ;==>Example

The UDF:

;~ Opt("MustDeclareVars", 1)
;~ #AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7

#include-once ; if you're gonna use it as UDF
#include <Misc.au3> ; for _ChooseFont()

;~ Global $__ChooseFontEx_Hook_WH_CBT = 5, $__ChooseFontEx_Hook_hProc = 0, $__ChooseFontEx_Hook_hHook = 0, _ ; move to "Global" to make it 3.2.12.1 compatible
;~      $__ChooseFontEx_Hook_iHideThese = 3, $__ChooseFontEx_Hook_sNewWinTitle = "", $__ChooseFontEx_Hook_wParamKept = 0, _
;~      $__ChooseFontEx_Default_iHideThese = 3, $__ChooseFontEx_Default_sNewWinTitle = "", $__ChooseFontEx_Default_hWndOwner = 0

; #FUNCTION# ====================================================================================================================
; Name...........: _ChooseFontEx
; Description ...: Creates a Font dialog box that enables the user to choose attributes for a logical font.
; Syntax.........: _ChooseFont([$sFontName = "Courier New"[, $iPointSize = 10[, $iColorRef = 0[, $iFontWeight = 0[, $iItalic = False[, $iUnderline = False[, $iStrikethru = False[, $hWndOwner = 0[, $iHideThese = 7[, $sNewWinTitle = ""]]]]]]]]]])
; Parameters ....: $sFontName    - Default font name
;                  $iPointSize   - Pointsize of font
;                  $iColorRef    - COLORREF rgbColors
;                  $iFontWeight  - Font Weight
;                  $iItalic      - Italic
;                  $iUnderline   - Underline
;                  $iStrikethru  - Optional: Strikethru
;                  $hWndOwnder   - Handle to the window that owns the dialog box
;                  $iHideThese   - Bitwise: Default is 3 ( SysLink and Script )
;                                  1: hide SysLink
;                                  2: hide Script
;                                  4: hide Color ( if 4 and 8 , also the GroupBox "Effects" will be hidden )
;                                  8: hide underline & strikeout
;                                 16: hide "Font style"
;                                 32: hide "Font size"
;                  $sNewWinTitle - Change the WinTitle
; Return values .: Success      - Array in the following format:
;                  |[0] - contains the number of elements
;                  |[1] - attributes = BitOr of italic:2, undeline:4, strikeout:8
;                  |[2] - fontname
;                  |[3] - font size = point size
;                  |[4] - font weight = = 0-1000
;                  |[5] - COLORREF rgbColors
;                  |[6] - Hex BGR Color
;                  |[7] - Hex RGB Color
;                  Failure      - -1
; Author ........: argumentum ; https://www.autoitscript.com/forum/topic/209809-_choosefontex/
; Modified.......:
; Remarks .......: Default keyword friendly. Dark Mode friendly.
; Related .......: _ChooseFont()
; Link ..........;
; Example .......; Yes
; ===============================================================================================================================
Func _ChooseFontEx($sFontName = "Courier New", $iPointSize = 10, $iFontColorRef = Default, $iFontWeight = 0, _
        $bItalic = False, $bUnderline = False, $bStrikethru = False, $hWndOwner = Default, $iHideThese = Default, $sNewWinTitle = Default)
    Local $aRet, $iErr, $iExt, $COLOR_BTNTEXT = 18
    If $iPointSize = Default Then $iPointSize = 10 ; Default as a parameter is not in the Misc.au3 UDF. Added here for convenience.
    If $iFontColorRef = Default Then
        $iFontColorRef = 0
        $aRet = DllCall("user32.dll", "INT", "GetSysColor", "int", $COLOR_BTNTEXT) ; I use a dark theme, so 0x000000 is not my default.
        If Not @error Then $iFontColorRef = $aRet[0]
    EndIf
    If $iFontWeight = Default Then $iFontWeight = 0
    If $bItalic = Default Then $bItalic = False
    If $bUnderline = Default Then $bUnderline = False
    If $bStrikethru = Default Then $bStrikethru = False
    If $hWndOwner = Default Then $hWndOwner = _ChooseFontEx_hWndOwner() ; default = 0 ( no parent gui )
    If Not IsHWnd($hWndOwner) Then $hWndOwner = 0
    If $iHideThese = Default Then $iHideThese = _ChooseFontEx_HideThese() ; default = 3 ( hide SysLink + hide Script )
    If $sNewWinTitle = Default Then $sNewWinTitle = _ChooseFontEx_NewWinTitle() ; default = "" ( no change )
    __ChooseFontEx_Hook("StartIt", $iHideThese, $sNewWinTitle) ; logic moved to this func to keep hook variables as local
    $aRet = _ChooseFont($sFontName, $iPointSize, $iFontColorRef, $iFontWeight, $bItalic, $bUnderline, $bStrikethru, $hWndOwner)
    $iErr = @error
    $iExt = @extended
    __ChooseFontEx_Hook("EndIt", "", "")
    Return SetError($iErr, $iExt, $aRet)
EndFunc   ;==>_ChooseFontEx

#Region helper func to set new defaults

; #FUNCTION# ====================================================================================================================
; Name...........: _ChooseFontEx_HideThese()
; Description ...: Set a default value / get value / set "ResetDefault" to Reset default value ( saves you from having to know it )
; ===============================================================================================================================
Func _ChooseFontEx_HideThese($iHideThese = Default) ; set a new default / get value
    Local $iRet = __ChooseFontEx_Hook("HideThese", $iHideThese, "")
    Return SetError(@error, @extended, $iRet)
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _ChooseFontEx_NewWinTitle()
; Description ...: Set a default WinTitle / get value / set "ResetDefault" to Reset default value ( saves you from having to know it )
; ===============================================================================================================================
Func _ChooseFontEx_NewWinTitle($sNewWinTitle = Default) ; 
    Local $sRet = __ChooseFontEx_Hook("NewWinTitle", $sNewWinTitle, "")
    Return SetError(@error, @extended, $sRet)
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _ChooseFontEx_hWndOwner()
; Description ...: Set a default parent GUI / get value / set "ResetDefault" to Reset default value ( saves you from having to know it )
; ===============================================================================================================================
Func _ChooseFontEx_hWndOwner($hWndOwner = Default)
    Local $hRet = __ChooseFontEx_Hook("hWndOwner", $hWndOwner, "")
    Return SetError(@error, @extended, $hRet)
EndFunc

#EndRegion

; #FUNCTION# ====================================================================================================================
; Name...........: __ChooseFontEx_Hook
; Description ...: Helper function for _ChooseFontEx()
; Remarks .......: For Internal Use Only
; ===============================================================================================================================
Func __ChooseFontEx_Hook($nCode, $wParam, $lParam) ; Tested in XP, 10 and 11
    Local Static $__ChooseFontEx_Hook_WH_CBT = 5, $__ChooseFontEx_Hook_hProc = 0, $__ChooseFontEx_Hook_hHook = 0, _ ; move to "Global" to make it 3.2.12.1 compatible
        $__ChooseFontEx_Hook_iHideThese = 3, $__ChooseFontEx_Hook_sNewWinTitle = "", $__ChooseFontEx_Hook_wParamKept = 0, _
        $__ChooseFontEx_Default_iHideThese = 3, $__ChooseFontEx_Default_sNewWinTitle = "", $__ChooseFontEx_Default_hWndOwner = 0
    Local $aRet
    Switch $nCode
        Case "HideThese"
            If $wParam = "ResetDefault" Then $__ChooseFontEx_Default_iHideThese = 3
            If $wParam <> Default Then $__ChooseFontEx_Default_iHideThese = $wParam
            Return $__ChooseFontEx_Default_iHideThese
        Case "NewWinTitle"
            If $wParam = "ResetDefault" Then $__ChooseFontEx_Default_sNewWinTitle = ""
            If $wParam <> Default Then $__ChooseFontEx_Default_sNewWinTitle = $wParam
            Return $__ChooseFontEx_Default_sNewWinTitle
        Case "hWndOwner"
            If $wParam = "ResetDefault" Then $__ChooseFontEx_Default_hWndOwner = 0
            If $wParam <> Default And IsHWnd($wParam) Then $__ChooseFontEx_Default_hWndOwner = $wParam
            Return SetError(0, IsHWnd($__ChooseFontEx_Default_hWndOwner), $__ChooseFontEx_Default_hWndOwner)
        Case "StartIt"
            $__ChooseFontEx_Hook_sNewWinTitle = $lParam
            If $wParam < 0 Or $wParam > 63 Or $wParam = Default Then
                $__ChooseFontEx_Hook_iHideThese = 3
            Else
                $__ChooseFontEx_Hook_iHideThese = Int($wParam)
            EndIf
            $__ChooseFontEx_Hook_hProc = DllCallbackRegister("__ChooseFontEx_Hook", "int", "int;int;int")
            Local $hThreadId = DllCall("kernel32.dll", "dword", "GetCurrentThreadId")
            If Not @error Then
                $aRet = DllCall("user32.dll", "handle", "SetWindowsHookEx", "int", $__ChooseFontEx_Hook_WH_CBT, _
                        "ptr", DllCallbackGetPtr($__ChooseFontEx_Hook_hProc), "handle", 0, "dword", $hThreadId[0])
                If Not @error Then $__ChooseFontEx_Hook_hHook = $aRet[0]
            EndIf
            Return
        Case "EndIt"
            DllCall("user32.dll", "bool", "UnhookWindowsHookEx", "handle", $__ChooseFontEx_Hook_hHook)
            ;If @error Then ConsoleWrite('! @error ' & @ScriptLineNumber & @CRLF)
            DllCallbackFree($__ChooseFontEx_Hook_hProc)
            $__ChooseFontEx_Hook_hHook = 0
            $__ChooseFontEx_Hook_hProc = 0
            Return
    EndSwitch ; $nCode = 3 ( 1st control created in the canvas is the window, hance "wParamKept = $wParam" )
    If $nCode = 3 And $__ChooseFontEx_Hook_wParamKept = 0 Then $__ChooseFontEx_Hook_wParamKept = $wParam
    If $nCode = 9 And $__ChooseFontEx_Hook_wParamKept = 1 Then $__ChooseFontEx_Hook_wParamKept = 0
    If $nCode = 5 And $__ChooseFontEx_Hook_wParamKept = $wParam Then
        $__ChooseFontEx_Hook_wParamKept = 1
        Local $hWnd = HWnd($wParam)
        If BitAND($__ChooseFontEx_Hook_iHideThese, 1) Then ControlHide($hWnd, "", "SysLink1")     ; <A>Show more fonts</A>
        If BitAND($__ChooseFontEx_Hook_iHideThese, 2) Then
            ControlHide($hWnd, "", "Static7")     ; Script:
            ControlHide($hWnd, "", "ComboBox5")   ; Script comboBox
        EndIf
        If BitAND($__ChooseFontEx_Hook_iHideThese, 4) Then
            ControlHide($hWnd, "", "Static4")     ; Color:
            ControlHide($hWnd, "", "ComboBox4")   ; Color comboBox
        EndIf
        If BitAND($__ChooseFontEx_Hook_iHideThese, 8) Then
            ControlHide($hWnd, "", "Button2")     ; Strikeout:
            ControlHide($hWnd, "", "Button3")     ; Underline:
        EndIf
        If BitAND($__ChooseFontEx_Hook_iHideThese, 16) Then ; Font style:
            ControlHide($hWnd, "", "Static2")
            ControlHide($hWnd, "", "ComboBox2")
        EndIf
        If BitAND($__ChooseFontEx_Hook_iHideThese, 32) Then ; Size:
            ControlHide($hWnd, "", "Static3")
            ControlHide($hWnd, "", "ComboBox3")
        EndIf
        If BitAND($__ChooseFontEx_Hook_iHideThese, 4) And BitAND($__ChooseFontEx_Hook_iHideThese, 8) Then ControlHide($hWnd, "", "Button1") ; Effects:
        If $__ChooseFontEx_Hook_sNewWinTitle <> "" Then WinSetTitle($hWnd, "", $__ChooseFontEx_Hook_sNewWinTitle)
    EndIf
    $aRet = DllCall("user32.dll", "lresult", "CallNextHookEx", "handle", $__ChooseFontEx_Hook_hHook, "int", $nCode, "wparam", $wParam, "lparam", $lParam)
    If Not @error Then Return $aRet[0]
    Return 0
EndFunc   ;==>__ChooseFontEx_Hook

#cs

#include <Array.au3> ; for the example

Example()

Func Example()

    Local $hParent = GUICreate("Test GUI", 300, 300, @DesktopWidth / 3)
    For $i = 1 To 7
        GUICtrlCreateLabel("label " & $i, 10, $i * 25)
    Next
    GUISetState()
    Sleep(1000) ; used "Arial" as it is found from WinXP to Win11
    Local $aFont = _ChooseFont("Arial", 8, Default, 0, False, False, False, $hParent)
    _ArrayDisplay($aFont, "Example original")
    _ChooseFontEx_NewWinTitle("My default title")
    $aFont = _ChooseFontEx("Arial", 8, Default, Default, Default, Default, Default, $hParent)
    _ArrayDisplay($aFont, "Example # 1")
    $aFont = _ChooseFontEx("Arial", 8, Default, 600, Default, Default, Default, $hParent, 63, "Please choose a font:")
    _ArrayDisplay($aFont, "Example # 2")
    Sleep(1000)
EndFunc   ;==>Example

#ce

Talking to myself: I don't know if start annoying the MVPs into modifying the default ChooseFont() to include these features in it is well worth it. It should be but I don't wanna push the code uphill ...hmmm. They may just do it if they see this expansion as favorable. At least the default color should be implemented. idk.

If you're using this thing, please do leave a like, as to get an idea if is really useful or just not that useful. Thank you.

Edited by argumentum
better code

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

  • argumentum changed the title to _ChooseFontEx()

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

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...