Jump to content

Automatic sizing of controls to fit text


WRP
 Share

Go to solution Solved by Melba23,

Recommended Posts

I'm having trouble with labels that don't expand to fit their text. According the docs, this should work, but the bottom two labels are truncated. How does expansion work, in general?
 

#include <GuiConstantsEx.au3>

GUICreate("Label Width Test", 250, 120)

GUICtrlCreateLabel("Five Four Three Two One", 5, 5)
GUICtrlSetBkColor(-1, 0x50cc40)
GUICtrlSetFont(-1, 10, 400, 0, "Arial")

GUICtrlCreateLabel("Six Five Four Three Two One", 5, 35)
GUICtrlSetBkColor(-1, 0x50cc40)
GUICtrlSetFont(-1, 10, 400, 0, "Arial")

GUICtrlCreateLabel("Five Four Three Two One", 5, 65)
GUICtrlSetBkColor(-1, 0x50cc40)
GUICtrlSetFont(-1, 10, 700, 0, "Arial")

GUICtrlCreateLabel("Six Five Four Three Two One", 5, 95)
GUICtrlSetBkColor(-1, 0x50cc40)
GUICtrlSetFont(-1, 10, 700, 0, "Arial")

GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd
Link to comment
Share on other sites

  • Moderators
  • Solution

WRP,

Welcome to the AutoIt forums. :)

Expansion is based on the default GUI font - not the control itself. So if you set the GUI default to 10 point (rather then the normal 8.5) then you will see that the first 2 labels are correctly expanded:

#include <GuiConstantsEx.au3>

GUICreate("Label Width Test", 250, 120)
GUISetFont(-1, 10, 400, 0, "Arial")

GUICtrlCreateLabel("Five Four Three Two One", 5, 5)
GUICtrlSetBkColor(-1, 0x50cc40)
;GUICtrlSetFont(-1, 10, 400, 0, "Arial")

GUICtrlCreateLabel("Six Five Four Three Two One", 5, 35)
GUICtrlSetBkColor(-1, 0x50cc40)
;GUICtrlSetFont(-1, 10, 400, 0, "Arial")

GUICtrlCreateLabel("Five Four Three Two One", 5, 65)
GUICtrlSetBkColor(-1, 0x50cc40)
GUICtrlSetFont(-1, 10, 700, 0, "Arial")

GUICtrlCreateLabel("Six Five Four Three Two One", 5, 95)
GUICtrlSetBkColor(-1, 0x50cc40)
GUICtrlSetFont(-1, 10, 700, 0, "Arial")

GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd
As soon as the label font is not the default GUI font, the expansion no longer works. :(

But you can use my StringSize UDF (look in my sig for the link) to correctly size text in any font face/size/attribute - which will let you correctly size the control regardless of the default font used by the GUI. ;)

I hope that helps - please ask again if you have any questions. :)

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

There may be some useful ideas in >this thread, if not for this particular project, then maybe in similar circumstances in the future. It's not the same thing, but it is related. In the code I posted, the only reason the constants were replaced was because, at the time, I didn't have time to test the code with the latest release of AutoIt. It will run as is. The original code is in the comments (includes are also needed - a little jigsaw puzzle).

Link to comment
Share on other sites

Expansion is based on the default GUI font - not the control itself.

Thanks. Now it's clear what's happening.

I'm trying to get a grip on coding GUIs with AutoIt. As far as I can tell, buttons and labels are the only things that change shape to fit their contents. Is that correct?

Is there a way to have controls rearrange automatically? For example, if a button in a row of buttons changes size, then the buttons on the right shift to the right to make room. I guess for that to work, every other element would have to be subject to re-sizing/positioning. It looks like control positions are always hard-coded, but I wanted to make sure that I hadn't missed something.

Link to comment
Share on other sites

  • Moderators

WRP,

 

Is there a way to have controls rearrange automatically?

Not as far as I know. You can set controls to resize as the GUI changes size (GUICtrlSetResizing) but to amend the size of a row of buttons if one changes size is another kettle of fish. :wacko:

But take a look at my ExtMsgBox UDF. There is some code in there to resize buttons depending on the number of buttons required and the size of the text to contain within them - that might be of some use. Although the idea is to keep all the buttons at the same size rather than adjst their individual sizes. ;)

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

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