Jump to content

Get IDs of autoit control


MyEarth
 Share

Recommended Posts

Hello ;)

I have searched before opening but i don't have found nothing for an internal autoit GUI, something for external window. There is a function to collect all ID of the control in a GUI? I have think at the ID because this value never change, right? Handle and other things change at every execution:

$hGUI = GUICreate("")
$A = GUICtrlCreateButton("", -1, -1)
$B = GUICtrlCreateLabel("", -1, -1)
$C = GUICtrlCreatePic("", -1, -1)
$D = GUICtrlCreateProgress("", -1, -1)
$E = GUICtrlCreateListView("", -1, -1)
$F = GUICtrlCreateTreeView("", -1, -1)
;etc

Global $aIDs[1]
;<<<<<<<<<<<<<<<<<<<<< INSERT HERE

Thanks

Edited by MyEarth
Link to comment
Share on other sites

  • Moderators

MyEarth,

Normal practice would be to store the ControlIDs in the array when creating them: :)

Global $aControls[5]

$hGUI = GUICreate("")

$aControls[0] = GUICtrlCreateButton(...)
$aControls[1] = GUICtrlCreateLabel(...)
$aControls[2] = GUICtrlCreatePic(...)
$aControls[3] = GUICtrlCreateProgress(...)
$aControls[4] = GUICtrlCreateListView(...)
You can also use Enum to help you in identifying the controls once created: ;)

Global Enum $eButton, $eLabel, $ePic, $eProgress, $eListView, $eMax

Global $aControls[$eMax]

$hGUI = GUICreate("")

$aControls[$eButton] = GUICtrlCreateButton(...)
$aControls[$eLabel] = GUICtrlCreateLabel(...)
$aControls[$ePic] = GUICtrlCreatePic(...)
$aControls[$eProgress] = GUICtrlCreateProgress(...)
$aControls[$eListView] = GUICtrlCreateListView(...)
Then you can use something like guinness' PreExpand to replace the Enum constants with their numerical equivalent before compiling to speed up the execution. :)

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

  • Moderators

MyEarth,

If all you want is to have a collection of the current controls you could just add a dummy control before and behind the control creation code to check the ControlID bounds. You could also check the class of control associated with each ControlID by doing something like this: :)

#include <GUIConstantsEx.au3>
#include <Array.au3>
#include <WinAPI.au3>

$hGUI = GUICreate("Test", 500, 500)

$cStart = GUICtrlCreateDummy()
GUICtrlCreateButton("Test", 10, 10, 80, 30)
GUICtrlCreateLabel("Test", 10, 50, 80, 20)
GUICtrlCreatePic("", 10, 100, 100, 100)
GUICtrlCreateProgress(10, 250, 200, 20)
GUICtrlCreateListView("", 10, 300, 200, 100)
$cEnd = GUICtrlCreateDummy()

GUISetState()

; Count controls
$iCount = $cEnd - $cStart
; Create an array to hold their details
Global $aControls[$iCount][2]

For $i = 1 To $iCount - 1
    ; Save ControlID
    $aControls[$i][0] = $cStart + $i
    ; Get control handle
    $hHandle = GUICtrlGetHandle($cStart + $i)
    ; Save control class
    $aControls[$i][1] = _WinAPI_GetClassName($hHandle)
Next

; And here is the result
_ArrayDisplay($aControls, "", Default, 8)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd
Obviously the best solution depends on why you need to collect the controls of a GUI you have created. If you explained why you need to do this we could perhaps offer better focused advice. :)

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

I have stripped out partially the (Großvater & progandy & UEZ) function for getting the ID only:

#include <GUIConstantsEx.au3>
#include <Array.au3>

$hGUI = GUICreate("Test", 500, 500)

ConsoleWrite(GUICtrlCreateButton("Test", 10, 10, 80, 30))
ConsoleWrite(GUICtrlCreateLabel("Test", 10, 50, 80, 20))
ConsoleWrite(GUICtrlCreatePic("", 10, 100, 100, 100))
ConsoleWrite(GUICtrlCreateProgress(10, 250, 200, 20))
ConsoleWrite(GUICtrlCreateListView("", 10, 300, 200, 100))
ConsoleWrite(GUICtrlCreateTreeView("", 10, 300, 200, 100))

$aTest = _WinGetControlsID($hGUI)
_ArrayDisplay($aTest)

GUISetState()

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

