Jump to content

How to retrieve the text of the element clicked by the mouse?


Recommended Posts

How can one obtain the text or at least the ID of the label on which the mouse click event occurred?

Please help solve this issue if it's not too difficult for someone. I would be grateful!

 

#include <GUIConstantsEx.au3>

Global $hGUI = GUICreate("Mouse Click Detection", 300, 200)
GUICtrlCreateLabel('ITEM 01',Default,Default,Default,Default)
GUICtrlCreateLabel('ITEM 02',Default,40,Default,Default)
GUICtrlCreateLabel('ITEM 03',Default,80,Default,Default)
GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_PRIMARYDOWN
            Local $hControl = ControlGetHandle($hGUI, "", ControlGetFocus($hGUI))
            Local $sControlText = ControlGetText($hGUI, "", $hControl)
            MsgBox(0, "Mouse Click Detected", "Clicked on Control: " & $sControlText)
    EndSwitch
WEnd

 

Link to comment
Share on other sites

#include <GUIConstantsEx.au3>

Global $hGUI = GUICreate("Mouse Click Detection", 300, 200)
GUICtrlCreateLabel('ITEM 01',Default,Default,Default,Default)
GUICtrlCreateLabel('ITEM 02',Default,40,Default,Default)
GUICtrlCreateLabel('ITEM 03',Default,80,Default,Default)
GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_PRIMARYDOWN
            $aCtrl = GUIGetCursorInfo($hGUI)
            MsgBox(0, "Mouse Click Detected", "Clicked on Control: " & GUICtrlRead($aCtrl[4]))
    EndSwitch
WEnd

 

I know that I know nothing

Link to comment
Share on other sites

Is there any particular reason why you use $GUI_EVENT_PRIMARYDOWN to check if a specific control was clicked? Why not simply this:

#include <GUIConstantsEx.au3>

Global $hGUI = GUICreate("Mouse Click Detection", 300, 200)
$a = GUICtrlCreateLabel('ITEM 01',Default,Default,Default,Default)
$b = GUICtrlCreateLabel('ITEM 02',Default,40,Default,Default)
$c = GUICtrlCreateLabel('ITEM 03',Default,80,Default,Default)
GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $a
            MsgBox(0, '', 'Yayyy! You clicked ITEM 01')
    EndSwitch
WEnd

 

When the words fail... music speaks.

Link to comment
Share on other sites

Thank you very much! You've been a great help to me!

The reason is, I don't initially know the ID labels, so I need to figure out which one I clicked on and what's written in it.

Link to comment
Share on other sites

If you create the labels you can keep track of them, otherwise you can use GUIGetCursorInfo() as in @ioa747 's example above or something like this:

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

Global $hGUI = GUICreate("Mouse Click Detection", 300, 200)
GUICtrlCreateLabel('ITEM 01',Default,Default,Default,Default)
GUICtrlCreateLabel('ITEM 02',Default,40,Default,Default)
GUICtrlCreateLabel('ITEM 03',Default,80,Default,Default)
GUIRegisterMsg($WM_COMMAND, 'WM_COMMAND')
GUISetState()

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

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $lParam
    If _WinAPI_HiWord($wParam) = 0 Then    ; STN_CLICKED
        ConsoleWrite(GUICtrlRead(_WinAPI_LoWord($wParam)) & @CRLF)
    EndIf
EndFunc

 

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

alternately

#include <GUIConstantsEx.au3>

Global $hGUI = GUICreate("Mouse Click Detection", 300, 200)
$a = GUICtrlCreateLabel('ITEM 01', Default, Default, Default, Default)
ConsoleWrite("$a=" & $a & @CRLF)
$b = GUICtrlCreateLabel('ITEM 02', Default, 40, Default, Default)
$c = GUICtrlCreateLabel('ITEM 03', Default, 80, Default, Default)
GUISetState()

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

        Case Else
            If $msg > 0 Then
                ConsoleWrite("Clicked on Control: ITEM " & StringFormat("%02s", $msg - 2) & " with text:" & GUICtrlRead($msg) & @CRLF)
            EndIf

    EndSwitch
WEnd

 

Edited by ioa747

I know that I know nothing

Link to comment
Share on other sites

@ioa747 Sure, if he have a series of label created one after another then your code would fit nicely or this variation that would target specific controls created in series.

#include <GUIConstantsEx.au3>

Global $aLabel[3]
Global $hGUI = GUICreate("Mouse Click Detection", 300, 200)
$aLabel[0] = GUICtrlCreateLabel('ITEM 01',Default,Default,Default,Default)
$aLabel[1] = GUICtrlCreateLabel('ITEM 02',Default,40,Default,Default)
$aLabel[2] = GUICtrlCreateLabel('ITEM 03',Default,80,Default,Default)
GUISetState()

While True
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $aLabel[0] To $aLabel[UBound($aLabel) - 1]
            ConsoleWrite('Clicked $aLabel[' & $nMsg - $aLabel[0] & ']' & @CRLF)
    EndSwitch
WEnd

@SEKOMD now you have a lot of ways to solve your problem 😄

When the words fail... music speaks.

Link to comment
Share on other sites

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

GUICreate("Mouse Click Detection", 300, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

GUICtrlCreateLabel('ITEM 01',Default,Default,Default,Default)
GUICtrlSetOnEvent(-1, "_Clicked")
GUICtrlCreateLabel('ITEM 02',Default,40,Default,Default)
GUICtrlSetOnEvent(-1, "_Clicked")
GUICtrlCreateLabel('ITEM 03',Default,80,Default,Default)
GUICtrlSetOnEvent(-1, "_Clicked")

GUISetState()

While 1
    Sleep(100)
WEnd

Func _Exit()
    Exit
EndFunc

Func _Clicked()
    Local $id = @GUI_CtrlId
    ConsoleWrite("Clicked at label id " & $id & " with text '"&GUICtrlRead($id)&"'" & @CRLF)
EndFunc

 

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

4 hours ago, funkey said:
#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

GUICreate("Mouse Click Detection", 300, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

GUICtrlCreateLabel('ITEM 01',Default,Default,Default,Default)
GUICtrlSetOnEvent(-1, "_Clicked")
GUICtrlCreateLabel('ITEM 02',Default,40,Default,Default)
GUICtrlSetOnEvent(-1, "_Clicked")
GUICtrlCreateLabel('ITEM 03',Default,80,Default,Default)
GUICtrlSetOnEvent(-1, "_Clicked")

GUISetState()

While 1
    Sleep(100)
WEnd

Func _Exit()
    Exit
EndFunc

Func _Clicked()
    Local $id = @GUI_CtrlId
    ConsoleWrite("Clicked at label id " & $id & " with text '"&GUICtrlRead($id)&"'" & @CRLF)
EndFunc

 

Thank you for the interesting example!

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...