Jump to content

Gui full of Buttons!


Recommended Posts

Ok, im sure the answer will come to me, but my computer has died at home and I only have limited time to do this on the work computer...

Im wanting to make a GUI that is nothing but size 40x40 buttons...

Ive started the coding but the problem is that I want all the buttons to be sequential...i.e. button1 - button500. I know I can fit 39 buttons per row, as you will see in my script, but I don't want the buttons naming to be dependent on row....please ignore all the arrays except the first one...its all for experimentation...

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

Global $ButtonR1, $ButtonR2

#Region ### START Koda GUI section ### Form=G:\Mine_Autoit\Models\Testing\Icon Testing\Icon Testing.kxf
$Form1 = GUICreate("Form1", 1574, 779, 7, 9)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

_Buttons()

While 1
    Sleep(100)
WEnd

Func _Buttons()
    Local $abR1[80]
    Local $abR2[38]
    Local $abR3[39]
    Local $abR4[39]
    Local $abR5[39]
    Local $abR6[39]
    Local $abR7[39]
    Local $abR8[39]
    Local $abR9[39]


    ; Create J#T Buttons
    For $ButtonR1 = 0 To 79
        Switch $ButtonR1
            Case 0 to 38
                $abR1[$ButtonR1] = GUICtrlCreateButton("R1" & $ButtonR1, 10 + ($ButtonR1 * 40), 10, 40, 40)
            Case 39 to 77
                $ButtonR2 = $ButtonR1 - 39
                $abR1[$ButtonR2] = GUICtrlCreateButton("R2" & $ButtonR2, 10 + ($ButtonR2 * 40), 55, 40, 40)
            Case 78 to 115
                $ButtonR3 = $ButtonR1 - 39
                $abR1[$ButtonR3] = GUICtrlCreateButton("R2" & $ButtonR3, 10 + ($ButtonR3 * 40), 55, 40, 40)
        EndSwitch
    Next


EndFunc   ;==>_Buttons

 

Link to comment
Share on other sites

That's quite easy: Run your counter from 0 to 499, divide the counter by the number of buttons in a row (39), if modulo is 0 then a new line starts.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I don't know if it can help. <Here> I put an example of how to create a "matrix" of controls (buttons or, if desired, other controls) using a custom function.

40x40 are a lot of buttons, however here is an (quick and dirty) example using that function.

The buttons created do not have a variable name for each of them, but the function returns an array where each element contains the reference to a button.

I hope it will be useful to you.

#include <EditConstants.au3>

Example()

Func Example()

    Local $hGui = GUICreate("Matrix of controls", 100, 100, 10, 10)

    ; create a matrix of controls (see function header for parameters details)
    Local $aMyMatrix = _GuiControlPanel("Button", 40, 40, 25, 15, 3, 75)

    ; resulting dimensions
    Local $iWidth = ($aMyMatrix[0])[11]
    Local $iHeight = ($aMyMatrix[0])[12]

    ; resize the GUI to fit buttons
    WinMove($hGui, '', Default, Default, $iWidth + 10, $iHeight + 10)
    GUISetState()

    ; create a "console" to view click action
    $hConsole = GUICtrlCreateEdit('Click any button', 3, 3, $iWidth, 70, BitOR($ES_READONLY, $ES_MULTILINE))
    GUICtrlSetBkColor(-1, 0)
    GUICtrlSetColor(-1, 0x00FF00)
    GUICtrlSetFont(-1, 40, 0, 0, "Courier New")

    ; Main loop - check which control was clicked
    ; ---------
    Do
        $GUIGetMsg = GUIGetMsg()

        ; scan all buttons to check if one was pressed
        For $i = 1 To UBound($aMyMatrix) - 1
            If $GUIGetMsg = $aMyMatrix[$i] Then
                GUICtrlSetData($hConsole, "Click on button " & $i)
            EndIf
        Next

    Until $GUIGetMsg = -3 ; -3 = $GUI_EVENT_CLOSE
EndFunc   ;==>Example