Func _WinGetControlsID($hWnd)
    Local $UClasses = 50
    Local $aClasses[$UClasses + 1][2]
    Local $aReturn[1000], $iNum = 0
    Local $sClasses = WinGetClassList($hWnd)
    Local $aSplit = StringSplit(StringStripWS($sClasses, 2), @LF)
    For $I = 1 To $aSplit[0]
        $Class = $aSplit[$I]
        $ClassExist = False
        $C = $aClasses[0][0]
        For $J = 1 To $C
            If $aClasses[$J][0] = $Class Then
                $NN = $aClasses[$J][1] + 1
                $aClasses[$J][1] = $NN
                $ClassExist = True
                ExitLoop
            EndIf
        Next
        If Not $ClassExist Then
            $NN = 1
            $C += 1
            If $C > $UClasses Then
                $UClasses += 50
                ReDim $aClasses[$UClasses + 1][2]
            EndIf
            $aClasses[0][0] = $C
            $aClasses[$C][0] = $Class
            $aClasses[$C][1] = $NN
        EndIf
        $Handle = ControlGetHandle($hWnd, "", $Class & $NN)
        $aResult = DllCall("User32.dll", "Int", "GetDlgCtrlID", "HWND", $Handle)
        If @error Or $aResult[0] = 0 Then
            $ID = ""
        Else
            $aReturn[$iNum] = $aResult[0]
            $iNum += 1
        EndIf
    Next
    ReDim $aReturn[$iNum]
    Return $aReturn
EndFunc   ;==>_WinGetControlsID

I don't know if have "extra" information i don't care or do something i don't need

EDIT: The Melba version but require the two dummy:

#include <GUIConstantsEx.au3>
#include <Array.au3>

$hGUI = GUICreate("Test", 500, 500)

$cStart = GUICtrlCreateDummy()
ConsoleWrite(GUICtrlCreateButton("Test", 10, 10, 80, 30))
ConsoleWrite(GUICtrlCreateLabel("Test", 10, 50, 80, 20))
ConsoleWrite(GUICtrlCreatePic("", 10, 100, 100, 100))
ConsoleWrite(GUICtrlCreateProgress(10, 250, 200, 20))
ConsoleWrite(GUICtrlCreateListView("", 10, 300, 200, 100))
ConsoleWrite(GUICtrlCreateTreeView("", 10, 300, 200, 100))
$cEnd = GUICtrlCreateDummy()

$aTest = _WinGetControlsID($cStart, $cEnd)
_ArrayDisplay($aTest)

GUISetState()

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

Func _WinGetControlsID($cStart, $cEnd)
    ; Count controls
    Local $iCount = $cEnd - $cStart
    ; Create an array to hold their details
    Local $aControls[$iCount] = [$iCount - 1]
    For $i = 1 To $iCount - 1
        ; Save ControlID
        $aControls[$i] = $cStart + $i
    Next
    Return $aControls
EndFunc   ;==>_WinGetControlsID
Edited by MyEarth
Link to comment
Share on other sites

#include <Array.au3>
#include <WinAPISys.au3>

Local $hForm = GUICreate('Test ' & StringReplace(@ScriptName, '.au3', '()'), 310, 360)
$a = GUICtrlCreateButton("Test", 10, 10, 80, 30)
$b = GUICtrlCreateLabel("Test", 10, 50, 80, 20)
$c = GUICtrlCreatePic("", 10, 100, 100, 100)
$d = GUICtrlCreateProgress(10, 250, 200, 20)
$e = GUICtrlCreateListView("", 10, 300, 200, 100)
GUISetState(@SW_SHOW)

; Msgbox(0,"", $a &@crlf& $b &@crlf& $c &@crlf& $d &@crlf& $e ) 

$aRes = _GetControls($hForm)
_ArrayDisplay($aRes)

Func _GetControls($hWnd)
  Local $aData = _WinAPI_EnumChildWindows($hWnd)
  _ArrayColInsert($aData, 2)
  For $i = 1 to $aData[0][0]
      $aData[$i][2] = _WinAPI_GetDlgCtrlID($aData[$i][0])
  Next
  Return $aData
EndFunc

GUIDelete()

?

Link to comment
Share on other sites

It was just an example

Instead of editing the udf (not a very good idea) you'd better do it like this, and get 2 cols with handles and IDs only  - handles could be useful

Func _GetControls($hWnd)
  Local $aData = _WinAPI_EnumChildWindows($hWnd)
  For $i = 1 to $aData[0][0]
      $aData[$i][1] = _WinAPI_GetDlgCtrlID($aData[$i][0])
  Next
  Return $aData
EndFunc
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...