Jump to content
Sign in to follow this  
PhoenixXL

Control Resize

Recommended Posts

PhoenixXL

Hey Every1

Is it Possible to Resize a Label

Reason: Using GuiCtrlSetData when the Text is Too Long it Doesn't Fit the Label and is not Displayed

If its possible to Resize the Label a Offset Could be Created and The Text Would Also be Able to get Displayed

Thnx for Your Time ;)

Regards

Phoenix XL

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
BrewManNH

Look at Melba23's StringSize UDF in the Example Scripts section, with that you send it the text you want to display, the font and size of the font and it will return a value to be used for a control.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
UEZ

I don't know whether this is a good approach here:

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

_GDIPlus_Startup()
Global $hGUI = GUICreate("Test", 800, 200, -1, -1, BitOR($WS_MINIMIZEBOX, $WS_SIZEBOX, $WS_CAPTION, $WS_POPUP, $WS_SYSMENU, $WS_MAXIMIZEBOX), $WS_EX_COMPOSITED)
Global $idGroup = GUICtrlCreateGroup("", 224, 0, 569, 92)
Global $sLabel = "Scale this label dynamically"
Global $fFontsize, $sFont = "Impact"
Global $idLabel = GUICtrlCreateLabel($sLabel, 228, 14,  563, 76, $SS_CENTER)
GUICtrlSetResizing(-1, $GUI_DOCKAUTO)
GUICtrlSetColor(-1, 0x000080)
Global $idLabelDummy = GUICtrlCreateLabel("", 228, 14,  563, 76)
GUICtrlSetResizing(-1, $GUI_DOCKAUTO)
GUICtrlSetState(-1, $GUI_DISABLE)
GUICtrlCreateGroup("", -99, -99, 1, 1)

Resize_Font()
GUISetState(@SW_SHOW)

Global $aWDim = WinGetPos($hGUI)
Global $w = $aWDim[2], $h = $aWDim[3]
GUIRegisterMsg($WM_GETMINMAXINFO, "WM_GETMINMAXINFO")
GUIRegisterMsg($WM_SIZE, "WM_SIZE")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            GUIRegisterMsg($WM_GETMINMAXINFO, "")
            GUIRegisterMsg($WM_SIZE, "")
            GUIDelete($hGUI)
            _GDIPlus_Shutdown()
            Exit
        Case $GUI_EVENT_RESIZED, $GUI_EVENT_MAXIMIZE, $GUI_EVENT_RESTORE
            Resize_Font()
    EndSwitch
WEnd

Func Resize_Font()
    $aPOS = ControlGetPos($hGUI, "", $idLabelDummy)
    GUICtrlSetPos($idLabel, $aPOS[0] + 2, $aPOS[1] + 8, $aPOS[2] - 6, $aPOS[3] - 12)
    $fFontsize = CalcFontSize($sLabel, $aPOS[2] - 6, $aPOS[3] - 12, $sFont)
    GUICtrlSetFont($idLabel, $fFontsize, 400, 0, $sFont, 5)
EndFunc

Func CalcFontSize($sString, $iW, $iH, $sFont)
    Local $aResult, $hFont, $i, $iLen = StringLen($sString)
    Local $hGfx = _GDIPlus_GraphicsCreateFromHWND(WinGetHandle(AutoItWinGetTitle()))
    Local $tRectF = DllStructCreate($tagGDIPRECTF)
    Local $tLayout = _GDIPlus_RectFCreate(0, 0, $iW, $iH)
    Local $hFormat = _GDIPlus_StringFormatCreate()
    Local $hFamily = _GDIPlus_FontFamilyCreate($sFont)
    $i = 1
    Do
        $hFont = _GDIPlus_FontCreate($hFamily, $i, 0)
        $aResult = DllCall($ghGDIPDll, "int", "GdipMeasureString", "handle", $hGfx, "wstr", $sString, "int", -1, "handle", $hFont, "struct*", $tLayout, "handle", $hFormat, "struct*", $tRectF, "int*", 0, "int*", 0)
        _GDIPlus_FontDispose($hFont)
        If $aResult[8] <  $iLen Or $aResult[9] = 2 Or $i > $iH / 1.45 Then ExitLoop
        $i += 1
    Until False
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_GraphicsDispose($hGfx)
    $tLayout  = 0
    Return $i
EndFunc

Func WM_SIZE($hWnd, $Msg, $wParam, $lParam)
    Resize_Font()
    Return "GUI_RUNDEFMSG"
EndFunc

Func WM_GETMINMAXINFO($hWnd, $Msg, $wParam, $lParam)
        Local $minmaxinfo = DllStructCreate("int;int;int;int;int;int;int;int;int;int", $lParam)
        DllStructSetData($minmaxinfo, 7, $w) ; min W
        DllStructSetData($minmaxinfo, 8, $h) ; min H
    Return "GUI_RUNDEFMSG"
