Jump to content

Detect if button has been clicked on external GUI or software?


Go to solution Solved by antonioj84,

Recommended Posts

I am trying to run that code  that was posted previously , I  made little modification  no luck so far

http://www.autoitscript.com/forum/index.php?showtopic=88082&st=0&p=632463&#entry632463

Basically open calculator  if button "=" is clicked then I just  need a message box to popup

#include <Misc.au3>
#include <WinAPI.au3>
;

Global $hUser32_Dll = DllOpen("User32.dll")

HotKeySet("^e", "_Quit")

While 1
    If _IsPressed("01", $hUser32_Dll) Then
        ;Waiting untill the button is released
        While _IsPressed("01", $hUser32_Dll)
            Sleep(10)
        WEnd

        $hWindow = _WinGetHoveredHandle()
        $iCtrlID = _ControlGetHoveredID()

        $sWinClass = _WinAPI_GetClassName($hWindow)
        $sCtrlData = ControlGetText($hWindow, "", $iCtrlID)

        If $sWinClass = "CalcFrame" And $iCtrlID  = 121 And $sCtrlData = "=" Then
            $sResult = Number(ControlGetText($hWindow, "", "Edit1"))
            ToolTip("The result has been calculated: " & $sResult, Default, Default, "Calc Info", 1, 5)
        Else
            ToolTip("")
        EndIf
    EndIf

    Sleep(10)
WEnd

Func _ControlGetHoveredID()
    Local $iOld_Opt_MCM = Opt("MouseCoordMode", 1)

    Local $hRet = DllCall("user32.dll", "int", "WindowFromPoint", "long", MouseGetPos(0), "long", MouseGetPos(1))

    $hRet = DllCall("user32.dll", "int", "GetDlgCtrlID", "hwnd", $hRet[0])
    If $hRet[0] < 0 Then $hRet[0] = 0

    Opt("MouseCoordMode", $iOld_Opt_MCM)

    Return $hRet[0]
EndFunc

Func _WinGetHoveredHandle()
    Local $iOld_Opt_MCM = Opt("MouseCoordMode", 1)
    Local $aRet = DllCall("user32.dll", "int", "WindowFromPoint", "long", MouseGetPos(0), "long", MouseGetPos(1))

    Opt("MouseCoordMode", $iOld_Opt_MCM)

    $aRet = DllCall("User32.dll", "hwnd", "GetAncestor", "hwnd", $aRet[0], "uint", 2) ;$GA_ROOT

    Return HWnd($aRet[0])
EndFunc

Func _Quit()
    DllClose($hUser32_Dll)
    Exit
EndFunc

post-81487-0-45066200-1424722264_thumb.g

Edited by antonioj84
Link to comment
Share on other sites

Happy now?

; Originally made by MrCreatoR - Updated by Johnmcloud 2015
#include <WinAPI.au3>
#include <Misc.au3>

HotKeySet("{ESC}","_Exit")

Global $hWindow, $iCtrlID, $sWinClass, $sResult, $fToolTip = False
Global $hUser32_Dll = DllOpen("user32.dll")

Run("calc.exe")
WinWait("[CLASS:CalcFrame]")

While 1
    If _IsPressed("0D", $hUser32_Dll) Then
        While _IsPressed("0D", $hUser32_Dll)  ;Waiting until the button is released
            Sleep(10)
        WEnd
        If WinActive("[CLASS:CalcFrame]") And Not $fToolTip Then
            $sResult = WinGetText($hWindow)
            ToolTip("The result has been calculated: " & $sResult, Default, Default, "Calc Info", 1, 5)
            $fToolTip = True
        ElseIf $fToolTip Then
            ToolTip("")
            $fToolTip = False
        EndIf
    EndIf
    If _IsPressed("01", $hUser32_Dll) Then
        While _IsPressed("01", $hUser32_Dll)  ;Waiting until the button is released
            Sleep(10)
        WEnd
        $hWindow = _WinGetHoveredHandle()
        $iCtrlID = _ControlGetHoveredID()
        $sWinClass = _WinAPI_GetClassName($hWindow)
        If $sWinClass = "CalcFrame" And $iCtrlID = 121 And Not $fToolTip Then
            $sResult = WinGetText($hWindow)
            ToolTip("The result has been calculated: " & $sResult, Default, Default, "Calc Info", 1, 5)
            $fToolTip = True
        ElseIf $fToolTip Then
            ToolTip("")
            $fToolTip = False
        EndIf
    EndIf
    Sleep(10)
WEnd

Func _ControlGetHoveredID()
    Local $iOld_Opt_MCM = Opt("MouseCoordMode", 1)
    Local $hRet = DllCall("user32.dll", "int", "WindowFromPoint", "long", MouseGetPos(0), "long", MouseGetPos(1))
    $hRet = DllCall("user32.dll", "int", "GetDlgCtrlID", "hwnd", $hRet[0])
    If $hRet[0] < 0 Then $hRet[0] = 0
    Opt("MouseCoordMode", $iOld_Opt_MCM)
    Return $hRet[0]
EndFunc

Func _WinGetHoveredHandle()
    Local $iOld_Opt_MCM = Opt("MouseCoordMode", 1)
    Local $aRet = DllCall("user32.dll", "int", "WindowFromPoint", "long", MouseGetPos(0), "long", MouseGetPos(1))
    Opt("MouseCoordMode", $iOld_Opt_MCM)
    $aRet = DllCall("User32.dll", "hwnd", "GetAncestor", "hwnd", $aRet[0], "uint", 2) ;$GA_ROOT
    Return HWnd($aRet[0])
EndFunc

Func _Exit()
    DllClose($hUser32_Dll)
    Exit
EndFunc

Edit your post and use the CODE tags, that cool button with the symbol of Autoit, under the Smiley

Link to comment
Share on other sites

  • 2 weeks later...
  • 1 year later...

I'm trying to use a variation of this. My issue is my external app is an autoit window and the class is useless because of that...unless there is a way to set the window class that I haven't found. How can I get the return to be the actual window title instead of the window class?

 

Thanks.

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