Jump to content
Sign in to follow this  
Anteaus

Identify process window

Recommended Posts

Anteaus

Requirement:

Launch a process wth Run()

Identify the window belonging to this process.

Wait until this window is in a minimised or trayed state.

Proceed with rest of code.

Bit I can't figure out is how to identify the window-title or other handle associated with the process-number, so as to determine the window state. Any ideas how to do this?

Share this post


Link to post
Share on other sites
sstouk

Since you are starting the process yourself, I would enumerate all the windows existing before running the process and then enumerate the windows after the process was initiated, then parse the list through the filter and get your new windows ID.

The filter is to make sure you are capturing the right window and not the one that was excedentally started by a user manually while you were waiting for the process to startup.

Share this post


Link to post
Share on other sites
Gestalt
WinGetProcess() can filter it. For example, if your PID is 200, then you get the PIDs for the open windows and whichever one is 200 is your window.

Share this post


Link to post
Share on other sites
rasim

Requirement:

Launch a process wth Run()

Identify the window belonging to this process.

Wait until this window is in a minimised or trayed state.

Proceed with rest of code.

Bit I can't figure out is how to identify the window-title or other handle associated with the process-number, so as to determine the window state. Any ideas how to do this?

Example:

$pPID = Run("notepad")
WinWait("Untitled")
$wPID = WinGetProcess("Untitled")

If $pPID = $wPID Then
    While Not WinState()
        Sleep(100)
    WEnd
EndIf

Func WinState()
    Return BitAND(WinGetState("Untitled"), 16)
EndFunc

Share this post


Link to post
Share on other sites
Anteaus

WinGetProcess() can filter it. For example, if your PID is 200, then you get the PIDs for the open windows and whichever one is 200 is your window.

Should've thought of that, yes, it's basically just a question of polling to find the right one.

Thanks.

Share this post


Link to post
Share on other sites
BrettF

Have a look at this:

Func _SetParentWindowPID($PID, $hGUI)
    $hWnd = 0
    $stPID = DllStructCreate("int")
    Do
        $winlist2 = WinList()
        For $i = 1 To $winlist2[0][0]
            If $winlist2[$i][0] <> "" Then
                DllCall("user32.dll", "int", "GetWindowThreadProcessId", "hwnd", $winlist2[$i][1], "ptr", DllStructGetPtr($stPID))
                If DllStructGetData($stPID, 1) = $PID Then
                    $hWnd = $winlist2[$i][1]
                    ExitLoop
                EndIf
            EndIf
        Next
        Sleep(100)
    Until $hWnd <> 0
    $stPID = 0

    If $hWnd <> 0 Then
        $nExStyle = DllCall("user32.dll", "int", "GetWindowLong", "hwnd", $hWnd, "int", -20)
        $nExStyle = $nExStyle[0]
        DllCall("user32.dll", "int", "SetWindowLong", "hwnd", $hWnd, "int", -20, "int", BitOR($nExStyle, $WS_EX_MDICHILD))
        DllCall("user32.dll", "int", "SetParent", "hwnd", $hWnd, "hwnd", $hGUI)
        WinSetState($hWnd, "", @SW_SHOW)
        _WinAPI_RedrawWindow ($hWnd)
    EndIf
    GUISetState()
EndFunc  ;==>_SetParentWindowPID

See if you adapt it. Can't remember where I got it from...

Edited by Bert

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
Sign in to follow this  

×