Jump to content
Sign in to follow this  
dg0421

Auto Resizing/Scaling Fonts within Controls

Recommended Posts

dg0421

How can I automatically resize the fonts that are within a control? In my program the controls all scale automatically when the GUI is resized by the user, but like the font to be scaled as well.

Share this post


Link to post
Share on other sites
simbo

I'm sure someone will have a better solution, but I use the $BS_BITMAP style and set a BMP image using GUICtrlSetImage.

It's rather time consuming, but seems to work. AFAIK Windows just doesn't do scalable fonts on controls.

Matt

How can I automatically resize the fonts that are within a control? In my program the controls all scale automatically when the GUI is resized by the user, but like the font to be scaled as well.

Share this post


Link to post
Share on other sites
rasim

dg0421

Welcome! Show your code, please.

Share this post


Link to post
Share on other sites
dg0421

The attached code will allow a GUI to be resized by the user and the label control also resizes but the font doesn't. I would like the font to auto-resize with the control.

#include <GUIConstants.au3>

$StatusView = GUICreate("Window Title",600,400,-1,-1,$WS_SIZEBOX+$WS_SYSMENU,$WS_EX_TOPMOST)

GUISetFont (14,400,-1,"Arial")

$CtrlStatus = GUICtrlCreateLabel("This is a test",0,0,600,150, $SS_Center)

GUICtrlSetResizing ($CtrlStatus,$GUI_DOCKAUTO)

GUICtrlSetColor($CtrlStatus,0x000000)

GUICtrlSetFont($CtrlStatus,10,400,-1,"Arial Black")

GUICtrlSetState($CtrlStatus,$GUI_Show)

GUISetState (@SW_SHOW,$StatusView)

While 1

$Event = GUIGetMsg()

If $Event = $GUI_EVENT_CLOSE Then

ExitLoop

EndIf

WEnd

Test.au3

Share this post


Link to post
Share on other sites
rasim

dg0421

Try this:

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

$StatusView = GUICreate("Window Title",600,400,-1,-1, BitOR($WS_SIZEBOX, $WS_SYSMENU), $WS_EX_TOPMOST) 
GUISetFont(14, 400, Default, "Arial")

$CtrlStatus = GUICtrlCreateLabel("This is a test", 0, 0, 600, 20, BitOR($SS_CENTER, $SS_CENTERIMAGE))
GUICtrlSetResizing(-1, $GUI_DOCKAUTO)
GUICtrlSetBkColor(-1, 0xFF0000)
GUICtrlSetFont(-1, 10, 400, Default, "Arial Black")

GUIRegisterMsg($WM_SIZE, "WM_SIZE")

GUISetState(@SW_SHOW, $StatusView)

While 1
    $Event = GUIGetMsg()
    If $Event = $GUI_EVENT_CLOSE Then
        ExitLoop
    EndIf
WEnd

Func WM_SIZE($hWnd, $Msg, $wParam, $lParam)
    Local $aPos = ControlGetPos($StatusView, "", $CtrlStatus)
    Local $iSize = $aPos[3] - 4
    
    If $iSize > 10 Then
        $iSize = 10
    ElseIf $iSize <= 0 Then
        $iSize = 1
    EndIf
    
    GUICtrlSetFont($CtrlStatus, $iSize, 400, Default, "Arial Black")

    Return $GUI_RUNDEFMSG
EndFunc

Share this post


Link to post
Share on other sites
dg0421

dg0421

Try this:

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

$StatusView = GUICreate("Window Title",600,400,-1,-1, BitOR($WS_SIZEBOX, $WS_SYSMENU), $WS_EX_TOPMOST) 
GUISetFont(14, 400, Default, "Arial")

$CtrlStatus = GUICtrlCreateLabel("This is a test", 0, 0, 600, 20, BitOR($SS_CENTER, $SS_CENTERIMAGE))
GUICtrlSetResizing(-1, $GUI_DOCKAUTO)
GUICtrlSetBkColor(-1, 0xFF0000)
GUICtrlSetFont(-1, 10, 400, Default, "Arial Black")

GUIRegisterMsg($WM_SIZE, "WM_SIZE")

GUISetState(@SW_SHOW, $StatusView)

While 1
    $Event = GUIGetMsg()
    If $Event = $GUI_EVENT_CLOSE Then
        ExitLoop
    EndIf
WEnd

Func WM_SIZE($hWnd, $Msg, $wParam, $lParam)
    Local $aPos = ControlGetPos($StatusView, "", $CtrlStatus)
    Local $iSize = $aPos[3] - 4
    
    If $iSize > 10 Then
        $iSize = 10
    ElseIf $iSize <= 0 Then
        $iSize = 1
    EndIf
    
    GUICtrlSetFont($CtrlStatus, $iSize, 400, Default, "Arial Black")

    Return $GUI_RUNDEFMSG
EndFunc
That's pretty cool! The font does automatically scale down but when I make the GUI larger (by dragging right corner) the font doesn't get larger. I looked at the GUIRegister function in the help guide but it wasn't immediatly apparent to me how this works. Is there a way to get it to scale both larger and smaller?

Share this post


Link to post
Share on other sites
dg0421

RASIM --

OK .... Thanks a bunch ..... U ROCK!!! I slightly changed your routine and got it to work for my application. Thanks again.

Share this post


Link to post
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
Sign in to follow this  

×