Jump to content

How can I run ChooseFont dialog with custom Font.DLG using lpTemplateName?


Go to solution Solved by MattyD,

Recommended Posts

Posted

I'm trying to create a dark mode ChooseFont dialog. The initial hook proc with fnHook / CF_ENABLEHOOK has been successful. However, attempting to load ChooseFont dialog with a custom Font.DLG file has not been successful at all.

I have tried a few variations with $CF_ENABLETEMPLATE which failed and a few variations with $CF_ENABLETEMPLATEHANDLE that also failed.

If anyone is able to help with this, that would be fantastic. I searched back in the forum and could not find any usage of lpTemplateName with the ChooseFont dialog.

In the _ChooseFontHooked() function, I left a comment "Hook and template testing" on line 66 which is the start of the area testing hook and template stuff.

Thank you for your time. :)

Font.DLG

DARKFONTDLG DIALOG 13, 54, 243, 234
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU |
      DS_3DLOOK
CAPTION "Font"
FONT 9, "Segoe UI"
BEGIN
    LTEXT           "&Font:", stc1, 7, 7, 98, 9
    COMBOBOX        cmb1, 7, 16, 98, 76,
                    CBS_SIMPLE | CBS_AUTOHSCROLL | CBS_DISABLENOSCROLL |
                    CBS_SORT | WS_VSCROLL | WS_TABSTOP | CBS_HASSTRINGS |
                    CBS_OWNERDRAWFIXED

    LTEXT           "Font st&yle:", stc2, 114, 7, 74, 9
    COMBOBOX        cmb2, 114, 16, 74, 76,
                    CBS_SIMPLE | CBS_AUTOHSCROLL | CBS_DISABLENOSCROLL |
                    WS_VSCROLL | WS_TABSTOP | CBS_HASSTRINGS |
                    CBS_OWNERDRAWFIXED

    LTEXT           "&Size:", stc3, 198, 7, 36, 9
    COMBOBOX        cmb3, 198, 16, 36, 76,
                    CBS_SIMPLE | CBS_AUTOHSCROLL | CBS_DISABLENOSCROLL |
                    CBS_SORT | WS_VSCROLL | WS_TABSTOP | CBS_HASSTRINGS

    GROUPBOX        "Effects", grp1, 7, 97, 98, 76, WS_GROUP
    AUTOCHECKBOX    "Stri&keout", chx1, 13, 111, 90, 10, WS_TABSTOP
    AUTOCHECKBOX    "&Underline", chx2, 13, 127, 90, 10

    LTEXT           "&Color:", stc4, 13, 144, 89, 9
    COMBOBOX        cmb4, 13, 155, 85, 100,
                    CBS_DROPDOWNLIST | CBS_OWNERDRAWFIXED | CBS_AUTOHSCROLL |
                    CBS_HASSTRINGS | WS_BORDER | WS_VSCROLL | WS_TABSTOP

    GROUPBOX        "Sample", grp2, 114, 97, 120, 43, WS_GROUP
    CTEXT           "AaBbYyZz", stc5, 116, 106, 117, 33,
                    SS_NOPREFIX | NOT WS_VISIBLE
    LTEXT           "", stc6, 7, 178, 227, 20, SS_NOPREFIX | NOT WS_GROUP

    LTEXT           "Sc&ript:", stc7, 114, 145, 118, 9
    COMBOBOX        cmb5, 114, 155, 120, 30, CBS_DROPDOWNLIST |
                    CBS_AUTOHSCROLL | CBS_HASSTRINGS |
                    WS_BORDER | WS_VSCROLL | WS_TABSTOP

    CONTROL         "<A>Show more fonts</A>", IDC_MANAGE_LINK, "SysLink",
                    WS_TABSTOP, 7, 199, 227, 9

    DEFPUSHBUTTON   "OK", IDOK, 141, 215, 45, 14, WS_GROUP
    PUSHBUTTON      "Cancel", IDCANCEL, 190, 215, 45, 14, WS_GROUP
    PUSHBUTTON      "&Apply", psh3, 92, 215, 45, 14, WS_GROUP
    PUSHBUTTON      "&Help", pshHelp, 43, 215, 45, 14, WS_GROUP
END

 

Testing script:

#include <WinAPIProc.au3>
#include <FontConstants.au3>
#include <StructureConstants.au3>
#include <GUIConstantsEx.au3>
#include <APIGdiConstants.au3>
#include <SendMessage.au3>
#include <WinAPISysWin.au3>
#include <WinAPITheme.au3>
#include <WindowsStylesConstants.au3>
#include <WinAPIGdiInternals.au3>
#include <WinAPIGdiDC.au3>
#include <AutoItConstants.au3>
#include <WindowsNotifsConstants.au3>
#include <WinAPIGdi.au3>
#include <Misc.au3>

; Initialize System DPI awareness
DllCall("user32.dll", "bool", "SetProcessDpiAwarenessContext", @AutoItX64 ? "int64" : "int", -2)

Global Const $CF_ENABLEHOOK = 8
Global Const $CF_ENABLETEMPLATE = 16
Global Const $CF_ENABLETEMPLATEHANDLE = 32

Global Const $sTemplate = @ScriptDir & "\font.dlg"
Global Const $sTemplateName = "DARKFONTDLG"
Global $hTemplate = FileOpen($sTemplate)

Global $g_hDialogProc = 0

_ChooseFontHooked()

FileClose($hTemplate)

