Jump to content

Label Automatic Width (Fit Text)


JFee
 Share

Recommended Posts

Ok, now I found really something weird. A very long line gets calculated too short, despite the correct use of the style parameter.

Here's a short example:

#NoTrayIcon
Opt("GuiOnEventMode", 1)

#include <GuiConstants.au3>
#include <StaticConstants.au3>
#include <GDIPlus.au3>

$fontName = "Tahoma"
$fontSize = 9




$guiMain = GUICreate("", 500, 200)
GUISetOnEvent($gui_event_close, "quit")
GUISetFont($fontSize, 400, 0, $fontName, $guiMain)

$text = "THIS IS LOOOOOOOOOOOOOOOOOOOONG TEXT"
$len = getTextLengthInPixel($text, "Tahoma", 10, 2) ; bold
GUICtrlCreateLabel($text, 20, 20, $len, 20, $ss_leftnowordwrap)
GUICtrlSetFont(-1, 10, 600)

GUICtrlCreateLabel("Following text", 20 + $len, 20+1, 100, 20)


GUISetState()
While True
    Sleep(1000)
WEnd

Func getTextLengthInPixel($text, $font, $size = 9, $style = 0)
    ;Calculate actual width of the given text
    _GDIPlus_Startup()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($guiMain)
    $hFamily = _GDIPlus_FontFamilyCreate($font)
    $hGDIFont = _GDIPlus_FontCreate($hFamily, $size, $style)
    $hFormat = _GDIPlus_StringFormatCreate()
    $tLayout = _GDIPlus_RectFCreate(0, 0, @DesktopWidth, @DesktopHeight)

    $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $text, $hGDIFont, $tLayout, $hFormat)
    $width = Int(DllStructGetData($aInfo[0], 3)) + 1
    print($width)

    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_FontDispose($hGDIFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()
    Return $width
EndFunc

Func print($s)
    ConsoleWrite($s & @CRLF)
EndFunc

Func quit()
    Exit
EndFunc

For the moment, this workaround while getting the width from the structure will suffice for my thing:

$width = Int(DllStructGetData($aInfo[0], 3)) * 1.05 + 10  ;instead of +1

Maybe it's an issue after all and the dev's wanna have a look into it... ;)

Edited by Der_Andi
Link to comment
Share on other sites

  • 11 years later...
On 8/16/2010 at 9:35 AM, Der_Andi said:

Ok, now I found really something weird. A very long line gets calculated too short, despite the correct use of the style parameter.

 

Here's a short example:

 

#NoTrayIcon
Opt("GuiOnEventMode", 1)

#include <GuiConstants.au3>
#include <StaticConstants.au3>
#include <GDIPlus.au3>

$fontName = "Tahoma"
$fontSize = 9




$guiMain = GUICreate("", 500, 200)
GUISetOnEvent($gui_event_close, "quit")
GUISetFont($fontSize, 400, 0, $fontName, $guiMain)

$text = "THIS IS LOOOOOOOOOOOOOOOOOOOONG TEXT"
$len = getTextLengthInPixel($text, "Tahoma", 10, 2) ; bold
GUICtrlCreateLabel($text, 20, 20, $len, 20, $ss_leftnowordwrap)
GUICtrlSetFont(-1, 10, 600)

GUICtrlCreateLabel("Following text", 20 + $len, 20+1, 100, 20)


GUISetState()
While True
    Sleep(1000)
WEnd

Func getTextLengthInPixel($text, $font, $size = 9, $style = 0)
    ;Calculate actual width of the given text
    _GDIPlus_Startup()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($guiMain)
    $hFamily = _GDIPlus_FontFamilyCreate($font)
    $hGDIFont = _GDIPlus_FontCreate($hFamily, $size, $style)
    $hFormat = _GDIPlus_StringFormatCreate()
    $tLayout = _GDIPlus_RectFCreate(0, 0, @DesktopWidth, @DesktopHeight)

    $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $text, $hGDIFont, $tLayout, $hFormat)
    $width = Int(DllStructGetData($aInfo[0], 3)) + 1
    print($width)

    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_FontDispose($hGDIFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()
    Return $width
EndFunc

Func print($s)
    ConsoleWrite($s & @CRLF)
EndFunc

Func quit()
    Exit
EndFunc

 

For the moment, this workaround while getting the width from the structure will suffice for my thing:

 

$width = Int(DllStructGetData($aInfo[0], 3)) * 1.05 + 10  ;instead of +1

 

Maybe it's an issue after all and the dev's wanna have a look into it... ;)

Any solution to this problem? I have a floating window that being resized based on text, increasing width with static number does not work in my case...

Link to comment
Share on other sites

  • 2 weeks later...
  • Moderators

VAN0,

Have you tried my StringSize UDF - the link is in my sig.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

6 hours ago, Melba23 said:

VAN0,

Have you tried my StringSize UDF - the link is in my sig.

M23

Thanks, just tried it, it has the same issue

P.S. the decision to have height before width in return array is odd ;) 

Edited by VAN0
Link to comment
Share on other sites

Something seems incorrect in this script, but even by modifying one line, the result won't be accurate :

; $len = getTextLengthInPixel($text, "Tahoma", 10, 2) ; bold (?) (no, 2 means italic)
$len = getTextLengthInPixel($text, "Tahoma", 10, 1) ; bold <=========================

The display will change from :

1831978208_looongtext1.png.472a98596139011eea6e9262c730d4a6.png

to

992968179_looongtext2.png.715fa6dd4631abea6f8e9e1f092a3be8.png

For what it's worth...

Link to comment
Share on other sites

On 5/1/2022 at 8:19 AM, VAN0 said:

I have a floating window that being resized based on text, increasing width with static number does not work in my case...

Why not showing your code ?
I'm sure you'll get help if you do so, because Melba23's StringSize function calculates accurately the label width (I tried it just now) :

#include <GUIConstantsEx.au3>
#include "StringSize.au3" ; Melba23's

$hGUI = GUICreate("StringSize Test", 450, 80)
GUISetFont(9, 400, 0, "Arial", $hGUI)

; A label sized by StringSize
$sText = "THIS IS LOOOOOOOOOOOOOOOOOOOONG TEXT"
$aSize = _StringSize($sText, 10, 600, 0, "Tahoma")
GUICtrlCreateLabel($aSize[0], 20, 20, $aSize[2], $aSize[3])
GUICtrlSetFont(-1, 10, 600, 0, "Tahoma")
GUICtrlSetBkColor(-1, 0x80FF80)

GUICtrlCreateLabel("Another line not formatted in any way", 20, 50)

GUISetState()

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

StringSize.png.bb6e99a9d81a44213b20f94de9f7746c.png

Writing exactly 4 same arguments in _StringSize(...) and GUICtrlSetFont(-1, ...) gave a very accurate result as shown in the preceding pic.

Now if your (bold) text had to change within the label, it should be possible to recalculate the label's width (using the StringSize function) and re-display the label accordingly inside the (resized) floating window.

Your move :)
 

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