Jump to content

Can someone suggest a better way to 'autosize a font' with Drawstring?


kendama
 Share

Recommended Posts

Hi everyone, I'm trying to fill a user generated string into a variable size window with the largest font possible.  Below is a sample that shows an issue I can't seem to figure a way around.  If I display the string 'this is a test' it fits fine, if I display the string 'A NEW TEST' it wraps the last word of the text partially off the viewable window.  Me writing to the input boxes is just trying to figure out what's going on.  Anyone have some suggestions to help?

Thanks,

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>

Global $hGUI = GUICreate("GDI+",400,300, -1, -1, BitOR($WS_MINIMIZEBOX, $WS_CAPTION, $WS_POPUP, $WS_SYSMENU, $WS_MAXIMIZEBOX, $WS_SIZEBOX, 0))
Global $TexttoDisplay = GUICtrlCreateInput("",60,1,176,20)
Global $Doit = GUICtrlCreateButton("Display",1,1,56,19)
Global $FSText = GUICtrlCreateInput("FontSize",240,1,40,20)
Global $aInfoSZ = GUICtrlCreateInput("aInfo",290,1,40,20)
Global $DoitFixed = GUICtrlCreateButton("Fixed",340,1,56,19)

GUISetState(@SW_SHOW)
_GDIPlus_Startup()
DrawText("A NEW TEST")

    While 1
        Local $GuiMsg = GUIGetMsg()

        Switch $GuiMsg
            Case $GUI_EVENT_CLOSE
                _GDIPlus_Shutdown()
                ExitLoop

            Case $Doit
                DrawText(GUICtrlRead($TexttoDisplay))

            case $DoitFixed
                DrawText(GUICtrlRead($TexttoDisplay), True)

        EndSwitch
    WEnd

Func DrawText($StringToDraw, $Fixed=False)
    Local $aWinSize = WinGetClientSize($hGUI)
    Local  $tLayout, $aInfo
    Local $FontSize = 11

    Local $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    Local $bgBrush = _GDIPlus_BrushCreateSolid("0xFFFFFFFF") ;color format AARRGGBB (hex)
    Local $hBrush = _GDIPlus_BrushCreateSolid("0xFF000000")
    Local $hFormat = _GDIPlus_StringFormatCreate()
    _GDIPlus_StringFormatSetAlign($hFormat, 1) ;center text
    Local $hFamily = _GDIPlus_FontFamilyCreate("Arial")

    $tLayout = _GDIPlus_RectFCreate(1, 1, $aWinSize[0]-1, $aWinSize[1]-1)

    Do
        $FontSize = $FontSize +1
        Local $hFont = _GDIPlus_FontCreate($hFamily, $FontSize, 0)
        $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $StringToDraw, $hFont, $tLayout, $hFormat)
        _GDIPlus_FontDispose($hFont)
    Until $aInfo[1] < StringLen($StringToDraw)-1

    If $Fixed = True Then
        $FontSize = GUICtrlRead($FSText)
    EndIf

    Local $hFont = _GDIPlus_FontCreate($hFamily, $FontSize-1, 0)
    $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $StringtoDraw, $hFont, $tLayout, $hFormat)
    Local $hBitmap = _GDIPlus_BitmapCreateFromGraphics($aWinSize[0], $aWinSize[1], $hGraphic)
    Local $hBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsFillRect($hBuffer, 0, 0, $aWinSize[0],  $aWinSize[1], $BGBrush)
    $aInfo[0].X = Round (($aWinSize[0]-$aInfo[0].Width)/2)
    _GDIPlus_GraphicsDrawStringEx($hBuffer, $StringToDraw, $hFont, $tLayout, $hFormat, $hBrush)
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $aWinSize[0], $aWinSize[1])

    GUICtrlSetData($aInfoSZ,$aInfo[0].X & ":" & $aInfo[0].Y & ":" & $aInfo[0].Width & ":" & $aInfo[0].Height)
    GUICtrlSetData($FSText,$FontSize & ":" & $aInfo[1] & ":" & $aInfo[2])

    ; Clean up resources
    _GDIPlus_GraphicsDispose($hBuffer)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_BrushDispose($bgBrush)
    _GDIPlus_GraphicsDispose($hGraphic)

EndFunc

 

Link to comment
Share on other sites

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...