Jump to content

GUISetOnEvent does not work with "Run"


Recommended Posts

Hello,

I have some troubles using GUISetOnEvent with a "Run" instruction.

The "Close" event is never detected: when I click on the "X", nothing happens. There is no error after GUISetOnEvent.

Here after is the script.

Thanks in advance for your answers.

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

Opt("GUIOnEventMode", 1)

;Run process
$iPID = Run("Notepad.exe")

;Allow window to initialize...
Sleep (500)

;Get HWND.
$hWnd = _GetHwndFromPID($iPID)

GUISetOnEvent($GUI_EVENT_CLOSE, "OnClose", $hWnd)
if @error then
        MsgBox($MB_SYSTEMMODAL, "GUISetOnEvent", "Error : " & @error & @CRLF & @extended)
        Exit
EndIf


;Wait 15 seconds to let the user click on X
Sleep(15000)

;Close notepad
WinClose($hWnd)


;Function for getting HWND from PID
Func _GetHwndFromPID($PID)
    $hWnd = 0
    $winlist = WinList()
    Do
        For $i = 1 To $winlist[0][0]
            If $winlist[$i][0] <> "" Then
                $iPID2 = WinGetProcess($winlist[$i][1])
                If $iPID2 = $PID Then
                    $hWnd = $winlist[$i][1]
                    ExitLoop
                EndIf
            EndIf
        Next
    Until $hWnd <> 0
    Return $hWnd
EndFunc;==>_GetHwndFromPID

Func OnClose()
   MsgBox($MB_SYSTEMMODAL, "Message", "Closing the window.", 10)
EndFunc

 

Link to comment
Share on other sites

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

;Run process
$iPID = Run("Notepad.exe")

;Allow window to initialize...
Sleep (500)

;Get HWND.
$hWnd = _GetHwndFromPID($iPID)

While WinExists($hwnd)
   Sleep(200)
WEnd

MsgBox(0, "Info", "Notepad was closed.")

;Function for getting HWND from PID
Func _GetHwndFromPID($PID)
    $hWnd = 0
    $winlist = WinList()
    Do
        For $i = 1 To $winlist[0][0]
            If $winlist[$i][0] <> "" Then
                $iPID2 = WinGetProcess($winlist[$i][1])
                If $iPID2 = $PID Then
                    $hWnd = $winlist[$i][1]
                    ExitLoop
                EndIf
            EndIf
        Next
    Until $hWnd <> 0
    Return $hWnd
EndFunc;==>_GetHwndFromPID

 

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