Func _ChooseFontHooked($sFontName = "Courier New", $iPointSize = 10, $iFontColorRef = Default, $iFontWeight = 0, $bItalic = False, $bUnderline = False, $bStrikethru = False, $hWndOwner = 0)
    Local $iItalic = 0, $iUnderline = 0, $iStrikeout = 0, $iRetExtended = 0
    If $sFontName = Default Then $sFontName = "Courier New"
    If $iPointSize = Default Then $iPointSize = 10
    If $iFontColorRef = Default Then
        $iFontColorRef = 0
        Local $aRet = DllCall("user32.dll", "int", "GetSysColor", "int", 18) ; $COLOR_BTNTEXT
        If @error Then
            $iRetExtended = @error
        Else
            $iFontColorRef = $aRet[0]
        EndIf
    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 = 0
    $iFontColorRef = BitOR(BitShift(BitAND($iFontColorRef, 0x000000FF), -16), BitAND($iFontColorRef, 0x0000FF00), BitShift(BitAND($iFontColorRef, 0x00FF0000), 16))

    Local $hDC = __MISC_GetDC(0)
    Local $iHeight = Round(($iPointSize * __MISC_GetDeviceCaps($hDC, $LOGPIXELSX)) / 72, 0)
    __MISC_ReleaseDC(0, $hDC)

    Local $tChooseFont = DllStructCreate($tagCHOOSEFONT)
    Local $tLogFont = DllStructCreate($tagLOGFONT)

    DllStructSetData($tChooseFont, "Size", DllStructGetSize($tChooseFont))
    DllStructSetData($tChooseFont, "hWndOwner", $hWndOwner)
    DllStructSetData($tChooseFont, "LogFont", DllStructGetPtr($tLogFont))
    DllStructSetData($tChooseFont, "PointSize", $iPointSize)

    ; Hook and template testing
    $g_hDialogProc = DllCallbackRegister(_DialogProc, "int", "hwnd;uint;wparam;lparam")
    DllStructSetData($tChooseFont, 'fnHook', DllCallbackGetPtr($g_hDialogProc))
    ;DllStructSetData($tChooseFont, "Flags", BitOR($CF_SCREENFONTS, $CF_PRINTERFONTS, $CF_EFFECTS, $CF_INITTOLOGFONTSTRUCT, $CF_NOSCRIPTSEL, $CF_ENABLEHOOK)) ; <<< this works with hook but no custom template
    DllStructSetData($tChooseFont, "Flags", BitOR($CF_SCREENFONTS, $CF_PRINTERFONTS, $CF_EFFECTS, $CF_INITTOLOGFONTSTRUCT, $CF_NOSCRIPTSEL, $CF_ENABLEHOOK, $CF_ENABLETEMPLATE))
    ;DllStructSetData($tChooseFont, "Flags", BitOR($CF_SCREENFONTS, $CF_PRINTERFONTS, $CF_EFFECTS, $CF_INITTOLOGFONTSTRUCT, $CF_NOSCRIPTSEL, $CF_ENABLEHOOK, $CF_ENABLETEMPLATEHANDLE))
    ;DllStructSetData($tChooseFont, 'hInstance', $hTemplate)
    DllStructSetData($tChooseFont, 'hInstance', Null)
    Local $tagBuffer = "char[4096]", $iBufferSize = 4095
    ;If $fUnicode Then $tagBuffer = "w" & $tagBuffer
    Local $tTemplateName = DllStructCreate($tagBuffer);'char[256]')
    DllStructSetData($tTemplateName, 1, $sTemplateName)
    DllStructSetData($tChooseFont, 'lpTemplateName', DllStructGetPtr($tTemplateName))

    DllStructSetData($tChooseFont, "rgbColors", $iFontColorRef)
    DllStructSetData($tChooseFont, "FontType", 0)

    DllStructSetData($tLogFont, "Height", $iHeight)
    DllStructSetData($tLogFont, "Weight", $iFontWeight)
    DllStructSetData($tLogFont, "Italic", $bItalic)
    DllStructSetData($tLogFont, "Underline", $bUnderline)
    DllStructSetData($tLogFont, "Strikeout", $bStrikethru)
    DllStructSetData($tLogFont, "FaceName", $sFontName)

    Local $aCall = DllCall("comdlg32.dll", "bool", "ChooseFontW", "struct*", $tChooseFont)
    If @error Then Return SetError(@error, @extended, -2) ; ..to differentiate from the error below
    If $aCall[0] = 0 Then Return SetError(-3, $iRetExtended, -1) ; user selected cancel or struct settings incorrect

    Local $sFaceName = DllStructGetData($tLogFont, "FaceName")
    If StringLen($sFaceName) = 0 And StringLen($sFontName) > 0 Then $sFaceName = $sFontName

    If DllStructGetData($tLogFont, "Italic") Then $iItalic = 2
    If DllStructGetData($tLogFont, "Underline") Then $iUnderline = 4
    If DllStructGetData($tLogFont, "Strikeout") Then $iStrikeout = 8

    Local $iAttributes = BitOR($iItalic, $iUnderline, $iStrikeout)
    Local $iSize = DllStructGetData($tChooseFont, "PointSize") / 10
    Local $iColorRef = DllStructGetData($tChooseFont, "rgbColors")
    Local $iWeight = DllStructGetData($tLogFont, "Weight")

    Local $sColor_picked = Hex(String($iColorRef), 6)

    Return SetError(0, $iRetExtended, StringSplit($iAttributes & "," & $sFaceName & "," & $iSize & "," & $iWeight & "," & $iColorRef & "," & _
            '0x' & $sColor_picked & "," & '0x' & StringMid($sColor_picked, 5, 2) & StringMid($sColor_picked, 3, 2) & StringMid($sColor_picked, 1, 2), ","))
EndFunc   ;==>_ChooseFontHooked

