Jump to content

get control handle from mouseClick?

Recommended Posts


Is it possible to get the handle of a control that is the object of a mouseClick? tia.

Share this post

Link to post
Share on other sites


Welcome to the autoit forum :)

Yes, take a look at the _WinAPI_WindowFromPoint function.

Edit: And the function _IsPressed to detect the mouseclick. There is maybe more reliable functions for your needs, can you tell us what are you trying to do?

Br, FireFox.

Edited by FireFox


OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control


Share this post

Link to post
Share on other sites

This might give you some ideas.

Press"Esc" key to exit.

#include <Misc.au3>
#include <WinAPI.au3>
Local $hDLL = DllOpen("user32.dll")

While 1
    If _IsPressed("01", $hDLL) Then ; Left mouse button
        ; Wait until key is released.
        While _IsPressed("01", $hDLL)
        $hCtrlHnd = ControlGetHandle("", "", "")
        ConsoleWrite("Ctrl Id #" & _WinAPI_GetDlgCtrlID($hCtrlHnd) & @TAB & "Ctrl Handle:" & $hCtrlHnd & @CRLF)

    ElseIf _IsPressed("1B", $hDLL) Then ; Press Esc key to exit.
        MsgBox(0, "_IsPressed", "The Esc Key was pressed, therefore we will close the application.", 3)


Share this post

Link to post
Share on other sites

Here is another one...it will grab ALL controls that the mouse cords are in, at release of click (so grabs groups behind the actual control, etc)...esc to stop...oh, might want to verify that the control is visible...or if win is tabbed, it will grab from ALL tabs as well

one more note: to grab a button, without performing the action of the button, click on window anywhere, and release on the button

HotKeySet("{ESC}", "_Exit")
Func _Exit()
EndFunc   ;==>_Exit
#include <Misc.au3>
#include <Array.au3>
AutoItSetOption("MouseCoordMode", 2)
While 1
    If _IsPressed(01) Then
        While _IsPressed(01)
        $aMouse = MouseGetPos()
        ConsoleWrite("Mouse [x/y]=[" & $aMouse[0] & "/" & $aMouse[1] & "]" & @CRLF)
        ConsoleWrite("Active WinHandle=[" & WinGetHandle("[ACTIVE]") & "] title=[" & WinGetTitle("[ACTIVE]") & "] ")
        $windowList = WinGetClassList("[ACTIVE]")
        $aClassList = StringSplit($windowList, @CRLF)
        _ArrayDelete($aClassList, UBound($aClassList))
        _ArrayDelete($aClassList, 0)

        $iCurrentClass = ""
        $iCurrentCount = 1
        $iTotalCounter = 1
        For $i = 0 To UBound($aClassList) - 1
            If $aClassList[$i] = $iCurrentClass Then
                $iCurrentCount += 1
                $iCurrentClass = $aClassList[$i]
                $iCurrentCount = 1

            $hControl = ControlGetHandle("[ACTIVE]", "", $iCurrentClass & $iCurrentCount)
            $aPos = ControlGetPos("[ACTIVE]", "", $hControl)

            If $aPos[0] < $aMouse[0] And ($aPos[0] + $aPos[2]) > $aMouse[0] And $aPos[1] < $aMouse[1] And ($aPos[1] + $aPos[3]) > $aMouse[1] Then
                ConsoleWrite(@CRLF & "Control found=[" & $hControl & "]" )
        ConsoleWrite(@CRLF & "done in loop" & @CRLF)
Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post

Link to post
Share on other sites

I am making a random xy MouseClick on a Win app - not totally random  - I know there is a control under the click. Since there a lot of WinControls I did not map them all to their coordinates and names ... I could not work out how to make the jump from random MouseClick to the actual WinControl that got clicked. Thank you very much for all the info - going to read and inwardly digest :-)


- all info looks good - I just clicked one of them to mark answered.

Edited by crzymnmchl

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


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.