Sign in to follow this  
Followers 0
johnmcloud

[SOLVED] Dynamic GUI Creation

5 posts in this topic

#1 ·  Posted (edited)

Hi guys,

I'm try to create a gui can be create element by itself. This is what i have done:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Opt("GUIResizeMode", BitOR($GUI_DOCKTOP, $GUI_DOCKLEFT, $GUI_DOCKSIZE))

Global Const $Element_For_Column = 5 ; Max number of element for column
Global Const $Max_Element = 15 ; Number of total element
Global $iCount = 1 ; Number 1 is used for basic element
Global $Move_Top, $Move_Left, $Temp_iCount
Global $Input_[1][1], $Label_[1][1], $Button_[1][2]

$hGUI = GUICreate("Dynamic GUI", 233, 107, -1, -1)
$Label_A1 = GUICtrlCreateLabel("This is a label n°1", 8, 26, 90, 17)
$Input_A1 = GUICtrlCreateInput("", 8, 43, 161, 21)
$Button_A1 = GUICtrlCreateButton("1", 176, 41, 51, 25)
$Button_A2 = GUICtrlCreateButton("Check Input", 8, 72, 75, 25)
$Button_Minus = GUICtrlCreateButton("-", 176, 2, 19, 25)
$Button_Plus = GUICtrlCreateButton("+", 210, 2, 19, 25)
GUISetState(@SW_SHOW)

While 1
$msg = GUIGetMsg()
Switch $msg
Case $GUI_EVENT_CLOSE
Exit
Case $Button_A1
GUICtrlSetData($Input_A1, "You have clicked button 1")
Case $Button_A2
_Test_Func($Input_A1)
Case $Button_Plus
_Add_Element()
Case $Button_Minus
Delete_Element()
Case Else
For $i = 0 To UBound($Button_, 1) - 1
    Switch $msg
     Case $Button_[$i][0]
     GUICtrlSetData($Input_[$i][0], "You have clicked button " & $i + 1)
     Case $Button_[$i][1]
     _Test_Func($Input_[$i][0])
    EndSwitch
Next
EndSwitch
WEnd

Func _Test_Func($Value)
MsgBox(0, 0, "This is the value of Input: " & GUICtrlRead($Value) & @CRLF)
EndFunc ;==>_Test_Func

Func _Add_Element()
Global $hGUISize_Height = 89, $hGUISize_Width = 250
Local Const $Size = WinGetPos($hGUI)
$Temp_iCount += 1
$iCount += 1
$Move_Top += 90
If $Temp_iCount = $Element_For_Column Then
If $iCount <= $Max_Element Then
WinMove($hGUI, "", Default, Default, $Size[2] + $hGUISize_Width, Default)
_Middle($hGUI, "")
$Move_Top = 0 ; reset top
$Move_Left += 250 ; create left vaule
$Temp_iCount = 0 ; reset temp_element
EndIf
EndIf
If $iCount <= $Max_Element Then
ReDim $Label_[$iCount][$iCount]
ReDim $Input_[$iCount][$iCount]
ReDim $Button_[$iCount][$iCount * 2]
If $iCount <= $Element_For_Column Then
$Label_[$iCount - 1][0] = GUICtrlCreateLabel("This is a label n°" & $iCount, 8, 26 + $Move_Top, 90, 17)
$Input_[$iCount - 1][0] = GUICtrlCreateInput("", 8, 43 + $Move_Top, 161, 21)
$Button_[$iCount - 1][0] = GUICtrlCreateButton($iCount, 176, 41 + $Move_Top, 51, 25)
$Button_[$iCount - 1][1] = GUICtrlCreateButton("Check Input", 8, 72 + $Move_Top, 75, 25)
WinMove($hGUI, "", Default, Default, Default, $Size[3] + $hGUISize_Height)
_Middle($hGUI, "")
Else
$Label_[$iCount - 1][0] = GUICtrlCreateLabel("This is a label n°" & $iCount, 8 + $Move_Left, 26 + $Move_Top, 90, 17)
$Input_[$iCount - 1][0] = GUICtrlCreateInput("", 8 + $Move_Left, 43 + $Move_Top, 161, 21)
$Button_[$iCount - 1][0] = GUICtrlCreateButton($iCount, 176 + $Move_Left, 41 + $Move_Top, 51, 25)
$Button_[$iCount - 1][1] = GUICtrlCreateButton("Check Input", 8 + $Move_Left, 72 + $Move_Top, 75, 25)
EndIf
EndIf
If $iCount > $Max_Element Then
$iCount = $Max_Element
$Temp_iCount = $Element_For_Column - 1
EndIf
EndFunc ;==>_Add_Element