Func _DialogProc($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam, $lParam
    Local Static $hBrush

    Switch $iMsg

        Case $WM_INITDIALOG
            _WinAPI_DwmSetWindowAttribute($hWnd, $DWMWA_USE_IMMERSIVE_DARK_MODE, True)
            $hBrush = _WinAPI_CreateSolidBrush(0x202020)

            Local $aCtrlsEx = _WinAPI_EnumProcessWindows(0, False) ; allows getting handles for tooltips_class32, ComboLBox, etc.
            If @error = 0 Then
                For $i = 1 To $aCtrlsEx[0][0]
                    ;If $aCtrlsEx[$i][1] = 'tooltips_class32' Then _GUIDarkTheme_GUICtrlSetDarkTheme($hGui, $aCtrlsEx[$i][0], $bEnableDarkTheme)
                    If $aCtrlsEx[$i][1] = 'ComboLBox' Then
                        _WinAPI_SetWindowTheme($aCtrlsEx[$i][0], "DarkMode_Explorer")
                        _SendMessage($aCtrlsEx[$i][0], $WM_THEMECHANGED, 0, 0)
                    EndIf
                Next
            EndIf

            ; Find all controls to apply dark theme
            Local $aCtrls = _WinAPI_EnumChildWindows($hWnd, False)
            If @error = 0 Then
                For $i = 1 To $aCtrls[0][0]
                    ;ConsoleWrite("class: " & $aCtrls[$i][1] & " hwnd: " & $aCtrls[$i][0] & @CRLF)
                    If $aCtrls[$i][1] = "Button" Then
                        _WinAPI_SetWindowTheme($aCtrls[$i][0], "DarkMode_Explorer")
                        ;_WinAPI_SetWindowTheme($aCtrls[$i][0], "DarkMode_DarkTheme") ; DarkMode_DarkTheme needed for group box
                        _SendMessage($aCtrls[$i][0], $WM_THEMECHANGED, 0, 0)
                    EndIf
                    If $aCtrls[$i][1] = "ComboBox" Then
                        _WinAPI_SetWindowTheme($aCtrls[$i][0], "DarkMode_CFD")
                        _SendMessage($aCtrls[$i][0], $WM_THEMECHANGED, 0, 0)
                    EndIf
                    If $aCtrls[$i][1] = "ComboLBox" Then
                        _WinAPI_SetWindowTheme($aCtrls[$i][0], "DarkMode_Explorer")
                        _SendMessage($aCtrls[$i][0], $WM_THEMECHANGED, 0, 0)
                    EndIf
                    If $aCtrls[$i][1] = "Edit" Then
                        _WinAPI_SetWindowTheme($aCtrls[$i][0], "DarkMode_Explorer")
                        _SendMessage($aCtrls[$i][0], $WM_THEMECHANGED, 0, 0)
                        _WinAPI_SetWindowLong($aCtrls[$i][0], $GWL_EXSTYLE, BitAND(_WinAPI_GetWindowLong($aCtrls[$i][0], $GWL_EXSTYLE), BitNOT($WS_EX_CLIENTEDGE)))
                        _WinAPI_SetWindowLong($aCtrls[$i][0], $GWL_EXSTYLE, BitOR(_WinAPI_GetWindowLong($aCtrls[$i][0], $GWL_EXSTYLE), $WS_EX_STATICEDGE))
                        _WinAPI_SetWindowPos($aCtrls[$i][0], 0, 0, 0, 0, 0, BitOR($SWP_NOMOVE, $SWP_NOSIZE, $SWP_NOZORDER, $SWP_FRAMECHANGED))
                    EndIf
                Next
            EndIf

        Case $WM_SHOWWINDOW
            ; center dialog on desktop
            Local $aChildPos = WinGetPos($hWnd)
            WinMove($hWnd, "", (@DesktopWidth / 2 - ($aChildPos[2] / 2)), (@DesktopHeight / 2 - ($aChildPos[3] / 2)))

        Case $WM_CTLCOLORSTATIC,  $WM_CTLCOLORDLG
            _WinAPI_SetBkMode($wParam, $TRANSPARENT)
            _WinAPI_SetTextColor($wParam, 0xFFFFFF)
            Return $hBrush

        Case $WM_CTLCOLOREDIT, $WM_CTLCOLORLISTBOX
            _WinAPI_SetBkMode($wParam, $TRANSPARENT)
            _WinAPI_SetTextColor($wParam, 0xFFFFFF)
            Return $hBrush

        Case $WM_CTLCOLORBTN
            _WinAPI_SetBkColor($wParam, 0x000000) ; needed for focus rectangle
            Return $hBrush

        Case $WM_DESTROY
            _WinAPI_DeleteObject($hBrush)

    EndSwitch
EndFunc

 

Posted (edited)

I suppose that you need to save font.dlg to the resource of the exe file. There is ResHacker project that might assit you into adding that template into exe file.

Edited by ahmet
Edit: For hInstance maybe this will be necessary: _WinAPI_GetModuleHandle(Null)
Posted
12 hours ago, ahmet said:

I suppose that you need to save font.dlg to the resource of the exe file.

Thanks for your response. Unfortunately I was really hoping to be able to achieve this without compiling to .dll or .exe binary. I'm starting to think that it might not be possible without compiling.

Posted
1 hour ago, ahmet said:

What is your goal?

I'm trying to replicate this C++ souce code in AutoIt:

	/**
	 * @brief Hook procedure for customizing common dialogs with dark mode.
	 *
	 * This function handles messages for all Windows common dialogs.
	 * When initialized (`WM_INITDIALOG`), it applies dark mode styling to the dialog.
	 *
	 * ## Special Case: Font Dialog Workaround
	 * - When a hook is used with `ChooseFont`, Windows **automatically falls back**
	 *   to an **older template**, losing modern UI elements.
	 * - To prevent this forced downgrade, a **modified template** (based on Font.dlg) is used.
	 * - **CBS_OWNERDRAWFIXED should be removed** from the **Size** and **Script** combo boxes
	 *   to restore proper visualization.
	 * - **Custom owner-draw visuals remain** for other font combo boxes to allow font preview.
	 * - Same for the `"AaBbYyZz"` sample text.
	 * - However **Automatic system translation for captions and static texts is lost** in this workaround.
	 *
	 * ## Custom Font Dialog Template (Resource File)
	 * ```rc
	 * IDD_DARK_FONT_DIALOG DIALOG 13, 54, 243, 234
	 * STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU |
	 *       DS_3DLOOK
	 * CAPTION "Font"
	 * FONT 9, "Segoe UI"
	 * BEGIN
	 *     LTEXT           "&Font:", stc1, 7, 7, 98, 9
	 *     COMBOBOX        cmb1, 7, 16, 98, 76,
	 *                     CBS_SIMPLE | CBS_AUTOHSCROLL | CBS_DISABLENOSCROLL |
	 *                     CBS_SORT | WS_VSCROLL | WS_TABSTOP | CBS_HASSTRINGS |
	 *                     CBS_OWNERDRAWFIXED
	 *
	 *     LTEXT           "Font st&yle:", stc2, 114, 7, 74, 9
	 *     COMBOBOX        cmb2, 114, 16, 74, 76,
	 *                     CBS_SIMPLE | CBS_AUTOHSCROLL | CBS_DISABLENOSCROLL |
	 *                     WS_VSCROLL | WS_TABSTOP | CBS_HASSTRINGS |
	 *                     CBS_OWNERDRAWFIXED
	 *
	 *     LTEXT           "&Size:", stc3, 198, 7, 36, 9
	 *     COMBOBOX        cmb3, 198, 16, 36, 76,
	 *                     CBS_SIMPLE | CBS_AUTOHSCROLL | CBS_DISABLENOSCROLL |
	 *                     CBS_SORT | WS_VSCROLL | WS_TABSTOP | CBS_HASSTRINGS |
	 *                     CBS_OWNERDRAWFIXED // remove CBS_OWNERDRAWFIXED
	 *
	 *     GROUPBOX        "Effects", grp1, 7, 97, 98, 76, WS_GROUP
	 *     AUTOCHECKBOX    "Stri&keout", chx1, 13, 111, 90, 10, WS_TABSTOP
	 *     AUTOCHECKBOX    "&Underline", chx2, 13, 127, 90, 10
	 *
	 *     LTEXT           "&Color:", stc4, 13, 144, 89, 9
	 *     COMBOBOX        cmb4, 13, 155, 85, 100,
	 *                     CBS_DROPDOWNLIST | CBS_OWNERDRAWFIXED | CBS_AUTOHSCROLL |
	 *                     CBS_HASSTRINGS | WS_BORDER | WS_VSCROLL | WS_TABSTOP
	 *
	 *     GROUPBOX        "Sample", grp2, 114, 97, 120, 43, WS_GROUP
	 *     CTEXT           "AaBbYyZz", stc5, 116, 106, 117, 33,
	 *                     SS_NOPREFIX | NOT WS_VISIBLE
	 *     LTEXT           "", stc6, 7, 178, 227, 20, SS_NOPREFIX | NOT WS_GROUP
	 *
	 *     LTEXT           "Sc&ript:", stc7, 114, 145, 118, 9
	 *     COMBOBOX        cmb5, 114, 155, 120, 30, CBS_DROPDOWNLIST |
	 *                     CBS_OWNERDRAWFIXED | CBS_AUTOHSCROLL | CBS_HASSTRINGS | // remove CBS_OWNERDRAWFIXED
	 *                     WS_BORDER | WS_VSCROLL | WS_TABSTOP
	 *
	 *     CONTROL         "<A>Show more fonts</A>", IDC_MANAGE_LINK, "SysLink",
	 *                     WS_TABSTOP, 7, 199, 227, 9
	 *
	 *     DEFPUSHBUTTON   "OK", IDOK, 141, 215, 45, 14, WS_GROUP
	 *     PUSHBUTTON      "Cancel", IDCANCEL, 190, 215, 45, 14, WS_GROUP
	 *     PUSHBUTTON      "&Apply", psh3, 92, 215, 45, 14, WS_GROUP
	 *     PUSHBUTTON      "&Help", pshHelp, 43, 215, 45, 14, WS_GROUP
	 * END
	 * ```
	 *
	 * ## Usage Example:
	 * ```cpp
	 * #define IDD_DARK_FONT_DIALOG 1000 // usually in resource.h or other header
	 *
	 * CHOOSEFONT cf{};
	 * cf.Flags |= CF_ENABLEHOOK | CF_ENABLETEMPLATE;
	 * cf.lpfnHook = static_cast<LPCFHOOKPROC>(dmlib::HookDlgProc);
	 * cf.hInstance = GetModuleHandle(nullptr);
	 * cf.lpTemplateName = MAKEINTRESOURCE(IDD_DARK_FONT_DIALOG);
	 * ```
	 *
	 * @param[in]   hWnd        Handle to the dialog window.
	 * @param[in]   uMsg        Message identifier.
	 * @param[in]   wParam      First message parameter (unused).
	 * @param[in]   lParam      Second message parameter (unused).
	 * @return UINT_PTR A value defined by the hook procedure.
	 */

Unfortunately, $CBS_OWNERDRAWFIXED is a style that cannot be removed with _WinAPI_SetWindowLong() after the dialog has already been created. So the technique that I am attempting to do involves loading a modified Font.Dlg template which has $CBS_OWNERDRAWFIXED already removed.

Posted

..if you can load the fancy combo with each line having the font applied to it, we can use that and make out own GUI.
Is an interface to choose a font and that's all that is.
Go figure if in 27H2 they fix it 🤷‍♂️
So if this approach don't work out, look for a pleasing familiar ( visually GUI wise ) and go with that.

Am sure one of those MVPs will snick it in the standard UDFs somehow 😇

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

Posted
15 hours ago, argumentum said:

..just run the thing and is too CPU intensive.

I did not spot any issues with the Dialog_Box_Resources.au3. There is high CPU usage with the generated files becuse of While loop without Sleep. Once you put sleep in the generated file CPU usage drops.

Posted (edited)

for exploration,
because I didn't get far, I stumbled upon Microsoft patents,
it has an open combo box and inside it has another combo box, and on top of that a text box

;~ #AutoIt3Wrapper_UseX64=y

#include <FontConstants.au3>
#include <APIGdiConstants.au3>
#include <WinAPISysWin.au3>
#include <WinAPITheme.au3>
#include <WindowsStylesConstants.au3>
#include <WinAPIGdiDC.au3>
#include <WindowsNotifsConstants.au3>
#include <Misc.au3>
#include <Array.au3>

; Initialize System DPI awareness
DllCall("User32.dll", "bool", "SetProcessDpiAwarenessContext" , "int_ptr", -2)

Global Const $CF_ENABLEHOOK = 0x00000008
Global $g_hDialogProc = 0

Local $aResult = _ChooseFontHooked()
If Not @error Then
    ConsoleWrite("$aResult[2]=" & $aResult[2] & @CRLF)
    _ArrayDisplay($aResult)
EndIf

Func _ChooseFontHooked($sFontName = "Segoe UI", $iPointSize = 10, $iFontColorRef = Default, $iFontWeight = 0, $bItalic = False, $bUnderline = False, $bStrikethru = False, $hWndOwner = 0)
    Local $iItalic = 0, $iUnderline = 0, $iStrikeout = 0, $iRetExtended = 0
    If $sFontName = Default Then $sFontName = "Segoe UI"
    If $iPointSize = Default Then $iPointSize = 10
    If $iFontColorRef = Default Then $iFontColorRef = 0

    Local $hDC = _WinAPI_GetDC(0)
    Local $iHeight = Round(($iPointSize * _WinAPI_GetDeviceCaps($hDC, $LOGPIXELSX)) / 72, 0)
    _WinAPI_ReleaseDC(0, $hDC)

    Local $tChooseFont = DllStructCreate($tagCHOOSEFONT)
    Local $tLogFont = DllStructCreate($tagLOGFONT)

    DllStructSetData($tChooseFont, "Size", DllStructGetSize($tChooseFont))
    DllStructSetData($tChooseFont, "hWndOwner", $hWndOwner)
    DllStructSetData($tChooseFont, "LogFont", DllStructGetPtr($tLogFont))
    DllStructSetData($tChooseFont, "PointSize", $iPointSize * 10)

    $g_hDialogProc = DllCallbackRegister("_DialogProc", "int", "hwnd;uint;wparam;lparam")
    DllStructSetData($tChooseFont, 'fnHook', DllCallbackGetPtr($g_hDialogProc))

    ; just the Hook!
    DllStructSetData($tChooseFont, "Flags", BitOR($CF_SCREENFONTS, $CF_EFFECTS, $CF_INITTOLOGFONTSTRUCT, $CF_ENABLEHOOK))
    DllStructSetData($tChooseFont, "rgbColors", $iFontColorRef)

    DllStructSetData($tLogFont, "Height", -$iHeight)
    DllStructSetData($tLogFont, "Weight", $iFontWeight)
    DllStructSetData($tLogFont, "Italic", $bItalic)
    DllStructSetData($tLogFont, "Underline", $bUnderline)
    DllStructSetData($tLogFont, "Strikeout", $bStrikethru)
    DllStructSetData($tLogFont, "FaceName", $sFontName)

    ; Pass raw pointer to ensure stable execution across architectures
    Local $aCall = DllCall("comdlg32.dll", "bool", "ChooseFontW", "ptr", DllStructGetPtr($tChooseFont))

    DllCallbackFree($g_hDialogProc)

    If @error Then Return SetError(@error, @extended, -2)
    If $aCall[0] = 0 Then Return SetError(-3, $iRetExtended, -1)

    Local $sFaceName = DllStructGetData($tLogFont, "FaceName")
    If DllStructGetData($tLogFont, "Italic") Then $iItalic = 2
    If DllStructGetData($tLogFont, "Underline") Then $iUnderline = 4
    If DllStructGetData($tLogFont, "Strikeout") Then $iStrikeout = 8

    Return StringSplit(BitOR($iItalic, $iUnderline, $iStrikeout) & "," & $sFaceName & "," & (DllStructGetData($tChooseFont, "PointSize") / 5), ",")
EndFunc

Func _DialogProc($hWnd, $iMsg, $wParam, $lParam)
    #forceref $wParam, $lParam
    Local Static $hBrush

    Switch $iMsg
        Case $WM_INITDIALOG

            ; The background brush
            $hBrush = _WinAPI_CreateSolidBrush(0x202020)

            Local $aCtrls = _WinAPI_EnumChildWindows($hWnd, False)
            If Not @error Then
                Local $sClass
                For $i = 1 To $aCtrls[0][0]
                    $sClass = _WinAPI_GetClassName($aCtrls[$i][0])
                    ConsoleWrite("$sClass=" & $sClass & @CRLF)

                    $iStyle = _WinAPI_GetWindowLong($aCtrls[$i][0], $GWL_STYLE)

                    Switch $sClass
                        Case "Button"
                            ; Check for GroupBoxes (they don't have WS_TABSTOP)
                            If Not BitAND($iStyle, $WS_TABSTOP) Then
                                _WinAPI_SetWindowTheme($aCtrls[$i][0], "", "")
                            Else
                                _WinAPI_SetWindowTheme($aCtrls[$i][0], "DarkMode_Explorer", "Button")
                            EndIf

                        Case "ComboBox", "ListBox", "ComboLBox"
                            _WinAPI_SetWindowTheme($aCtrls[$i][0], "DarkMode_CFD", "Combobox")

                        Case "Edit"
                            _WinAPI_SetWindowTheme($aCtrls[$i][0], "DarkMode_Explorer", "Edit")

                        Case "Static"
                            _WinAPI_SetWindowTheme($aCtrls[$i][0], "", "")

                        Case Else
                            _WinAPI_SetWindowTheme($aCtrls[$i][0], "DarkMode_Explorer", Default)
                    EndSwitch
                Next
            EndIf


        Case $WM_SHOWWINDOW
            Local $aChildPos = WinGetPos($hWnd)
            WinMove($hWnd, "", (@DesktopWidth / 2 - ($aChildPos[2] / 2)), (@DesktopHeight / 2 - ($aChildPos[3] / 2)))

        Case $WM_CTLCOLORSTATIC, $WM_CTLCOLORDLG, $WM_CTLCOLORLISTBOX, $WM_CTLCOLORBTN, $WM_CTLCOLOREDIT
            _WinAPI_SetBkMode($wParam, $TRANSPARENT)
            _WinAPI_SetBkColor($wParam, 0x202020)   ; Dark background for text-well
            _WinAPI_SetTextColor($wParam, 0xFFFFFF) ; Pure white text
            Return $hBrush

        Case $WM_DESTROY
            If $hBrush Then _WinAPI_DeleteObject($hBrush)
    EndSwitch
    Return 0
EndFunc

 

Edited by ioa747

I know that I know nothing

Posted (edited)
On 7/9/2026 at 7:54 PM, WildByDesign said:

I have tried a few variations with $CF_ENABLETEMPLATE which failed and a few variations with $CF_ENABLETEMPLATEHANDLE that also failed.

We mightn't need it, but for fun I had a quick go at the custom dialog template.  Mine is failing on Win11 with CFERR_NOFONTS though. I've probably stuffed something up - but I'll give it a spin on a win7 box first in case the api is just antiquated.  Also worth noting that I'm using a DLGTEMPLATEEX structure - but perhaps we need a DLGTEMPLATE struct... 🤷‍♂️ TBH I probably should do a bit more reading!  

Example needs latest udf from here:  When you get to the main GUI, you can test our dialog template.

;~ #AutoIt3Wrapper_UseX64=N

#include "Dialogs.au3"

#include <WinAPI.au3>
#include <WinAPITheme.au3>
#include <GuiConstants.au3>
#include <Misc.au3>


;~ Global Const $CF_SCREENFONTS = 0x00000001
;~ Global Const $CF_PRINTERFONTS = 0x00000002
Global Const $CF_BOTH = BitOR($CF_SCREENFONTS, $CF_PRINTERFONTS)
Global Const $CF_SHOWHELP = 0x00000004
Global Const $CF_ENABLEHOOK = 0x00000008
Global Const $CF_ENABLETEMPLATE = 0x00000010
Global Const $CF_ENABLETEMPLATEHANDLE = 0x00000020
;~ Global Const $CF_INITTOLOGFONTSTRUCT = 0x00000040
Global Const $CF_USESTYLE = 0x00000080
;~ Global Const $CF_EFFECTS = 0x00000100
Global Const $CF_APPLY = 0x00000200
Global Const $CF_ANSIONLY = 0x00000400          ;Obsolete
Global Const $CF_SCRIPTSONLY = $CF_ANSIONLY
Global Const $CF_NOVECTORFONTS = 0x00000800
Global Const $CF_NOOEMFONTS = $CF_NOVECTORFONTS
Global Const $CF_NOSIMULATIONS = 0x00001000
Global Const $CF_LIMITSIZE = 0x00002000
Global Const $CF_FIXEDPITCHONLY = 0x00004000
Global Const $CF_WYSIWYG = 0x00008000           ;Obsolete             ;// must also have CF_SCREENFONTS & CF_PRINTERFONTS
Global Const $CF_FORCEFONTEXIST = 0x00010000
Global Const $CF_SCALABLEONLY = 0x00020000
Global Const $CF_TTONLY = 0x00040000
Global Const $CF_NOFACESEL = 0x00080000
Global Const $CF_NOSTYLESEL = 0x00100000
Global Const $CF_NOSIZESEL = 0x00200000
Global Const $CF_SELECTSCRIPT = 0x00400000
;~ Global Const $CF_NOSCRIPTSEL = 0x00800000
Global Const $CF_NOVERTFONTS = 0x01000000
Global Const $CF_INACTIVEFONTS = 0x02000000

;~ Global Const $tagCHOOSEFONTW = _ ;"struct;" & _
;~      "dword lStructSize;" & _
;~      "hwnd hwndOwner;" & _
;~      "handle hDC;" & _
;~      "ptr lpLogFont;" & _
;~      "int iPointSize;" & _
;~      "dword Flags;" & _
;~      "uint rgbColors;" & _
;~      "lparam lCustData;" & _
;~      "ptr lpfnHook;" & _
;~      "ptr lpTemplateName;" & _
;~      "handle hInstance;" & _
;~      "ptr lpszStyle;" & _
;~      "word nFontType;word;" & _
;~      "int nSizeMin;" & _
;~      "int nSizeMax;"; & _
;~      "endstruct;"

;~ ConsoleWrite($tagCHOOSEFONT & @CRLF)


ChooseFont()
TestDialog()

Func ChooseFont()
;~  Local $hGUI1 = GUICreate("Test")
;~  GUISetState()
;~  Local $hDC = _WinAPI_GetDC($hGUI1)
;~  Local $iFontSize = 8

    $hTemplate = BuildTemplate()
    Local $tTemplate = _Dialog_Create($hTemplate)

    Local $tLOGFONT = DllStructCreate($tagLOGFONT)
    Local $tChooseFont = DllStructCreate($tagCHOOSEFONT)
;~  ConsoleWrite($tagLOGFONT & @CRLF)
;~  ConsoleWrite($tagCHOOSEFONTW & @CRLF)

    $tChooseFont.Size = DllStructGetSize($tChooseFont)
    $tChooseFont.LogFont = DllStructGetPtr($tLOGFONT)
    $tChooseFont.hInstance = DllStructGetPtr($tTemplate)
;~  $tChooseFont.hDC = $hDC
;~  $tChooseFont.hwndOwner = $hGUI1
    $tChooseFont.Flags = BitOR(0, $CF_ENABLETEMPLATEHANDLE )

    Local $__g_hDLL_COMDLG32 = DllOpen("Comdlg32.dll")
    $aCall = DllCall($__g_hDLL_COMDLG32, "bool", "ChooseFontW", "struct*", $tChooseFont)
    $aCall = DllCall($__g_hDLL_COMDLG32, "long", "CommDlgExtendedError") ; 0x2001 =
    ConsoleWrite(Hex($aCall[0]) & @CRLF) ; CFERR_NOFONTS (0x2001)

;~  Do
;~  Until GUIGetMsg() = $GUI_EVENT_CLOSE

EndFunc


Func BuildTemplate()
    Local $hTemplate = _Dialog_NewTemplate("Font", 243, 234)

    ;dialog will automatically grow & shrink according to typeface/font size (Dialog Units are based on this!)
    _Dialog_SetFont($hTemplate, "Segoe UI", 9)

    _Dialog_AddStatic($hTemplate, $idStc1, "&Font:", 7, 7, 98, 9, $SS_CENTERIMAGE)
    _Dialog_AddComboBox($hTemplate, $idStc1, 7, 16, 98, 76, BitOR($CBS_SIMPLE, $CBS_AUTOHSCROLL, $CBS_DISABLENOSCROLL, _
            $CBS_SORT, $WS_VSCROLL, $WS_TABSTOP, $CBS_HASSTRINGS, $CBS_OWNERDRAWFIXED))


    _Dialog_AddStatic($hTemplate, $idStc2, "Font st&yle:", 114, 7, 74, 9, $SS_CENTERIMAGE)
    _Dialog_AddComboBox($hTemplate, $idCmb2, 114, 16, 74, 76, BitOR($CBS_SIMPLE, $CBS_AUTOHSCROLL, $CBS_DISABLENOSCROLL, _
            $WS_VSCROLL, $WS_TABSTOP, $CBS_HASSTRINGS, $CBS_OWNERDRAWFIXED))


    _Dialog_AddStatic($hTemplate, $idStc3, "&Size:", 198, 7, 36, 9, $SS_CENTERIMAGE)
    _Dialog_AddComboBox($hTemplate, $idCmb3, 198, 16, 36, 76, BitOR($CBS_SIMPLE, $CBS_AUTOHSCROLL, $CBS_DISABLENOSCROLL, _
            $CBS_SORT, $WS_VSCROLL, $WS_TABSTOP, $CBS_HASSTRINGS))

    _Dialog_AddButton($hTemplate, $idGrp1, "Effects", 7, 97, 98, 76, BitOR($WS_GROUP, $BS_GROUPBOX))
    _Dialog_AddButton($hTemplate, $idChx1, "Stri&keout", 13, 111, 90, 10, $BS_AUTOCHECKBOX)
    _Dialog_AddButton($hTemplate, $idChx2, "&Underline", 13, 127, 90, 10, BitOR($BS_AUTOCHECKBOX, $WS_TABSTOP))

    _Dialog_AddStatic($hTemplate, $idStc4, "&Color:", 13, 144, 89, 9, $SS_CENTERIMAGE)
    _Dialog_AddComboBox($hTemplate, $idCmb4, 13, 155, 85, 100, BitOR($CBS_DROPDOWNLIST, $CBS_OWNERDRAWFIXED, _
            $CBS_AUTOHSCROLL, $CBS_DISABLENOSCROLL, $CBS_HASSTRINGS, $WS_BORDER, $WS_VSCROLL, $WS_TABSTOP))


    _Dialog_AddButton($hTemplate, $idGrp2, "Sample", 114, 97, 120, 43, BitOR($WS_GROUP, $BS_GROUPBOX))
    _Dialog_AddStatic($hTemplate, $idStc5, "AaBbYyZz", 116, 106, 117, 33, BitOR($SS_CENTER, $SS_NOPREFIX, $SS_CENTERIMAGE))
    _Dialog_AddStatic($hTemplate, $idStc6, "", 7, 178, 227, 20)

    _Dialog_AddStatic($hTemplate, $idStc7, "Sc&ript", 114, 145, 118, 9, $SS_CENTERIMAGE)
    _Dialog_AddComboBox($hTemplate, $idCmb5, 114, 155, 120, 30, BitOR($CBS_DROPDOWNLIST, $CBS_OWNERDRAWFIXED, _
            $CBS_AUTOHSCROLL, $CBS_HASSTRINGS, $WS_BORDER, $WS_VSCROLL, $WS_TABSTOP))

    _Dialog_AddControl($hTemplate, $IDC_MANAGE_LINK, "SysLink", "<A>Show more fonts</A>", 7, 199, 227, 9, $WS_TABSTOP)

    _Dialog_AddButton($hTemplate, $IDOK, "OK", 141, 215, 45, 14, BitOR($WS_GROUP, $BS_DEFPUSHBUTTON))
    _Dialog_AddButton($hTemplate, $IDCANCEL, "Cancel", 190, 215, 45, 14, $WS_GROUP)
    _Dialog_AddButton($hTemplate, $idPsh3, "&Apply", 92, 215, 45, 14, $WS_GROUP)
    _Dialog_AddButton($hTemplate, $idPshHelp, "&Help", 43, 215, 45, 14, $WS_GROUP)

    Return $hTemplate
EndFunc   ;==>BuildTemplate


Func TestDialog()

    _Dialog_Startup()
    Local $hTemplate = BuildTemplate()

    Local $hGUI = GUICreate("Example GUI", 160, 165, -1, -1, BitOR($WS_POPUP, $WS_CAPTION, $WS_SYSMENU))

    GUICtrlCreateGroup("Dialog Type: ", 10, 10, 140, 73)
    Local $idModal = GUICtrlCreateRadio("Modal", 20, 30, 120, 18)
    GUICtrlSetState(-1, $GUI_CHECKED)
    Local $idModless = GUICtrlCreateRadio("Modeless", 20, 55, 120, 18)
    Local $idLaunch = GUICtrlCreateButton("Launch", 10, 90, 140, 25, $BS_DEFPUSHBUTTON)
    Local $idResult = GUICtrlCreateLabel(" Return: ", 10, 134, 140, 20, BitOR($SS_SUNKEN, $SS_LEFT, $SS_CENTERIMAGE))
    GUICtrlSetFont(-1, 7)
    GUICtrlSetColor(-1, 0x808080)
    GUICtrlSetState($idLaunch, $GUI_FOCUS)

    GUISetState()

    Local $iSel = $idModal
    Local $iMsg, $vResult

    ;If module hasn't been loaded into mem, use _WinAPI_LoadLibrary instead!
    Local $hAu3Module = _WinAPI_GetModuleHandle(0)     ;0 = This process. i.e. @AutoItExe

    While 1
        $iMsg = GUIGetMsg()
        Switch $iMsg

            Case $idModal, $idModless
                $iSel = $iMsg

            Case $idLaunch
                Switch $iSel
                    Case $idModal
                        ; 3rd param is optional, sent with WM_INITDIALOG as its lparam
                        $vResult = _Dialog_LaunchModal($hTemplate, $hGUI)

                    Case $idModless
                        $vResult = _Dialog_LaunchModeless($hTemplate, $hGUI)

                EndSwitch
                GUICtrlSetData($idResult, StringFormat(" Return: %s", $vResult))

            Case $GUI_EVENT_CLOSE
                ExitLoop

        EndSwitch
    WEnd

    GUIDelete($hGUI)

    _Dialog_Shutdown()
EndFunc   ;==>Example
Edited by MattyD
  • Solution
Posted (edited)

Ok, now its going :)  I had my combo1 as $idStc1 instead of $idCmb1... 🤦‍♂️

;~ #AutoIt3Wrapper_UseX64=N

#include "Dialogs.au3"

#include <WinAPI.au3>
#include <WinAPITheme.au3>
#include <GuiConstants.au3>
#include <Misc.au3>


;~ Global Const $CF_SCREENFONTS = 0x00000001
;~ Global Const $CF_PRINTERFONTS = 0x00000002
Global Const $CF_BOTH = BitOR($CF_SCREENFONTS, $CF_PRINTERFONTS)
Global Const $CF_SHOWHELP = 0x00000004
Global Const $CF_ENABLEHOOK = 0x00000008
Global Const $CF_ENABLETEMPLATE = 0x00000010
Global Const $CF_ENABLETEMPLATEHANDLE = 0x00000020
;~ Global Const $CF_INITTOLOGFONTSTRUCT = 0x00000040
Global Const $CF_USESTYLE = 0x00000080
;~ Global Const $CF_EFFECTS = 0x00000100
Global Const $CF_APPLY = 0x00000200
Global Const $CF_ANSIONLY = 0x00000400          ;Obsolete
Global Const $CF_SCRIPTSONLY = $CF_ANSIONLY
Global Const $CF_NOVECTORFONTS = 0x00000800
Global Const $CF_NOOEMFONTS = $CF_NOVECTORFONTS
Global Const $CF_NOSIMULATIONS = 0x00001000
Global Const $CF_LIMITSIZE = 0x00002000
Global Const $CF_FIXEDPITCHONLY = 0x00004000
Global Const $CF_WYSIWYG = 0x00008000           ;Obsolete             ;// must also have CF_SCREENFONTS & CF_PRINTERFONTS
Global Const $CF_FORCEFONTEXIST = 0x00010000
Global Const $CF_SCALABLEONLY = 0x00020000
Global Const $CF_TTONLY = 0x00040000
Global Const $CF_NOFACESEL = 0x00080000
Global Const $CF_NOSTYLESEL = 0x00100000
Global Const $CF_NOSIZESEL = 0x00200000
Global Const $CF_SELECTSCRIPT = 0x00400000
;~ Global Const $CF_NOSCRIPTSEL = 0x00800000
Global Const $CF_NOVERTFONTS = 0x01000000
Global Const $CF_INACTIVEFONTS = 0x02000000


Global $hCFHOOK = DllCallbackRegister("_CFHook", "ptr", "hwnd;uint;wparam;lparam")
Global $pCFHOOK = DllCallbackGetPtr($hCFHOOK)

ChooseFont()
TestDialog()

Func ChooseFont()
    $hTemplate = BuildTemplate()
    Local $tTemplate = _Dialog_Create($hTemplate)

    Local $tLOGFONT = DllStructCreate($tagLOGFONT)
    Local $tChooseFont = DllStructCreate($tagCHOOSEFONT)

    $tChooseFont.Size = DllStructGetSize($tChooseFont)
    $tChooseFont.LogFont = DllStructGetPtr($tLOGFONT)
    $tChooseFont.hInstance = DllStructGetPtr($tTemplate)
    $tChooseFont.fnHook = $pCFHOOK
    $tChooseFont.Flags = BitOR($CF_ENABLEHOOK, $CF_ENABLETEMPLATEHANDLE)

    DllCall("Comdlg32.dll", "bool", "ChooseFontW", "struct*", $tChooseFont)
EndFunc


Func BuildTemplate()
    Local $hTemplate = _Dialog_NewTemplate("Font", 243, 234, 13, 54, BitOR($DS_MODALFRAME, $WS_POPUP, $WS_CAPTION, $WS_SYSMENU, $DS_3DLOOK))

    ;dialog will automatically grow & shrink according to typeface/font size (Dialog Units are based on this!)
    _Dialog_SetFont($hTemplate, "Segoe UI", 9)

    _Dialog_AddStatic($hTemplate, $idStc1, "&Font:", 7, 7, 98, 9, $SS_CENTERIMAGE)
    _Dialog_AddComboBox($hTemplate, $idCmb1, 7, 16, 98, 76, BitOR($CBS_SIMPLE, $CBS_AUTOHSCROLL, $CBS_DISABLENOSCROLL, _
            $CBS_SORT, $WS_VSCROLL, $WS_TABSTOP, $CBS_HASSTRINGS, $CBS_OWNERDRAWFIXED))


    _Dialog_AddStatic($hTemplate, $idStc2, "Font st&yle:", 114, 7, 74, 9, $SS_CENTERIMAGE)
    _Dialog_AddComboBox($hTemplate, $idCmb2, 114, 16, 74, 76, BitOR($CBS_SIMPLE, $CBS_AUTOHSCROLL, $CBS_DISABLENOSCROLL, _
            $WS_VSCROLL, $WS_TABSTOP, $CBS_HASSTRINGS, $CBS_OWNERDRAWFIXED))


    _Dialog_AddStatic($hTemplate, $idStc3, "&Size:", 198, 7, 36, 9, $SS_CENTERIMAGE)
    _Dialog_AddComboBox($hTemplate, $idCmb3, 198, 16, 36, 76, BitOR($CBS_SIMPLE, $CBS_AUTOHSCROLL, $CBS_DISABLENOSCROLL, _
            $CBS_SORT, $WS_VSCROLL, $WS_TABSTOP, $CBS_HASSTRINGS))

    _Dialog_AddButton($hTemplate, $idGrp1, "Effects", 7, 97, 98, 76, BitOR($WS_GROUP, $BS_GROUPBOX))
    _Dialog_AddButton($hTemplate, $idChx1, "Stri&keout", 13, 111, 90, 10, $BS_AUTOCHECKBOX)
    _Dialog_AddButton($hTemplate, $idChx2, "&Underline", 13, 127, 90, 10, BitOR($BS_AUTOCHECKBOX, $WS_TABSTOP))

    _Dialog_AddStatic($hTemplate, $idStc4, "&Color:", 13, 144, 89, 9, $SS_CENTERIMAGE)
    _Dialog_AddComboBox($hTemplate, $idCmb4, 13, 155, 85, 100, BitOR($CBS_DROPDOWNLIST, $CBS_OWNERDRAWFIXED, _
            $CBS_AUTOHSCROLL, $CBS_DISABLENOSCROLL, $CBS_HASSTRINGS, $WS_BORDER, $WS_VSCROLL, $WS_TABSTOP))


    _Dialog_AddButton($hTemplate, $idGrp2, "Sample", 114, 97, 120, 43, BitOR($WS_GROUP, $BS_GROUPBOX))
    _Dialog_AddStatic($hTemplate, $idStc5, "AaBbYyZz", 116, 106, 117, 33, BitOR($SS_CENTER, $SS_NOPREFIX, $SS_CENTERIMAGE))
    _Dialog_AddStatic($hTemplate, $idStc6, "", 7, 178, 227, 20)

    _Dialog_AddStatic($hTemplate, $idStc7, "Sc&ript", 114, 145, 118, 9, $SS_CENTERIMAGE)
    _Dialog_AddComboBox($hTemplate, $idCmb5, 114, 155, 120, 30, BitOR($CBS_DROPDOWNLIST, $CBS_OWNERDRAWFIXED, _
            $CBS_AUTOHSCROLL, $CBS_HASSTRINGS, $WS_BORDER, $WS_VSCROLL, $WS_TABSTOP))

    _Dialog_AddControl($hTemplate, $IDC_MANAGE_LINK, "SysLink", "<A>Show more fonts</A>", 7, 199, 227, 9, $WS_TABSTOP)

    _Dialog_AddButton($hTemplate, $IDOK, "OK", 141, 215, 45, 14, BitOR($WS_GROUP, $BS_DEFPUSHBUTTON))
    _Dialog_AddButton($hTemplate, $IDCANCEL, "Cancel", 190, 215, 45, 14, $WS_GROUP)
    _Dialog_AddButton($hTemplate, $idPsh3, "&Apply", 92, 215, 45, 14, $WS_GROUP)
    _Dialog_AddButton($hTemplate, $idPshHelp, "&Help", 43, 215, 45, 14, $WS_GROUP)

    Return $hTemplate
EndFunc   ;==>BuildTemplate


Func TestDialog()

    _Dialog_Startup()
    Local $hTemplate = BuildTemplate()

    Local $hGUI = GUICreate("Example GUI", 160, 165, -1, -1, BitOR($WS_POPUP, $WS_CAPTION, $WS_SYSMENU))

    GUICtrlCreateGroup("Dialog Type: ", 10, 10, 140, 73)
    Local $idModal = GUICtrlCreateRadio("Modal", 20, 30, 120, 18)
    GUICtrlSetState(-1, $GUI_CHECKED)
    Local $idModless = GUICtrlCreateRadio("Modeless", 20, 55, 120, 18)
    Local $idLaunch = GUICtrlCreateButton("Launch", 10, 90, 140, 25, $BS_DEFPUSHBUTTON)
    Local $idResult = GUICtrlCreateLabel(" Return: ", 10, 134, 140, 20, BitOR($SS_SUNKEN, $SS_LEFT, $SS_CENTERIMAGE))
    GUICtrlSetFont(-1, 7)
    GUICtrlSetColor(-1, 0x808080)
    GUICtrlSetState($idLaunch, $GUI_FOCUS)

    GUISetState()

    Local $iSel = $idModal
    Local $iMsg, $vResult

    While 1
        $iMsg = GUIGetMsg()
        Switch $iMsg

            Case $idModal, $idModless
                $iSel = $iMsg

            Case $idLaunch
                Switch $iSel
                    Case $idModal
                        ; 3rd param is optional, sent with WM_INITDIALOG as its lparam
                        $vResult = _Dialog_LaunchModal($hTemplate, $hGUI)

                    Case $idModless
                        $vResult = _Dialog_LaunchModeless($hTemplate, $hGUI)

                EndSwitch
                GUICtrlSetData($idResult, StringFormat(" Return: %s", $vResult))

            Case $GUI_EVENT_CLOSE
                ExitLoop

        EndSwitch
    WEnd

    GUIDelete($hGUI)

    _Dialog_Shutdown()
EndFunc   ;==>Example


Func _CFHook($hWnd, $iMsg, $wParam, $lParam)

    Switch $iMsg
        Case $WM_INITDIALOG
            Return True

    EndSwitch
    Return _WinAPI_CallNextHookEx(0, $iMsg, $wParam, $lParam)
EndFunc

Edit: to get the preview bit working properly (the AaBbYyZz box), comment out the forced "statics" style in "Dialogs.au3" 

Function: _Dialog_AddStatic()
$iStyle = BitOR($WS_CHILD, $WS_VISIBLE, $WS_TABSTOP, $iStyle) ;Comment this out!

Edited by MattyD
Posted

Thank you so much, Matty. I really can't thank you enough. I realized that when I was asking for help on this topic that it would be right in your wheelhouse since I knew that you had done a lot of work with dialogs recently.

This is quite incredible, by the way.

One problem that I have at the moment is that some sections do not show up (related to this below):

_Dialog_AddButton($hTemplate, $idGrp1, "Effects", 7, 97, 98, 76, BitOR($WS_GROUP, $BS_GROUPBOX))
    _Dialog_AddButton($hTemplate, $idChx1, "Stri&keout", 13, 111, 90, 10, $BS_AUTOCHECKBOX)
    _Dialog_AddButton($hTemplate, $idChx2, "&Underline", 13, 127, 90, 10, BitOR($BS_AUTOCHECKBOX, $WS_TABSTOP))

    _Dialog_AddStatic($hTemplate, $idStc4, "&Color:", 13, 144, 89, 9, $SS_CENTERIMAGE)
    _Dialog_AddComboBox($hTemplate, $idCmb4, 13, 155, 85, 100, BitOR($CBS_DROPDOWNLIST, $CBS_OWNERDRAWFIXED, _
            $CBS_AUTOHSCROLL, $CBS_DISABLENOSCROLL, $CBS_HASSTRINGS, $WS_BORDER, $WS_VSCROLL, $WS_TABSTOP))

When I look at the windows in System Informer, I can see that those windows/controls are there. In System Informer, if I click each one and choose Enable and Visible, they all show up. But I can't get them to show up by default in the template. Even when I add $WS_VISIBLE to each one.

Posted

They're just governed by the $tChooseFont.Flags Flags.

  • $CF_APPLY - Apply Button
  • $CF_SHOWHELP - Help Button
  • $CF_EFFECTS - Strike/underline/colour
  • $CF_USESTYLE - Style combo
  • $CF_SCREENFONTS (or $CF_BOTH) - populates static6 with extra info.

You may need to give Static6 this style though: BitOR($SS_NOPREFIX, $WS_VISIBLE)

Bit of background on CF_SCREENFONTS, because it no longer does what you'd think!

Starting with Windows 7, the CF_PRINTERFONTS, CF_SCREENFONTS, CF_BOTH, and CF_WYSIWYG flags are no longer used by the ChooseFont function for font enumeration. They are obsolete in Windows 7. However, the CF_PRINTERFONTS flag retains one function: to display the font type description label at the bottom of the Font dialog box.
Posted

Your solution was exactly what I was hoping to achieve, @MattyD. I was worried that I might have to load the template from a compiled binary, but thanks to your Dialogs UDF, no compiled binary is needed. It does exactly what I wanted to do plus I'm learning quite a bit more about dialogs at the same time. Thank you for your time and for always being willing to help. You seem to dive straight in to some of the more difficult help and support topics here. You are an important part of this community. :)

Posted

Font-Dialog.png

 

This might be the best that I can do with the ChooseFont dialog. It's definitely an improvement from what I was previously able to do. I've been able to add dark theme on 2 more controls here thanks to the Dialogs UDF.

The problem with the remaining light colored controls is the fact that they require the CBS_OWNERDRAWFIXED style. If I remove that style for each of those controls, I can make them dark. However, without CBS_OWNERDRAWFIXED, those controls would lack the ability to show the various font styles, colors, etc.

I have thought about (and briefly tried) using _WinAPI_InvertRect specifically on those light controls. But I think that CBS_OWNERDRAWFIXED overrides anything that I can do with it.

For now, I count this as a win. Some nice dark theme improvements overall. It is probably the best that we can do considering it's such an old Font dialog window. 😎

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
×
×
  • Create New...