Jump to content

Automatically size window based on text to display


JOlsson
 Share

Recommended Posts

Hi,

Here is my goal: I want to create a GUI window using AutoIt to display a multi-line text message to the user. The window should have the following characteristics:

  • The window should not be resizable by the user.
  • The window should base its width and height on the multi-line text message to display. That is, the window size should be calculated dynamically by the script based on the longest line in the multi-line text message to display and on how many lines there are in the message.

Essentially, what I want is a MsgBox window that I am able to control the status of and update the text and sizing of dynamically while it is being displayed.

My most immediate questions are:

  • How do I calculate horizontal window size based on number of characters per line in a string?
  • How do I calculate vertical window size based on number of lines in a string?

I realize that this is based on the font size used but it would be helpful with some pointers to get me started.

Link to comment
Share on other sites

consider...

Opt("GUIResizeMode",802)

$testtext1 = "test me" & @LF & "test the hell out of me" & @LF & "mutha tester"
$testtext2 = "I am a test, I am only a test, and I 'm sitting here on capitol hill"

$gui = GUICreate("",300,300)
GUICtrlCreateLabel($testtext1,10,10)
$wh = ControlGetPos($gui,"","Static1")
WinMove($gui,"",20,20,$wh[2]+20,$wh[3]+44)
GUISetState()
Sleep(5000)
GUIDelete($gui)

$gui = GUICreate("",300,300)
GUICtrlCreateLabel($testtext2,10,10)
$wh = ControlGetPos($gui,"","Static1")
WinMove($gui,"",20,20,$wh[2]+20,$wh[3]+44)
GUISetState()
Sleep(5000)
GUIDelete($gui)

You may want to determine the Caption Height programatically to add it to the WinMove() height parameter.

Lar.

f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

Link to comment
Share on other sites

  • Moderators

@Lar,

Your idea is very nearly perfect - and I thought it was the answer to my prayers. But it only works as long as you stick with the default font size! I was experimenting and wondering why it did not work as I expected. Then I discovered that it does not work if you use a GUICtrlSetFont on the label and change the point value or face (unless the combination closely matches the default font). It seems that calling GUICtrlCreateLabel without size parameters assumes that the font is the default - so any attempt to use a different font or point value will not produce the correct sized label.

Try the following:

#include <GUIConstantsEx.au3>

GUICreate("My Test", 110, 150)

GUICtrlCreateLabel("This is a test!", 10, 10)
    GUICtrlSetBkColor(-1, 0xFFFF00)

GUICtrlCreateLabel("This is a test!", 10, 40)
    GUICtrlSetBkColor(-1, 0xFFFF00)
    GUICtrlSetFont(-1, 12)
    
GUICtrlCreateLabel("This is a test!", 10, 70)
    GUICtrlSetBkColor(-1, 0xFFFF00)
    GUICtrlSetFont(-1, Default, Default, 1, "Comic Sans MS")
    
GUICtrlCreateLabel("This is a test!", 10, 100)
    GUICtrlSetBkColor(-1, 0xFFFF00)
    GUICtrlSetFont(-1, Default, Default, 1, "Courier New")

GUISetState()

While 1
    
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then Exit

WEnd

On my machine I get identical label sizes - and only the first and third allow all the text of the message to display. So, if you want to use this technique, be aware that it has its limitations.

M23

Edit:

More experimentation shows that the label does indeed use the size of the GUI font - and using GUISetFont on the GUI produces the label correctly for the chosen font face and size. So the limitation is not as bad as I feared - AutoIt comes up trumps again!

Edited by Melba23

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