Jump to content

Identify process window


Anteaus
 Share

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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