Func Delete_Element()
Local Const $Size = WinGetPos($hGUI)
Local $Temp_Move_Top = 90 ; Equal to $Move_Top
If $iCount = 1 Then ; No element
$iCount = 1
Return
Else
GUICtrlDelete($Label_[$iCount - 1][0]) ; delete element
GUICtrlDelete($Input_[$iCount - 1][0]) ; delete element
GUICtrlDelete($Button_[$iCount - 1][0]) ; delete element
GUICtrlDelete($Button_[$iCount - 1][1]) ; delete element
EndIf
If $iCount <> 0 Then
If $iCount <= $Element_For_Column Then
WinMove($hGUI, "", Default, Default, Default, $Size[3] - $hGUISize_Height) ; resize gui
_Middle($hGUI, "")
EndIf
EndIf
If $Temp_iCount = 0 Then ; New Column
WinMove($hGUI, "", Default, Default, $Size[2] - $hGUISize_Width, Default) ; resize gui
_Middle($hGUI, "")
$Move_Left -= 250 ; Remove left value
$Temp_iCount += $Element_For_Column ; Remove new colum
EndIf
$iCount -= 1 ; Delete one count
$Temp_iCount -= 1; Delete one temp count
If $Move_Top = 0 Then ; Equal to start posizion
$Move_Top = $Temp_Move_Top * ($Element_For_Column - 1)
Else
$Move_Top -= 90 ; Delete one position
EndIf
EndFunc ;==>Delete_Element

Func _Middle(Const $win, Const $txt) ; Snippet Valuater - cdkid
Local Const $Size = WinGetClientSize($win, $txt)
Local Const $y = (@DesktopHeight / 2) - ($Size[1] / 2)
Local Const $x = (@DesktopWidth / 2) - ($Size[0] / 2)
Return WinMove($win, $txt, $x, $y)
EndFunc ;==>_Middle

1) The gui can create element but the size of the width is good only for the second element, then increase every 3°, 4° etc.

2) Buttons don't work because i don't know how to set in the While...WEnd and the realtive function

3) I don't know how to make the creation of 3 element for column until the $Max_Element is reached

4) I don't know how to delete group element ( except the first )

Thanks ;)

EDIT: No more to go, if someone has suggestion/improvement just ask ;)

Edited by johnmcloud

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Try this and see if it aint what you want.

#include <ButtonConstants.au3>

#include <EditConstants.au3>

#include <GUIConstantsEx.au3>

#include <StaticConstants.au3>

#include <WindowsConstants.au3>

Opt("GUIResizeMode",  BitOR($GUI_DOCKTOP, $GUI_DOCKLEFT, $GUI_DOCKSIZE))

Global Const $Element_For_Column = 3 ; Max number of element for column

Global Const $Max_Element = 9 ; Number of total element

Global $iCount = 1 ; Number 1 is used for basic element

Global $Move_Top

Global $Move_Left

Global $Temp_iCount

Global Const $hGUI = GUICreate("Dynamic GUI", 233, 107, -1, -1)

Global Const $Label_A1 = GUICtrlCreateLabel("This is a label n°1", 8, 26, 90, 17)

Global Const $Input_A1 = GUICtrlCreateInput("", 8, 43, 161, 21)

Global Const $Button_A1 = GUICtrlCreateButton("1", 176, 41, 51, 25)

Global Const $Button_A2 = GUICtrlCreateButton("Check Input", 8, 72, 75, 25)

Global Const $Button_Minus = GUICtrlCreateButton("-", 176, 2, 19, 25)

Global Const $Button_Plus = GUICtrlCreateButton("+", 210, 2, 19, 25)

Global $ctrl_array[1][4]

GUISetState(@SW_SHOWNORMAL)

