Jump to content
Sign in to follow this  
litlmike

Technique for GUI Creation using Arrays and Resizing

Recommended Posts

litlmike

I am looking for the 'proper technique' on how to create a GUI from an array, have the controls 'autosize', and as the GUI controls are created to resize the GUI accordingly. So, I will have a table of data. From that table, a GUI will create controls. Those controls should 'autosize' based upon the text from the array elements. The GUI then should resize to fit the GUI controls.

The code I have so far is below, but the Url has been changed (its internal). Thanks:

#include <IE.au3>
#include <Array.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$sUrl ="http://www.baseball-reference.com/"
$oIE = _IECreate ($sUrl, 1)

    $oTable = _IETableGetCollection ($oIE, 3)
    $aTableData = _IETableWriteToArray ($oTable, True)


$Left =15
$Top = 15
$Width=650
$Height=450

$Form1 = GUICreate("Form1", $Width, $Height, 100, 100)

For $iCC=0 To UBound($aTableData)-1
    $Edit1 = GUICtrlCreateLabel("", $Left, $Top)
    GUICtrlSetData (-1, $aTableData[$iCC][1])
    $Top+=50
Next
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Share this post


Link to post
Share on other sites
litlmike

I think I scared everyone away with my big quesiton. How about this: how do I get a label (Gui Ctrl) to autosize based upon the amount of text to be placed into the label?

Edited by litlmike

Share this post


Link to post
Share on other sites
litlmike

Here is a better working example of what I am trying to accomplish. For right now, the only thing I am trying to resolve is how to set the location (left, top) for each newly assigned Label. If it is possible, I would like to set some kind of parameters that will create the new labels and the Y-Coords (Top) depending on the previous label. The difficulty I am having is the previous labels height will vary. For now, I set the height of the labels as a fixed value, but I would prefer that it were dynamic dependent on the amount of text in the label. Is there a way to do this? Maybe, is there a way to detect the height of a Gui Control?

#include <IE.au3>
#include <Array.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <NumberFormat.au3>
#Include <Date.au3>
#include <DateTimeConstants.au3>

InvoiceClient_Page2()

Func InvoiceClient_Page2()
    $sSF = "New askldjfiua edition Aasldkjfka membership on laskjdflkajsdfjalskd - CATEGORY(s): [xxxxxxxxx], with [Online Membership Access to Survey] [Pdf] [UPS] [Mail] [Survey]."
    $sSFdad = "Additional Aasldkjfka Categories:  Your PL Aasldkjfka(s) has/have been qualified for and will be incuded in the following PL ldkflas categories: XXXXXXXXXXXXX, XXXXXXXXXXXXX, XXXXXXXXX, XXXXXXXXX, and XXXXXXXXXX to run from [xx-xx-2007 to xx-xx-xx].    "
    $sAA = "Aasldkjfka inclusion into the lkasjdflaaksjdfkjdk network of ldkflas agency websites. Participants agree to work with ldkflas aksdfjk and pay a minimum asidufiausid of 10%."
    $sRB = "Renewal of Aasldkjfka membership on laskjdflkajsdfjalskd - CATEGORY(s): [CATEORY]" & @CRLF & "For Period [MM/DD/YYYY] through [MM/DD/YYYY]."
    $sCISPL = "aksjdlflaksdjf dkjfa dkjfalkd da placement on <u>laskjdflkajsdfjalskd</u> category home page for your akdjfkjdfkj's category. Each Ad spot will be shared with a maximum of 9 alsdkfjd9a and all das will equally rotate with every category home page impression.  The da will link to your aslkdjfkdj dkfjd Aasldkjfka and will run: [START DATE] through [SJLSDJFLSKJD EXPIRATION DATE].  "
    $sCISAA = "aksjdlflaksdjf dkjfa dkjfalkd da placement on the entire <u>lkasjdflaaksjdfkjdk</u> network category home page for your akdjfkjdfkj's category. Each Ad spot will be shared with a maximum of 9 alsdkfjd9a and all das will equally rotate with every category home page impression. The da will link to your aslkdjfkdj dkfjd Aasldkjfka on each participating ldkflas agent's website and will run: [START DATE] through [SJLSDJFLSKJD EXPIRATION DATE]."
    $sFAPL = "months alksdjfkd dlkfjlaksjdfaj davertising placement on <u>laskjdflkajsdfjalskd</u> Homepage. The da spot will be 1 of a maximum of 105 spots in a given month. However, up to 15 das will be displayed randomly on every page loda. Hence the da will have a worst-case scenario of a 1-in-7 chance of being seen. The da will link to your aslkdjfkdj dkfjd Aasldkjfka and will run: January 1, 2007 through [June 30, 2007] [December 31, 2007]."
    $sFAAA = "months alksdjfkd dlkfjlaksjdfaj davertising placement on the <u>lkasjdflaaksjdfkjdk</u> network Homepages. The da spot will be 1 of a maximum of 105 spots in a given month. However, up to 15 das will be displayed randomly on every page loda. Hence the da will have a worst-case scenario of a 1-in-7 chance of being seen. The da will link to your aslkdjfkdj dkfjd Aasldkjfka and will run: January 1, 2007 through [June 30, 2007] [December 31, 2007]."
    $sDA = "10,000 adkjf9is davertisement impressions on laskjdflkajsdfjalskd with link to Aasldkjfka. Ad will appear [alksdjf ious|in the following categories:]"
    Local $aStrings[9] = [$sSF, $sSFdad, $sAA, $sRB, $sCISPL, $sCISAA, $sFAPL, $sFAAA, $sDA] ;Original Strings

    ;Set [x,y] for GUI and Controls
    $Left = 15
    $Top = 15
    $Width = 600
    $Height = 75
    $Total = 0
    $Increment = 15
    $Width_Gui = 1000
    $Height_Gui = 800
    $Form1 = GUICreate("Invoice Items", $Width_Gui, $Height_Gui, 0, 0, $WS_EX_TOPMOST + $WS_SIZEBOX) ;$WS_SIZEBOX

    ;Create labels and inputs
    For $iCC = 1 To UBound($aStrings) - 1
        Assign("Label" & $iCC, GUICtrlCreateLabel("", $Left, $Top, $Width, $Height)) ;Creates Labels dynamically
        GUICtrlSetData(-1, $aStrings[$iCC]) ;Set Label text dynamically
        GUICtrlSetResizing(-1, $GUI_DOCKLEFT)
        $Top += $Increment + $Height ;Add X value to Y coord
    Next

    GUISetState(@SW_SHOW)
    
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
        EndSwitch
    WEnd

