Jump to content
Sign in to follow this  

Determining which graphic has been 'clicked' in a GUI?

Recommended Posts



I am still learning to work with the various GUI functions within AutoIT. I have a script that creates a bunch of graphic rectangles within a GUI control window. My question is fairly simple, how do I determine which of those graphics are "clicked" when the user does so? It is straightforward when there is only 1 graphic, however when there are multiples is there an easy/quick way to determine the one that was clicked? I'd like to be able to easily retrieve infomation such as the graphic's X and Y position upon clicking...I know a unique ControlID is assigned to each one upon creation, but I am not sure how to use this to advantage. Any help appreciated, thank you in advance.

Global X_pos = 0
Global Y_pos = 1
Dim $graph2[31]
Dim $Attributes[31][2]
$LIST = FileOpen("C:\Files\Automate\MASTER\data\MasterList.txt")
While 1
$line = FileReadLine($LIST)
If @error = -1 Then ExitLoop
$x = 1
$graph2[$x] = GUICtrlCreateGraphic($X_PLOT, $Y_PLOT, 10, $PIPS, $SS_BLACKFRAME)
$Attributes{$x][X_pos] = $X_PLOT
$Attributes{$x][Y_pos] = $Y_PLOT
$x += 1
     $msg = GUIGetMsg()
         Case $msg = $GUI_EVENT_PRIMARYDOWN         
         ;how do I determine which graphic ($graph2[$x]) has
         ;been clicked upon so I can retrieve certain info about
         ;that graphic like its X and Y position (previously assigned
         ;in the '$Attributes' array) for example?

Perhaps my code explains it better...I am confused as to how AutoIT enables me to program a 'dynamic' GUI that can retrieve/display info about the various graphics and other files that can be clicked and thus be interactive with the user? Thank you in advance.

Share this post

Link to post
Share on other sites

   Case $msg=$graph2[$x]


Phoenix XL

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post

Link to post
Share on other sites

Thanks, but how is the determination made (which index #) for the value of x? Do I have to write a loop within the case statement somehow? If so what am I searching for within my loop to "match up" with the $graph2[$x]...the ControlID?? How does the case statement determine if the $graph2[1] or the $graph2[15] or whatever has been clicked? Sorry for my confusion.

Share this post

Link to post
Share on other sites

Can I possibly get an explanation as to how the handle of the graphic that is 'clicked' can be reconciled with the multitude of graphic indexes stored in $graph2[$x]? I am not understanding the concept...

Share this post

Link to post
Share on other sites


PhoenixXL's suggestion while a good hint, was not exactly a full solution. You are correct that you need a loop to determine which control was clicked by comparing the returned message number with the array elements. :)

Here is a simplified version of your script to show how you might do it - you should be able to adapt it to read from your file without too much trouble:

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>

Global $X_pos = 0
Global $Y_pos = 1

; Do not use Dim - always explicitly set the scope
Global $graph2[31]
Global $Attributes[31][2]

; This is just so we can preven the graphics from overlapping
$X_PLOT = 10
$Y_PLOT = 10

GUICreate("Test", 700, 300)

; You only have this many array elements, so limit the loop to this number
For $x = 0 To UBound($graph2) - 1

    ; Amend the x,y coords for each graphic
    $X_PLOT += 20
    $Y_PLOT += 5

    ; Create the graphic controls and store the ControlID
    ; Note we need to define ALL the styles as they are not the default
    $graph2[$x] = GUICtrlCreateGraphic($X_PLOT, $Y_PLOT, 20, 20, BitOR($SS_NOTIFY, $SS_BLACKFRAME))
    $Attributes[$x][$X_pos] = $X_PLOT ; X coord
    $Attributes[$x][$Y_pos] = $Y_PLOT ; Y coord



While 1

    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
        Case $graph2[0] To $graph2[30]
            ; As long as we create the graphic controls in IMMEDIATE succession the ControlIDs shooudl form a block
            ; So we can use a loop to determine which one it was
            For $i = 0 To UBound($graph2) - 1 
                If $nMsg = $graph2[$i] Then
                    ; And so display the X/Y coordinates
                    MsgBox(0, "Clicked " & $i, $Attributes[$i][$X_pos] & " - " & $Attributes[$i][$Y_pos])

I hope the comments are clear enough, but please ask if you have any questions. ;)


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 Open spoiler to see my UDFs:


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


Share this post

Link to post
Share on other sites

Thanks Melba, I will try that later when I have the time...however looking at it quickly I think I understand, and it seems well commented...thank you again.

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