Global $msg = ''

Do
    $msg = GUIGetMsg()

    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Button_A1
            GUICtrlSetData($Input_A1, "You have clicked button 1")

        Case $Button_A2
            _Test_Func($Input_A1)

        Case $Button_Plus
            _Add_Element()

        Case $Button_Minus
            Delete_Element()

        Case Else
            Global $ctrl_array_upbound = UBound($ctrl_array, 1)

            For $i = 0 To $ctrl_array_upbound - 1
                Switch $msg
                    Case $ctrl_array[$i][2]
                        GUICtrlSetData($ctrl_array[$i][1], "You have clicked button " & $i + 1)

                    Case $ctrl_array[$i][3]
                        _Test_Func($ctrl_array[$i][1])
                EndSwitch
            Next
    EndSwitch
Until False

Func _Test_Func(Const $Value)
    ConsoleWrite("This is the value of Input: " & GUICtrlRead($Value) & @crlf)
EndFunc   ;==>_Test_Func

Func _Add_Element()
    Local Const $Size = WinGetClientSize($hGUI, '')

    $Temp_iCount += 1

    $iCount += 1

    $Move_Top += 90

    If $Temp_iCount = $Element_For_Column Then
        If $iCount <= $Max_Element Then
            Local Const $hGUISize_Width = 250

            WinMove($hGUI, "", Default, Default, $Size[0] + $hGUISize_Width, Default)

            _Middle($hGUI, "")

            $Move_Top = "" ; reset top

            $Move_Left += 240 ; create left vaule

            $Temp_iCount = "" ; reset temp_element
        EndIf
    EndIf

    If $iCount <= $Max_Element Then
        ReDim $ctrl_array[$iCount][$iCount * 4]

        If $iCount <= $Element_For_Column Then
            $ctrl_array[$iCount - 1][0] = GUICtrlCreateLabel("This is a label n°" & $iCount, 8, 26 + $Move_Top, 90, 17) ; how to set the variable name?

            $ctrl_array[$iCount - 1][1] = GUICtrlCreateInput("", 8, 43 + $Move_Top, 161, 21)

            $ctrl_array[$iCount - 1][2] = GUICtrlCreateButton($iCount, 176, 41 + $Move_Top, 51, 25)

            $ctrl_array[$iCount - 1][3] = GUICtrlCreateButton("Check Input", 8, 72 + $Move_Top, 75, 25)

            Local Const $hGUISize_Height = 120

            WinMove($hGUI, "", Default, Default, Default, $Size[1] + $hGUISize_Height)

            _Middle($hGUI, "")
        Else
            $ctrl_array[$iCount - 1][0] = GUICtrlCreateLabel("This is a label n°" & $iCount, 8 + $Move_Left, 26 + $Move_Top, 90, 17) ; how to set the varable name?

            $ctrl_array[$iCount - 1][1] = GUICtrlCreateInput("", 8 + $Move_Left, 43 + $Move_Top, 161, 21)

            $ctrl_array[$iCount - 1][2] = GUICtrlCreateButton($iCount, 176 + $Move_Left, 41 + $Move_Top, 51, 25)

            $ctrl_array[$iCount - 1][3] = GUICtrlCreateButton("Check Input", 8 + $Move_Left, 72 + $Move_Top, 75, 25)
        EndIf
    EndIf
EndFunc   ;==>_Add_Element

Func Delete_Element()
    ;???
EndFunc   ;==>Delete_Element

Func _Middle(Const $win, Const $txt) ; Snippet Valuater - cdkid
    Local Const $Size = WinGetClientSize($win, $txt)

    Local Const $y = (@DesktopHeight / 2) - ($Size[1] / 2)

    Local Const $x = (@DesktopWidth / 2) - ($Size[0] / 2)

    Return WinMove($win, $txt, $x, $y)
EndFunc   ;==>_Middle

I'll leave deleting them as an exercise for you. =)

Edited by jaberwocky6669

Share this post


Link to post
Share on other sites

Thanks, i have make some little change ( i have make 4 different array for every element ) and i have added the deleting func...but it's a little bugged :D

The script is on the first post. Can you/someone check it out?