EndFunc   ;==>InvoiceClient_Page2

Share this post


Link to post
Share on other sites
PsaltyDS

You need a way to find the size of the text in pixels. The only thing that comes to mind is _WinAPI_GetTextExtentPoint32().

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
litlmike

You need a way to find the size of the text in pixels. The only thing that comes to mind is _WinAPI_GetTextExtentPoint32().

:)

Hmmmm, I didn't expect it to get that messy, I assumed this was a commonly done thing and I was just missing the obvious. I may have to re-think my original goals. In the meantime, what does it mean that "$hDC Identifies the device contex"? I tried this, but received "the parameter is incorrect".

;Create labels and inputs
    For $iCC = 1 To UBound($aStrings) - 1
        Assign("Label" & $iCC, GUICtrlCreateLabel("", $Left, $Top, $Width, $Height));Creates Labels dynamically
        GUICtrlSetData(-1, $aStrings[$iCC]);Set Label text dynamically
        GUICtrlSetResizing(-1, $GUI_DOCKLEFT)
        $Test = _WinAPI_GetTextExtentPoint32(Eval("Label" & $iCC), $aStrings[$iCC])
        ConsoleWrite( "$Test = " & $Test & @CR)

        $Top += $Increment + $Height;Add X value to Y coord
    Next

Share this post


Link to post
Share on other sites
PsaltyDS

Hmmmm, I didn't expect it to get that messy, I assumed this was a commonly done thing and I was just missing the obvious. I may have to re-think my original goals. In the meantime, what does it mean that "$hDC Identifies the device contex"? I tried this, but received "the parameter is incorrect".

;Create labels and inputs
    For $iCC = 1 To UBound($aStrings) - 1
        Assign("Label" & $iCC, GUICtrlCreateLabel("", $Left, $Top, $Width, $Height));Creates Labels dynamically
        GUICtrlSetData(-1, $aStrings[$iCC]);Set Label text dynamically
        GUICtrlSetResizing(-1, $GUI_DOCKLEFT)
        $Test = _WinAPI_GetTextExtentPoint32(Eval("Label" & $iCC), $aStrings[$iCC])
        ConsoleWrite( "$Test = " & $Test & @CR)

        $Top += $Increment + $Height;Add X value to Y coord
    Next
Device context is retrieved by _WinAPI_GetDC(), which is critical. The size in pixels will be context sensitive to selected font, font size, etc. , which come from the DC.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
litlmike

Device context is retrieved by _WinAPI_GetDC(), which is critical. The size in pixels will be context sensitive to selected font, font size, etc. , which come from the DC.

:)

Bah! Which would return the window handle, but not the label handle. So then, I would have to find the label handle (not sure how to), pass that to _WinAPI_GetTextExtentPoint32, then hope that is successful in returning the correct width|height?