;
; #FUNCTION# ====================================================================================================================
; Name...........: _GuiControlPanel
; Description ...: Creates a rectangular panel with adequate size to contain the required amount of controls
;                  and then fills it with the same controls by placing them according to the parameters
; Syntax.........: _GuiControlPanel( $ControlType, $nrPerLine, $nrOfLines, $ctrlWidth, $ctrlHeight, $xPos = 0, $yPos = 0, $xBorder, $yBorder, $xSpace = 1, $ySpace = 1)
; Parameters ....: $ControlType  - Type of controls to be generated ("Button"; "Text"; .....
;                  $nrPerLine  - Nr. of controls per line in the matrix
;                  $nrOfLines - Nr. of lines in the matrix
;                  $ctrlWidth - Width of each control
;                  $ctrlHeight - Height of each control
;                  $xPanelPos - x Position of panel in GUI
;                  $yPanelPos - y Position of panel in GUI
;                  $xBorder - distance from lateral panel's borders to the matrix (width of left and right margin) default = 0
;                  $yBorder - distance from upper and lower panel's borders to the matrix (width of upper and lower margin) default = 0
;                  $xSpace - horizontal distance between the controls
;                  $ySpace - vertical distance between the controls
;                  $Group - if you want to group the controls (true or false)
;                  $sGrpTitle - title of the group (ignored if above is false)
; Return values .: an 1 based 1d array containing references to each control
;                  element [0] contains an 1d array containing various parameters about the panel
; Author ........: Gianni Addiego (Chimp)
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......:
; ===============================================================================================================================

Func _GuiControlPanel($ControlType, $nrPerLine, $nrOfLines, $ctrlWidth, $ctrlHeight, $xPanelPos = 0, $yPanelPos = 0, $xBorder = 0, $yBorder = 0, $xSpace = 1, $ySpace = 1, $Group = False, $sGrpTitle = "")

    Local Static $sAllowedControls = "|Label|Input|Edit|Button|CheckBox|Radio|List|Combo|Pic|Icon|Graphic|"
    If Not StringInStr($sAllowedControls, '|' & $ControlType & '|') Then Return SetError(1, 0, "Unkown control")

    Local $PanelWidth = (($ctrlWidth + $xSpace) * $nrPerLine) - $xSpace + ($xBorder * 2)
    Local $PanelHeight = (($ctrlHeight + $ySpace) * $nrOfLines) - $ySpace + ($yBorder * 2)

    Local $hGroup

    If $Group Then
        If $sGrpTitle = "" Then
            $xPanelPos += 1
            $yPanelPos += 1
            $hGroup = GUICtrlCreateGroup("", $xPanelPos - 1, $yPanelPos - 7, $PanelWidth + 2, $PanelHeight + 8)
        Else
            $xPanelPos += 1
            $yPanelPos += 15
            $hGroup = GUICtrlCreateGroup($sGrpTitle, $xPanelPos - 1, $yPanelPos - 15, $PanelWidth + 2, $PanelHeight + 16)
        EndIf
    EndIf

    ; create the controls
    Local $aGuiGridCtrls[$nrPerLine * $nrOfLines + 1]
    Local $aPanelParams[14] = [ _
            $ControlType, $nrPerLine, $nrOfLines, $ctrlWidth, $ctrlHeight, _
            $xPanelPos, $yPanelPos, $xBorder, $yBorder, $xSpace, $ySpace, $PanelWidth, $PanelHeight, $hGroup]

    For $i = 0 To $nrPerLine * $nrOfLines - 1
        ; coordinates 1 based
        $col = Mod($i, $nrPerLine) + 1 ; Vertical position within the grid (row) $iVtab
        $row = Int($i / $nrPerLine) + 1 ;  Horizontal position within the grid (column) $iHtab
        $left = $xPanelPos + ((($ctrlWidth + $xSpace) * $col) - $xSpace) - $ctrlWidth + $xBorder
        $top = $yPanelPos + ((($ctrlHeight + $ySpace) * $row) - $ySpace) - $ctrlHeight + $yBorder
        $text = $i + 1 ; "*" ; "." ; "(*)"

        $aGuiGridCtrls[$i + 1] = Execute("GUICtrlCreate" & $ControlType & "($text, $left, $top, $ctrlWidth, $ctrlHeight)")
    Next

    If $Group Then GUICtrlCreateGroup("", -99, -99, 1, 1) ; close group
    $aGuiGridCtrls[0] = $aPanelParams
    Return $aGuiGridCtrls
EndFunc   ;==>_GuiControlPanel

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Another example.

#include <WindowsConstants.au3>

_ButtonGui()


Func _ButtonGui()
    Local $NoButtons = 1600 ; 500 ;
    Local $NoPerRow = 50 ; 39 ;
    Local $ButtonsSelect, $iSetX = 30, $iSetY = 20
    Local $aButs[$NoButtons + 1]
    Local $ButtonsForm = GUICreate("Buttons", 20 + $iSetX * $NoPerRow, 20 + (Ceiling($NoButtons / $NoPerRow) * 25), 10, 10, -1, $WS_EX_TOPMOST)
    GUISetBkColor(0xF0F0FF, $ButtonsForm)

    For $x = 0 To $NoButtons - 1
        $aButs[$x] = GUICtrlCreateButton($x + 1, 10 + Mod($x, $NoPerRow) * $iSetX, 10 + Int($x / $NoPerRow) * 25, $iSetX, $iSetY + 5)
    Next
    GUISetState(@SW_SHOW)
    
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case -3
                ExitLoop
            Case $aButs[0] To $aButs[0] + $NoButtons
                MsgBox(0, "", "Button Number: " & $nMsg - $aButs[0] + 1, 2, $ButtonsForm)
        EndSwitch
    WEnd
    Return
EndFunc   ;==>_ButtonGui

 

Link to comment
Share on other sites

Good morning @Malkey!! I went with your example and it worked like a charm!! Its not pretty but with such short time to work it fits what I needed!! The script is to see the icons in a user selected dll… since I write test scripts for my work, I needed a quick and easy way to access windows icons, since that's what people are used to seeing. Thanks again!!

#include <EditConstants.au3>
#include <FileConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <WindowsConstants.au3>

Global $aButs, $NoButtons
Global $user_file

_UserFileOpen()

_ButtonGui()

Func _UserFileOpen()
    ; Create a constant variable in Local scope of the message to display in FileOpenDialog.
    Local Const $sMessage = "Select a single file of any type."

    ; Display an open dialog to select a file.
    Local $sFileOpenDialog = FileOpenDialog($sMessage, @WindowsDir & "\", "All (*.*)", $FD_FILEMUSTEXIST)
    If @error Then
        ; Display the error message.
        MsgBox($MB_SYSTEMMODAL, "", "No file was selected.")

        ; Change the working directory (@WorkingDir) back to the location of the script directory as FileOpenDialog sets it to the last accessed folder.
        FileChangeDir(@ScriptDir)
    Else
        ; Change the working directory (@WorkingDir) back to the location of the script directory as FileOpenDialog sets it to the last accessed folder.
        FileChangeDir(@ScriptDir)

        ; Replace instances of "|" with @CRLF in the string returned by FileOpenDialog.
        $sFileOpenDialog = StringReplace($sFileOpenDialog, "|", @CRLF)

        ; Display the selected file.
        $user_file = $sFileOpenDialog
        MsgBox($MB_SYSTEMMODAL, "", "You chose the following file:" & @CRLF & $user_file)
    EndIf
EndFunc   ;==>_UserFileOpen

Func _ButtonGui()
    Local $NoButtons = 500 ; 500 ;
    Local $NoPerRow = 30 ; 39 ;
    Local $ButtonsSelect, $iSetX = 50, $iSetY = 35
    Local $aButs[$NoButtons + 1]
    Local $ButtonsForm = GUICreate("Buttons", 100 + $iSetX * $NoPerRow, 400 + (Ceiling($NoButtons / $NoPerRow) * 25), -1, -1, BitOR($WS_THICKFRAME, $GUI_SS_DEFAULT_GUI), $WS_EX_TOPMOST)
    GUISetBkColor(0xF0F0FF, $ButtonsForm)



    For $x = 0 To $NoButtons - 1
        $aButs[$x] = GUICtrlCreateButton($x + 1, 10 + Mod($x, $NoPerRow) * $iSetX, 10 + Int($x / $NoPerRow) * 45, $iSetX, $iSetY + 5)
        ; Original $aButs[$x] = GUICtrlCreateButton($x + 1, 10 + Mod($x, $NoPerRow) * $iSetX, 10 + Int($x / $NoPerRow) * 25, $iSetX, $iSetY + 5)
    Next

    For $icons = 0 To 499
                    GUICtrlSetImage($aButs[$icons], $user_file, $icons)
                    ConsoleWrite($icons & @CR)
                    Sleep(50)
                Next

    GUISetState(@SW_SHOW)

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case -3
                ExitLoop
            Case $aButs[0] To $aButs[0] + $NoButtons
                MsgBox(0, "", "Button Number: " & $nMsg - $aButs[0] + 1, 2, $ButtonsForm)


        EndSwitch
    WEnd
    Return
EndFunc   ;==>_ButtonGui

 

Link to comment
Share on other sites

Now to get a scroll bar on this puppy.... tried $WS_VSCROLL..but the window didn't scroll...tried @Melba23 Gui Scroll Bar udf and _GUIScrollBars_Init($ButtonsForm, 100, 100) with many settings, scroll bar shows up but no vertical scroll.... Probly missing something easy but ugh....lol 

This became a problem because I needed to change number of buttons to 1000...

Still trying to find the answer for my vertical scroll problem but a shot in the right direction would be greatly appreciated!

#include <FileConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <GUIScrollbars_Ex.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <WindowsConstants.au3>

Global $aButs, $NoButtons
Global $user_file
Global $WinFolder = "C\Windows\System32"

_UserFileOpen()

_ButtonGui()

Func _UserFileOpen()
    ; Create a constant variable in Local scope of the message to display in FileOpenDialog.
    Local Const $sMessage = "Select a single dll file."

    ; Display an open dialog to select a file.
    Local $sFileOpenDialog = FileOpenDialog($sMessage, $WinFolder & "\", "All (*.dll)", $FD_FILEMUSTEXIST)
    If @error Then
        ; Display the error message.
        MsgBox($MB_SYSTEMMODAL, "", "No file was selected.")

        ; Change the working directory (@WorkingDir) back to the location of the script directory as FileOpenDialog sets it to the last accessed folder.
        FileChangeDir(@ScriptDir)
    Else
        ; Change the working directory (@WorkingDir) back to the location of the script directory as FileOpenDialog sets it to the last accessed folder.
        FileChangeDir(@ScriptDir)

        ; Replace instances of "|" with @CRLF in the string returned by FileOpenDialog.
        $sFileOpenDialog = StringReplace($sFileOpenDialog, "|", @CRLF)

        ; Display the selected file.
        $user_file = $sFileOpenDialog
        MsgBox($MB_SYSTEMMODAL, "", "You chose the following file:" & @CRLF & $user_file)
    EndIf
EndFunc   ;==>_UserFileOpen

Func _ButtonGui()
    Local $NoButtons = 1000 ; 500 ;
    Local $NoPerRow = 30 ; 39 ;
    Local $ButtonsSelect, $iSetX = 50, $iSetY = 35
    Local $aButs[$NoButtons + 1]
    Local $ButtonsForm = GUICreate("DLL Icon Viewer v1.0  *Subtract one for true icon number*", 100 + $iSetX * $NoPerRow, 400 + (Ceiling($NoButtons / $NoPerRow) * 25), 5, 5, BitOR($WS_THICKFRAME, $GUI_SS_DEFAULT_GUI), $WS_EX_TOPMOST)
    GUISetBkColor(0xF0F0FF, $ButtonsForm)
    ;_GUIScrollBars_Init($ButtonsForm, 100, 100)

    For $x = 0 To $NoButtons - 1
        $aButs[$x] = GUICtrlCreateButton($x + 1, 10 + Mod($x, $NoPerRow) * $iSetX, 10 + Int($x / $NoPerRow) * 45, $iSetX, $iSetY + 5)
        ; Original $aButs[$x] = GUICtrlCreateButton($x + 1, 10 + Mod($x, $NoPerRow) * $iSetX, 10 + Int($x / $NoPerRow) * 25, $iSetX, $iSetY + 5)
    Next

    For $icons = 0 To 999
        GUICtrlSetImage($aButs[$icons], $user_file, $icons)
        ConsoleWrite($icons & @CR)
    Next

    GUISetState(@SW_SHOW)



    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case -3
                ExitLoop
            Case $aButs[0] To $aButs[0] + $NoButtons
                MsgBox(0, "", "Button Number: " & $nMsg - $aButs[0] + 1, 2, $ButtonsForm)


        EndSwitch
    WEnd
    Return
EndFunc   ;==>_ButtonGui

 

Link to comment
Share on other sites

Here is my modification resolving LAGs:

note: it's version without scrollbars

#include <EditConstants.au3>
#include <FileConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>

Global $aButs, $NoButtons
Global $user_file

_UserFileOpen()
_ButtonGui()

Func _UserFileOpen()
    Local Const $sMessage = "Select a single file of any type."
    Local $sFileOpenDialog = FileOpenDialog($sMessage, @WindowsDir & "\", "All (*.*)", $FD_FILEMUSTEXIST)
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "No file was selected.")
        FileChangeDir(@ScriptDir)
    Else
        FileChangeDir(@ScriptDir)
        $sFileOpenDialog = StringReplace($sFileOpenDialog, "|", @CRLF)
        $user_file = $sFileOpenDialog
    EndIf
EndFunc   ;==>_UserFileOpen

Func _ButtonGui()
    Local $NoButtons = 500 ; 500 ;
    Local $NoPerRow = 30 ; 39 ;
    Local $ButtonsSelect, $iSetX = 50, $iSetY = 35
    Local $aButs[$NoButtons + 1]
    Local $ButtonsForm = GUICreate("Buttons", 100 + $iSetX * $NoPerRow, 400 + (Ceiling($NoButtons / $NoPerRow) * 25), -1, -1, BitOR($WS_THICKFRAME, $GUI_SS_DEFAULT_GUI));, $WS_EX_TOPMOST)
    GUISetBkColor(0xF0F0FF, $ButtonsForm)

    For $x = 0 To $NoButtons - 1
        $aButs[$x] = GUICtrlCreateButton($x + 1, 10 + Mod($x, $NoPerRow) * $iSetX, 10 + Int($x / $NoPerRow) * 45, $iSetX, $iSetY + 5, $BS_ICON)
        ; Original $aButs[$x] = GUICtrlCreateButton($x + 1, 10 + Mod($x, $NoPerRow) * $iSetX, 10 + Int($x / $NoPerRow) * 25, $iSetX, $iSetY + 5)

;~         GUICtrlSetImage($aButs[$x], $user_file, $x)
        GUICtrlSetImage($aButs[$x], $user_file, -1 * $x)
        ConsoleWrite($user_file & ' --> ' & $x & @CR)
    Next

    GUISetState(@SW_SHOW)

    While 1
        $nMsg = GUIGetMsg()
        If $nMsg = -3 Then ExitLoop
        If $nMsg > 0 Then ; this is important to remove LAGs !!!
            Switch $nMsg
                Case $aButs[0] To $aButs[0] + $NoButtons
                    MsgBox(0, "", "Button Number: " & $nMsg - $aButs[0] + 1, 2, $ButtonsForm)
            EndSwitch
        EndIf
    WEnd
    Return
EndFunc   ;==>_ButtonGui

 

Link to comment
Share on other sites

Thanx @Zedna! Im learning that the vertical scroll bar is what seems to add to the lag. When you scroll it takes a few beats before the buttons register again. I would love to have a scroll function. I want to be able to have it load more than the 500 icons, keep the icon sizing correct and dynamically size the window vertically depending on the number of icons... So this is all just functional testing and me trying to understand what im even trying todo!!!

Link to comment
Share on other sites

Link to comment
Share on other sites

@Nine LOL im sure it does... Im basically piecing together a bunch of snippets and cobbeling it into a "working " proto-type till I can understand everything that is going on... Learning by using...I looked into using GUI Image List but I don't have enough time at the moment to even begin to figure it out!

Im using thd built in GUIDcrollbars.au3....ill have to check out M23's...I have it on my pen drive but never played with it

 

Edited by Fractured
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...