Jump to content

Recommended Posts

Posted

I'm trying to create a simple clock widget that automatically scales the text to the size of the window. I came up with the following method, but it doesn't work as well as I'd like. It especially has trouble scaling to the width of the window for some reason (in the example, try resizing the window to be narrow and tall).

Does anyone have a better method?

#include <Misc.au3>
#include <WinAPIConv.au3>
#include <GUIConstants.au3>
#include <GDIPlus.au3>

Opt('MustDeclareVars', 1)

Global $_FONT_FAMILY = 'Arial', $_LB_TEXT

Main()

Func Main()
    _GDIPlus_Startup()

    Local $hGUI

    GUIRegisterMsg($WM_SIZE, WM_SIZE)

    $hGUI = GUICreate('', 300, 100, Default, Default, $WS_OVERLAPPEDWINDOW, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST))
    $_LB_TEXT = GUICtrlCreateLabel('This is a string', 0, 0, 300, 100, BitOR($SS_CENTER, $SS_CENTERIMAGE))

    GUICtrlSetFont($_LB_TEXT, _MeasureString($hGUI, GUICtrlRead($_LB_TEXT), $_FONT_FAMILY), 0, 0, $_FONT_FAMILY, 5)

    GUISetState()

    Local $iGM
    While 1
        $iGM = GUIGetMsg()
        Switch $iGM
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch
    WEnd

    _GDIPlus_Shutdown()
EndFunc

Func WM_SIZE($hWnd, $iMsg, $wParam, $lParam)
    GUICtrlSetFont($_LB_TEXT, _MeasureString($hWnd, GUICtrlRead($_LB_TEXT), $_FONT_FAMILY), 0, 0, $_FONT_FAMILY, 5)
EndFunc


Func _MeasureString($hWnd, $sString, $sFont = 'Arial')
    Local $iError, $aSize, $hGraphic, $hFormat, $hFamily, $tLayout, $iFontSize, $hFont, $aInfo

    If Not IsHWnd($hWnd) Then
        $hWnd = GUICtrlGetHandle($hWnd)
    EndIf

    $aSize = WinGetClientSize($hWnd)

    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hWnd)
    $hFormat = _GDIPlus_StringFormatCreate()
    $hFamily = _GDIPlus_FontFamilyCreate($sFont)
    $tLayout = _GDIPlus_RectFCreate(0, 0, $aSize[0], $aSize[1])
    $iFontSize = 0
    Do
        If Not $hFamily Then
            $iError = 1
            $iFontSize = 10
            ExitLoop
        EndIf
        $iFontSize += 1
        $hFont = _GDIPlus_FontCreate($hFamily, $iFontSize, 0)
        $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $sString, $hFont, $tLayout, $hFormat)
        _GDIPlus_FontDispose($hFont)
        If $aInfo[1] = 0 Then ExitLoop
    Until DllStructGetData($aInfo[0], 3) >= $aSize[0] Or DllStructGetData($aInfo[0], 4) >= $aSize[1]
    $iFontSize -= 1

    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_GraphicsDispose($hGraphic)

    Return SetError($iError, 0, $iFontSize)
EndFunc

PouFIIW.pngVPFeIdp.png

Posted

You need to create an ampty RectF to calculate current string size

;~     $tLayout = _GDIPlus_RectFCreate(0, 0, $aSize[0], $aSize[1])
    $tLayout = _GDIPlus_RectFCreate()

 

  • 3 weeks later...

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...