Share this post


Link to post
Share on other sites
PsaltyDS

Bah! Which would return the window handle, but not the label handle. So then, I would have to find the label handle (not sure how to), pass that to _WinAPI_GetTextExtentPoint32, then hope that is successful in returning the correct width|height?

ControlGetHandle().

But I haven't tried this myself with a control handle vice the window's handle.

You're a good guinea pig, post your results!

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
litlmike

ControlGetHandle().

But I haven't tried this myself with a control handle vice the window's handle.

You're a good guinea pig, post your results!

:)

I get the same error. Here is what I tried, let me know if I missed something, but I think I am passing the correct HWND's.

;Create labels and inputs
    For $iCC = 1 To UBound($aStrings) - 1
        Assign("Label" & $iCC, GUICtrlCreateLabel("", $Left, $Top, $Width, $Height)) ;Creates Labels dynamically
        GUICtrlSetData(-1, $aStrings[$iCC]) ;Set Label text dynamically
        GUICtrlSetResizing(-1, $GUI_DOCKLEFT)
        
        Local $hDC
        $hDC = _WinAPI_GetDC($Form1)
        ConsoleWrite ("Display Device: " & $hDC & @CR)
                
        $HWND_ControlID = ControlGetHandle ( $Form1, "", Eval("Label" & $iCC))
        ConsoleWrite ("$HWND_ControlID: " & $HWND_ControlID & @CR)
        ConsoleWrite ('EvalEval("Label" & $iCC) = ' & Eval("Label" & $iCC) & @CR & @CR)
        
        $Test = _WinAPI_GetTextExtentPoint32($HWND_ControlID, $aStrings[$iCC])
        ConsoleWrite( "$Test = " & $Test & @CR)

        _WinAPI_ReleaseDC($Form1, $hDC)
        
        $Top += $Increment + $Height ;Add X value to Y coord
    Next

Share this post


Link to post
Share on other sites
PsaltyDS