Thanks

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I have resolved the 99% of the problems with Delete_Element() ( i have updated the script on the first post ) i have only the last

When i created 6 element ( with 5 = Max element for column, so it created a new column with 1 element ) and i try to delete the 6° and recreate it, work fine and no problems

When i created 6 element ( with 5 = Max element for column, so it created a new column with 1 element ) and i try to delete the 6° and the 5° ( the result is only one column with four element ) and recreate the 5° again, i don't see anything

Tips?

EDIT: I have resolve the last problem, seems work fine now. Waiting for expert if the have improvement-suggestion on this script. Thanks

Edited by johnmcloud

Share this post


Link to post
Share on other sites

Silence gives consent ( i hope )

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  
Followers 0

  • Similar Content

    • Miliardsto
      By Miliardsto
      Hello.
      Drag option works only on parent window but as we can see on the parent gui is showed up child gui (on the right side with the color).
      Desired action is when we want to drag whole program we can also drag child gui and child gui will drag with parent gui.
      How to do this?
       
      #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <Array.au3> GUIRegisterMsg($WM_NCHITTEST, "_NCHITTEST") $hGUI = GUICreate("Test", 500, 500, -1, -1) $cButton_0 = GUICtrlCreateButton("Child 0", 10, 10, 80, 30) $cButton_1 = GUICtrlCreateButton("Child 1", 10, 50, 80, 30) $cButton_2 = GUICtrlCreateButton("Child 2", 10, 90, 80, 30) GUISetState(@SW_SHOW, $hGUI) ; guis Global $hChild_[3] Global $countGUI = UBound($hChild_) - 1 ;MsgBox("","",$countGUI) ; licznik dla GUI Global $gui = 0 $hChild_[0] = GUICreate("Child 0", 350, 300, 150, 0, $WS_POPUP, $WS_EX_MDICHILD, $hGUI) GUISetBkColor(0xFF0000) GUISetState(@SW_SHOW, $hChild_[0]) $hChild_[1] = GUICreate("Child 1", 350, 300, 150, 0, $WS_POPUP, $WS_EX_MDICHILD, $hGUI) GUISetBkColor(0x00FF00) GUISetState(@SW_HIDE, $hChild_[1]) $hChild_[2] = GUICreate("Child 2", 350, 300, 150, 0, $WS_POPUP, $WS_EX_MDICHILD, $hGUI) GUISetBkColor(0x0000FF) GUISetState(@SW_HIDE, $hChild_[2]) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $cButton_0 _All_Hide() GUISetState(@SW_SHOW, $hChild_[0]) $gui = 0 Case $cButton_1 _All_Hide() GUISetState(@SW_SHOW, $hChild_[1]) $gui = 1 Case $cButton_2 _All_Hide() GUISetState(@SW_SHOW, $hChild_[2]) $gui = 2 EndSwitch WEnd Func _All_Hide() GUISetState(@SW_HIDE, $hChild_[0]) GUISetState(@SW_HIDE, $hChild_[1]) GUISetState(@SW_HIDE, $hChild_[2]) EndFunc Func _NCHITTEST($hWnd, $uMsg, $wParam, $lParam) If $hWnd = $hGUI Then Local $aPos = WinGetPos($hWnd) If Abs(BitAND(BitShift($lParam, 16),0xFFFF)- $aPos[1]) < 500 Then Return $HTCAPTION EndIf Return $GUI_RUNDEFMSG EndFunc  
    • IAMK
      By IAMK
      Hello,
       
      I have a script which uses a GUI with two buttons. It gets shown and hidden., and pressing the buttons (including their hotkeys) highlights the button, making you able to press enter the next time.
      However, I don't want that.
       
      I've tried using WinActivate() to make enter do things on another program, but the enter is still tied to the AutoIT GUI (which still has the button highlighted).
       
      Here's the important part of my code:
      $MY_GUI = GUICreate("Check.exe", 400, 300, ((@DesktopWidth / 2) - 200), -500) WinSetOnTop($MY_GUI, "", 1) ;Keeps the window on the top. $Button1 = GUICtrlCreateButton("Pass (Ctrl+P)", 40, 250, 120, 40, $WS_GROUP) GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif") $Button2 = GUICtrlCreateButton("Fail (Ctrl+F)", 240, 250, 120, 40, $WS_GROUP) GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif") $GUIText = GUICtrlCreateLabel("", 20, 10, 360, 230) GUICtrlSetFont(-1, 13, 400, 0, "MS Sans Serif") GUICtrlSetData($GUIText, Execute($stepMessage[$step])) $showGUI = "True" GUISetState(@SW_SHOW) While($showGUI = "True") $buttonPressed = GUIGetMsg() Switch $buttonPressed Case $Button1 ;Pass $pass = "True" $buttonPressed = 0 $showGUI = "False" GUISetState(@SW_HIDE) Case $Button2 ;Fail $buttonPressed = 0 $showGUI = "False" GUISetState(@SW_HIDE) While($message = "") $message = InputBox("Check.exe", "Please enter the reason the step failed." & @CRLF, "", "", 300, 200, ((@DesktopWidth / 2) - 150), -400) WEnd Return $message EndSwitch WEnd Local $pass = "False" ;Whether "Pass" or "Fail" was pressed. Local $failLog = xxxfunc($pass) ;Returns a message to log if the step failed. ##THIS CALLS THE CODE SNIPPET ABOVE## FileWrite($file, _Now() & ": Step " & $step & getStatus($pass) & "and took " & Ceiling(TimerDiff($timer) / 1000) & " seconds." & @CRLF) If($failLog <> "") Then FileWrite($file, "Logged comment: " & $failLog & @CRLF) EndIf WinActivate("[TITLE:ProgramName]") ;Hotkeys: HotKeySet("^{p}", "pressPass") HotKeySet("^{f}", "pressFail") Func pressPass() $buttonPressed = $Button1 EndFunc Func pressFail() $buttonPressed = $Button2 EndFunc  
      The GUI's button/hotkey pressing works correctly.
      The same WinActivate code works in another script of mine.
      Together, WinActivate is not take "Active" away from the GUI's last-pressed button. How do I fix this?
       
      Thank you in advance.
    • fatpig
      By fatpig
      Dear AutoIT Community.
      I am currently working on a program that will display lots of images in a scroll down GUI.
      I create the GUI based on the number of images, which works fine.
      But all images I place using GuiCtrlCreatePic above a threshold of around 32000 pixels (cant say exactly) are placed at negative locations and finally will reach 0 again
      and will then populate on top of the images placed there before.
      For $i = 1 To $Images[0] ; Get picture dimensions $hImage = _GDIPlus_ImageLoadFromFile($ImgRep & $Images[$i]) $iX = _GDIPlus_ImageGetWidth($hImage) $iY = _GDIPlus_ImageGetHeight($hImage) _GDIPlus_ImageDispose($hImage) ; Resize & place images in GUI $Pic[$i] = GUICtrlCreatePic($ImgRep & $Images[$i], $Spacer, Round($yPos, 0), $MaxImgWidth, $MaxImgWidth * ($iY / $iX)) $yPos = $yPos + $MaxImgWidth * ($iY / $iX) + $Spacer $log = $log & "|" & $yPos $currentlut = $Images[1] Next I have found a limit at 32767 pixels for some GUI elements when googling around.
      Can someone tell me how to circumvent that?
      thanks!
      cheers
      Felix
    • Shirdish_chakravarthi
      By Shirdish_chakravarthi
      Hello Guys this is my gui and i need the numeric value typed in the highlighted  inputbox. if this value is greater than 1 i need to perform some operations
      \
       
      Here is the summary about that field which i got from wininfo tool

      i tried writing the code as shown below.. but the msgbox is popping up empty value..when the same code is pasted under some button action i am able to access the value present in the field..but when placed alone as written in the attached script i am unable to fetch it... 

      here i am using the valu present in the field

      can you guys tell me what's wrong??? or how can i get that value?
       
      My_GUI.au3

    • Neonovaz
      By Neonovaz
      Hello
       
      Is there anyway to store word documents in Autoit GUI? For example I have a instruction sheet that I want to bundle up with the exe.

      So a user simply clicks the icon and the stored document will launch  (Something like how you can add objects like excel sheets in word documents )

      (I Know we can launch word files from script directory)