EndFunc   ;==>WM_GETMINMAXINFO

But you can try it.

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
JScript

Probably what he wants is this:

; ##### JScript - 23/05/2012 #####
#include <GUIConstantsEx.au3>

Example()

Func Example()
    Local $msg, $iLabel

    GUICreate("My GUI", 360, 240) ; will create a dialog box that when displayed is centered

    $iLabel = GUICtrlCreateLabel("Initial text...", 10, 30)

    GUISetState() ; will display an empty dialog box

    Sleep(2000)

    _GUILabelSetText($iLabel, "This is the second text into the label control, fit!")

    Sleep(2000)

    _GUILabelSetText($iLabel, "This is the third text will be fully visible in label control, Autoit is the best!")

    ; Run the GUI until the dialog is closed
    Do
        $msg = GUIGetMsg()
    Until $msg = $GUI_EVENT_CLOSE

EndFunc   ;==>Example


Func _GUILabelSetText($iCtrlID, $sText)
    Local $asSize

    $asSize = _StringSize($sText)

    ControlMove("", "", $iCtrlID, Default, Default, $asSize[0], $asSize[1])
    Return GUICtrlSetData($iCtrlID, $sText)
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _StringSize
; Description ...: Returns the size (in pixels) of an string.
; Syntax.........: _StringSize( "string" [, size [, weight [, fontname ]]] )
; Parameters ....: string   - The string to evaluate the size.
;                 Size   - [Optional] Fontsize (default is 9).
;                 Weight     - [Optional] Font weight (default 400 = normal).
;                 FontName  - [Optional] Font to use (OS default GUI font is used if the font is "" or is not found).
; Requirement(s).:
; Return values .: Success   - Returns a 2-element array containing the following information:
;                           $array[0] = Width
;                           $array[1] = Height
;                  Failure   - Returns the same array with 0 and sets @error to 1.
; Author ........: jscript
; Example .......: _StringSize( "Text" )
; ===============================================================================================================================
Func _StringSize($sString, $iSize = 9, $iWeight = 400, $sFontName = "")
    Local $hWnd, $hGuiSwitch, $iCtrlID, $aCtrlSize, $aRetSize[2] = [0, 0]

    $hWnd = GUICreate("StringExInternalWin", 0, 0, 0, 0, BitOR(0x80000000, 0x20000000), BitOR(0x00000080, 0x00000020))
    $hGuiSwitch = GUISwitch($hWnd)

    If $iSize = 65535 Then ; Used by _ImageSize
        $iCtrlID = GUICtrlCreatePic($sString, 0, 0, 0, 0)
    Else
        GUISetFont($iSize, $iWeight, -1, $sFontName, $hWnd)
        $iCtrlID = GUICtrlCreateLabel($sString, 0, 0)
    EndIf
    $aCtrlSize = ControlGetPos($hWnd, "", $iCtrlID)
    GUIDelete($hWnd)
    GUISwitch($hGuiSwitch)

    If IsArray($aCtrlSize) Then
        $aRetSize[0] = $aCtrlSize[2]; Width
        $aRetSize[1] = $aCtrlSize[3]; Height
        Return SetError(0, 0, $aRetSize)
    EndIf
    Return SetError(1, 0, $aRetSize)
EndFunc   ;==>_StringSize

Regards,

João Carlos.

Edited by JScript

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Share this post


Link to post
Share on other sites
UEZ

@JScript: you are probably right but the code can be adjusted easily:

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

_GDIPlus_Startup()
Global $hGUI = GUICreate("Test", 800, 200)
Global $sLabel = "This is the 1st text"
Global $fFontsize, $sFont = "Arial"
Global $idLabel = GUICtrlCreateLabel($sLabel, 228, 14,  563, 76, $SS_CENTER)
GUICtrlSetColor(-1, 0x000080)
Resize_Font($sLabel, $idLabel)
GUISetState(@SW_SHOW)

Sleep(2000)

$sLabel =  "And the one is the 2nd label text which is longer!"
Resize_Font($sLabel, $idLabel)
GUICtrlSetData($idLabel, $sLabel)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            GUIDelete($hGUI)
            _GDIPlus_Shutdown()
            Exit
    EndSwitch
WEnd

Func Resize_Font($sLabel, $idControl)
    $aPOS = ControlGetPos($hGUI, "", $idControl)
    $fFontsize = CalcFontSize($sLabel, $aPOS[2], $aPOS[3], $sFont)
    GUICtrlSetFont($idLabel, $fFontsize, 400, 0, $sFont, 5)
EndFunc