Well, your script is way too complicated to use for working this out. Here is a simpler demo (that doesn't work):

#include <GuiConstants.au3>
#include <WinAPI.au3>

Global $hGUI = GUICreate("Test", 400, 300)
GUISetFont(10); Set GUI to 10pt
Global $IdLabel = GUICtrlCreateLabel("This is a Label control", 20, 20, 360, 20)
GUICtrlSetFont(-1, 12); Set label to 12pt font
Global $IdButton = GUICtrlCreateButton("OK", 150, 250, 100, 30)
GUISetState()

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

Func _ReadData()
    Local $sString = "This is my string.  There are many like it but this one is mine."
    Local $hDC_Win = _WinAPI_GetDC($hGUI)
    ConsoleWrite("Device Context (Window): " & $hDC_Win & @LF)
    Local $tSIZE = _WinAPI_GetTextExtentPoint32($hDC_Win, $sString)
    ConsoleWrite("In GUI context:  Width = " & DllStructGetData($tSIZE, 1) & "  Height = " & DllStructGetData($tSIZE, 2) & @LF)
    $tSIZE = 0
    _WinAPI_ReleaseDC($hGUI, $hDC_Win)
    
    Local $hLabel = ControlGetHandle($hGUI, "", $IdLabel)
    ConsoleWrite("Control Handle (Label): " & $hLabel & @LF)
    Local $hDC_Label = _WinAPI_GetDC($hLabel)
    ConsoleWrite("Device Context (Label): " & $hDC_Label & @LF)
    $tSIZE = _WinAPI_GetTextExtentPoint32($hDC_Label, $sString)
    ConsoleWrite("In Label context:  Width = " & DllStructGetData($tSIZE, 1) & "  Height = " & DllStructGetData($tSIZE, 2) & @LF)
    _WinAPI_ReleaseDC($hGUI, $hDC_Label)
EndFunc  ;==>_ReadData

The GUI font is set to 10pt and the Label font is 12pt. So the same string should read different width/height in the two contexts (I thought), but they both get the exact same for me...

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
litlmike

Well, your script is way too complicated to use for working this out. Here is a simpler demo (that doesn't work): ...The GUI font is set to 10pt and the Label font is 12pt. So the same string should read different width/height in the two contexts (I thought), but they both get the exact same for me...

Well darn. I liked your approach, but too bad it didn't quite work. I especially like your $sString! I laughed soo hard.

Here is what I decided to do instead, it didn't quite allow me to do everything I originally intended, but I think for now it will give me something that is passable and I can research our little approach in the mean time.

#include <DateTimeConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

$Form1 = GUICreate("Tester", 593, 574, 193, 115, $WS_SIZEBOX) ;, $WS_SIZEBOX

$X_Spacer = 15
$Y_Spacer = 15
$Left1 = 20
$Top1 = 20
Local $Width1 = 200, $Width2 = 100, $Width3 = 200, $Width4 = 50, $Width5 =75
Local $Height1 = 35, $Height2 = 20, $Height3 = 20

$Left2 = $Left1 + $Width1 + $X_Spacer
$Left3 = $Left2 + $Width2 + $X_Spacer
$Left4 = $Left3 + $Width3 + $X_Spacer

;Make Headers Using arrays
Local $aHeaders[4] = ["Description", "Price", "Start Date (Optional)", "End Date (Optional)"]
Local $aWidthHeader[4] = [$Width1, $Width2, $Width3, $Width3]
$NewLeft = $Top1
For $iCC = 0 To UBound($aHeaders) - 1
    Assign("Label_" & $iCC, GUICtrlCreateLabel($aHeaders[$iCC], $NewLeft, $Top1, $aWidthHeader[$iCC], $Height1, $ES_CENTER))
    GUICtrlSetFont(-1, 10, 600, 4)
    GUICtrlSetResizing(-1, $GUI_DOCKALL)
    $GetPos = ControlGetPos($Form1, "", Eval("Label_" & $iCC))
    $NewLeft = $GetPos[0] + $GetPos[2] + $X_Spacer
Next

;Make Main Controls (Dynamic)
$NewTop = $Top1 + $Y_Spacer * 2
For $i = 1 To 8
    Assign("Label_" & $i, GUICtrlCreateLabel("Too way meet sons him Too way meet sons him ", $Left1, $NewTop, $Width1, $Height1))
    GUICtrlSetResizing(-1, $GUI_DOCKALL)
    Assign("Input_" & $i, GUICtrlCreateInput("Input1", $Left2, $NewTop, $Width2, $Height2, $ES_CENTER))
    GUICtrlSetResizing(-1, $GUI_DOCKALL)
    Assign("Date_S" & $i, GUICtrlCreateDate("2008/06/17 07:14:19", $Left3, $NewTop, $Width3, $Height3))
    GUICtrlSetResizing(-1, $GUI_DOCKALL)
    Assign("Date_E" & $i, GUICtrlCreateDate("2008/06/17 07:14:19", $Left4, $NewTop, $Width3, $Height3))
    GUICtrlSetResizing(-1, $GUI_DOCKALL)
    $GetPos = ControlGetPos($Form1, "", Eval("Label_" & $i))
    $NewTop = $GetPos[1] + $GetPos[3] + $Y_Spacer
Next
$i -= 1 ;To .cf the last created ctrl

;Make Other Controls (Semi-Dynamic)
$TotalAmountLabel = GUICtrlCreateLabel("Total $", $Left1 + $X_Spacer, $NewTop, $Width2, $Height2, $ES_RIGHT)
GUICtrlSetResizing(-1, $GUI_DOCKALL)
GUICtrlSetFont(-1, 8, 600)
$TotalAmount = GUICtrlCreateInput("Total $", $Left2, $NewTop, $Width2, $Height2, $ES_CENTER)
GUICtrlSetResizing(-1, $GUI_DOCKALL)

$GetPos = ControlGetPos($Form1, "", Eval("Date_S" & $i))
$SplitPaymentsLabel = GUICtrlCreateLabel("# of Split Payments", $GetPos[0] , $NewTop, $Width2, $Height2)
GUICtrlSetResizing(-1, $GUI_DOCKALL)
GUICtrlSetFont(-1, 8, 400)
$SplitPayments = GUICtrlCreateCombo("1", $GetPos[0] + $GetPos[2] - $Width2, $NewTop, $Width4, $Height2)
GUICtrlSetResizing(-1, $GUI_DOCKALL)

$ButtonOK = GUICtrlCreateButton("&Calculate", $Left4 , $NewTop, $Width5, 20)
GUICtrlSetResizing(-1, $GUI_DOCKALL)
$ButtonCancel = GUICtrlCreateButton("&Done", $Left4 + $X_Spacer + $Width5, $NewTop, 75, 20)
GUICtrlSetResizing(-1, $GUI_DOCKALL)


;Resize/Move GUI
$GetPos = ControlGetPos($Form1, "", Eval("Date_E" & $i))
$GUI_Width = $GetPos[0] + $GetPos[2] + $Left1
$GetPos = ControlGetPos($Form1, "", $TotalAmount)
$GUI_Height = $GetPos[1] + $GetPos[3] + $Y_Spacer * 3
WinMove($Form1, "", 0, 0, $GUI_Width, $GUI_Height)

GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

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  

×