Jump to content

Cancel Button pressed?


Recommended Posts

I have a script that runs an application. This application opens a dialog of its own with 'Yes, No, Cancel'. How can I find out which button the user pressed on the dialog? All usual technics in AutoIt work only with dialogs opend by AutoIt and not by an application.

Link to comment
Share on other sites

I've had to do similar things. i think i used WinActive to trap for the window then ControlGetFocus to trap for the control.

How could you do this on a dialog? The focus is going to shift back to the parent as soon as you click any of the buttons.
Auto3Lib: A library of over 1200 functions for AutoIt
Link to comment
Share on other sites

You could send in your own MsgBox and transfer the operation clicked to the other MsgBox. This gives you the info of which button was pressed.

Example

Opt('WinWaitDelay', 10)

; MsgBox example from external program
$line = "MsgBox(4,'msg-test','some text')"
Run('"' & @AutoItExe & '" /AutoIt3ExecuteLine "' & $line & '"') 

; Control MsgBox with Script MsgBox
If WinWait('msg-test', '', 5) Then
    WinSetState('msg-test', '', @SW_HIDE)
    $result = MsgBox(4, '', 'Choose')
    WinSetState('msg-test', '', @SW_SHOW)
    If $result = 6 Then
        ControlClick('msg-test', '', 'Button1')
        $result = 'Yes'
    Else
        ControlClick('msg-test', '', 'Button2')
        $result = 'No'
    EndIf
    MsgBox(0, '', 'User clicked id: ' & $result)
EndIf
Link to comment
Share on other sites

  • Moderators

You can try the below... mind you... I haven't tested it.

;Assuming the ClassNameNN's of the Dialog buttons are Button1 to Button3
;Button1 = Yes / Button2 = No / Button3 = Cancel
While 1
    If _DialogDetectClick('Dialog Title', 1) Then MsgBox(64, 'Info', 'Yes was pressed')
    If _DialogDetectClick('Dialog Title', 2) Then MsgBox(64, 'Inof', 'No was pressed')
    If _DialogDetectClick('Dialog Title', 3) Then MsgBox(64, 'Info', 'Cancel was pressed')
    Sleep(100)
WEnd
        
Func _DialogDetectClick($hWnd, $iButton)
    If IsString($hWnd) Then $hWnd = WinGetHandle($hWnd)
    $OptMCM = Opt('MouseCoordMode', 2)
    Local $sPrimary = '01'
    If Int(RegRead("HKEY_CURRENT_USER\Control Panel\Mouse", "SwapMouseButtons")) = 1 Then $sPrimary = '02'
    Local $aCpos = ControlGetPos($hWnd, '', 'Button' & $iButton)
    Local $aMpos = MouseGetPos()
    Opt('MouseCoordMode', $OptMCM)
    If Not _IsPressed($sPrimary) Then Return SetError(1, 0, 0)
    Return _MouseInRegion($aMpos, $aCpos)
EndFunc
Func _MouseInRegion($aMpos, $aCpos)
    If Not IsArray($aMpos) Or Not IsArray($aCpos) Then Return SetError(1, 0, 0)
    If $aMpos[0] >= $aCpos[0] _
        And $aMpos[0] <= $aCpos[0] + $aCpos[2] _
        And $aMpos[1] >= $aCpos[1] _
        And $aMpos[1] <= $aCpos[1] + $aCpos[3] Then
        Return 1
    EndIf
    Return SetError(2, 0, 0)
EndFunc
Func _IsPressed($v_R, $v_dll = 'user32.dll')
    $v_R = DllCall($v_dll, 'int', 'GetAsyncKeyState', 'int', '0x' & $v_R)
    Return (Not @error And BitAND($v_R[0], 0x8000) = 0x8000) * 1
EndFunc

Edit:

Forgot a return.

Edit2:

Decided to test it, the above works fine (had to revise it a bit).

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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