Func CalcFontSize($sString, $iW, $iH, $sFont)
    Local $aResult, $hFont, $i, $iLen = StringLen($sString)
    Local $hGfx = _GDIPlus_GraphicsCreateFromHWND(WinGetHandle(AutoItWinGetTitle()))
    Local $tRectF = DllStructCreate($tagGDIPRECTF)
    Local $tLayout = _GDIPlus_RectFCreate(0, 0, $iW, $iH)
    Local $hFormat = _GDIPlus_StringFormatCreate()
    Local $hFamily = _GDIPlus_FontFamilyCreate($sFont)
    $i = 1
    Do
        $hFont = _GDIPlus_FontCreate($hFamily, $i, 0)
        $aResult = DllCall($ghGDIPDll, "int", "GdipMeasureString", "handle", $hGfx, "wstr", $sString, "int", -1, "handle", $hFont, "struct*", $tLayout, "handle", $hFormat, "struct*", $tRectF, "int*", 0, "int*", 0)
        _GDIPlus_FontDispose($hFont)
        If $aResult[8] <  $iLen Or $aResult[9] = 2 Or $i > $iH / 1.45 Then ExitLoop
        $i += 1
    Until False
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_GraphicsDispose($hGfx)
    $tLayout  = 0
    Return $i
EndFunc

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
PhoenixXL

thanks UEZ JScript Brewman and Zedna now it works

@Zedna I have didnt start any project for decapitalization i think i would not do it anymore (hope so ;))

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
PhoenixXL

Yeah the code of UEZ

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
_GDIPlus_Startup()
Global $hGUI = GUICreate("Test", 800, 200)
Global $sLabel = "This is the 1st text"
Global $fFontsize, $sFont = "Arial"
Global $idLabel = GUICtrlCreateLabel($sLabel, 228, 14,  563, 76, $SS_CENTER)
GUICtrlSetColor(-1, 0x000080)
Resize_Font($sLabel, $idLabel)
GUISetState(@SW_SHOW)
Sleep(2000)
$sLabel =  "And the one is the 2nd label text which is longer!"
Resize_Font($sLabel, $idLabel)
GUICtrlSetData($idLabel, $sLabel)
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            GUIDelete($hGUI)
            _GDIPlus_Shutdown()
            Exit
    EndSwitch
WEnd
Func Resize_Font($sLabel, $idControl)
    $aPOS = ControlGetPos($hGUI, "", $idControl)
    $fFontsize = CalcFontSize($sLabel, $aPOS[2], $aPOS[3], $sFont)
    GUICtrlSetFont($idLabel, $fFontsize, 400, 0, $sFont, 5)
EndFunc
Func CalcFontSize($sString, $iW, $iH, $sFont)
    Local $aResult, $hFont, $i, $iLen = StringLen($sString)
    Local $hGfx = _GDIPlus_GraphicsCreateFromHWND(WinGetHandle(AutoItWinGetTitle()))
    Local $tRectF = DllStructCreate($tagGDIPRECTF)
    Local $tLayout = _GDIPlus_RectFCreate(0, 0, $iW, $iH)
    Local $hFormat = _GDIPlus_StringFormatCreate()
    Local $hFamily = _GDIPlus_FontFamilyCreate($sFont)
    $i = 1
    Do
        $hFont = _GDIPlus_FontCreate($hFamily, $i, 0)
        $aResult = DllCall($ghGDIPDll, "int", "GdipMeasureString", "handle", $hGfx, "wstr", $sString, "int", -1, "handle", $hFont, "struct*", $tLayout, "handle", $hFormat, "struct*", $tRectF, "int*", 0, "int*", 0)
        _GDIPlus_FontDispose($hFont)
        If $aResult[8] <  $iLen Or $aResult[9] = 2 Or $i > $iH / 1.45 Then ExitLoop
        $i += 1
    Until False
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_GraphicsDispose($hGfx)
    $tLayout  = 0
    Return $i
EndFunc

gave me this error

C:UsersabhishekDesktopPresentationAbhishekSCriptsMy_Doubt.au3 (48) : ==> Subscript used with non-Array variable.:
If $aResult[8] <  $iLen Or $aResult[9] = 2 Or $i > $iH / 1.45 Then ExitLoop
If $aResult^ ERROR
->23:05:20 AutoIT3.exe ended.rc:1
>Exit code: 1    Time: 2.183

But the code of JScript(yours ;) )

worked flawlessly and i was able to do with it

thnx

Regards

Phoenix XL


My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
UEZ

Very strange that it crashes for you! It works for me on Win7 x64, WinXP x86 (vm).

What os are you using?

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
PhoenixXL

I'm Using Win7 32Bit 2GB Ram and Build 7600


My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
UEZ

I don't know why "GdipMeasureString" call is not working for you. How you are calling the function?

Further the error check is not implemented yet.

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
PhoenixXL

Updated the Autoit to the latest version

and now UEZ's code also works ;)

Thnx Guys


My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

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  

×