WinWait by process name

问题描述 投票:-1回答:1

我正在尝试执行此代码,但无法获取进程名称。

我看到ProcessWait函数将等待直到进程存在,但我需要等待窗口进程打开(不活动)

WinWait,仅当我使用TITLE作为字符串时,它才有效。

#AutoIt3Wrapper_UseX64=N
Example()

Func Example()
    ; Run Notepad


    ; Wait 10 seconds for the Notepad window to appear.
    WinWait("[CLASS:ProcessName]", "", 5000)


    ; Wait for 2 seconds to display the Notepad window.
    Sleep(2000)


    ; Close the Notepad window using the classname of Notepad.
    WinClose("[CLASS:ProcessName]")
   Opt("TrayIconHide", 1)
EndFunc   ;==>Example
autoit
1个回答
0
投票

确定,那么我想这些功能将为您提供帮助。

#include <array.au3>
$iPid = Run("notepad.exe")
Sleep(100) ; allow notepad to create window
$aArray = _WinGetHandleByPID($iPid)
_ArrayDisplay($aArray, "by Pid, only visible windows")
$aArray = _WinGetHandleByPID("notepad.exe", -1)
_ArrayDisplay($aArray, "by Name, all windows")
$aArray = _WinGetHandleByPID("notepad.exe", 0)
_ArrayDisplay($aArray, "by Name, only invisible windows")
$aArray = _WinGetHandleByPID("notepad.exe", 1)
_ArrayDisplay($aArray, "by Name, only visible windows")
$hHandle = _WinGetHandleByPID("notepad.exe", 2)
ControlSend($hHandle,"","","hello world.")

Func _WinGetHandleByPID($vProcess, $nShow = 1)
    ; Get Window Handle by PID
    ; Author Hubertus
    ; derived from Smoke_N's script with the same name,
    ; but to handle more than one process with the same name
    ; and to return handle of newest window ($nShow = 2).
    ;
    ; $vProcess = Processname(e.g. "Notepad.exe") or Processnumber(e.g. 4711 )
    ; $nShow = -1 "All (Visble or not)", $nShow = 0 "Not Visible Only", $nShow = 1 "Visible Only", $nShow = 2 "return handle of newest  window "
    ; @error = 0   Returns array of matches. Array[0][0] and @extended shows number of matches.
    ; @error = 0 and $nShow = 2   return handle of newest window
    ; Array[n][0] shows windows title. Array[n][1] shows windows handle. n = 1 to @extended
    ; @error = 1   Process not found.
    ; @error = 2   Window not found.
    If Not ProcessExists($vProcess) Then Return SetError(1, 0, 0) ; no matching process
    Local $iWinList, $aWinList = WinList()
    Local $iResult, $aResult[UBound($aWinList)][2]
    Local $iProcessList, $aProcessList = ProcessList($vProcess)
    If $aProcessList[0][0] = 0 Then Local $aProcessList[2][2] = [[1, 0],["", $vProcess]]
    For $iWinList = 1  To  $aWinList[0][0]
        For $iProcessList = 1 To $aProcessList[0][0]
            If WinGetProcess($aWinList[$iWinList][1]) = $aProcessList[$iProcessList][1] Then
                If $nShow > -1 And Not $nShow = (2 = BitAND(WinGetState($aWinList[$iWinList][1]), 2)) Then ContinueLoop
                $iResult += 1
                $aResult[$iResult][0] = $aWinList[$iWinList][0]
                $aResult[$iResult][1] = $aWinList[$iWinList][1]
            EndIf
        Next
    Next
    If $iResult = 0 Then Return SetError(2, 0, 0) ; no window found
    ReDim $aResult[$iResult + 1][2]
    $aResult[0][0] = $iResult
    If $nShow = 2  Then Return SetError(0, $iResult, $aResult[1][1])
    Return SetError(0, $iResult, $aResult)
EndFunc   ;==>_WinGetHandleByPID

另一个不错的帮助功能

#include <Array.au3>
$re = ProcessGetWindow(ProcessExists('firefox.exe'))
_ArrayDisplay($re)

Func ProcessGetWindow($PId)
    $PId = ProcessExists($PId)
    If $PId = 0 Then
        SetError(1)
    Else
        Local $WinList = WinList()
        Local $WindowTitle[1][2]
        Local $x = 0
        For $i = 1 To $WinList[0][0]
            If WinGetProcess($WinList[$i][1], "") = $PId And $WinList[$i][0] <> "" Then
                ReDim $WindowTitle[$x + 1][2]
                $WindowTitle[$x][0] = $WinList[$i][0]
                $WindowTitle[$x][1] = $WinList[$i][1]
                $x += 1
            EndIf
        Next
        Return $WindowTitle
    EndIf
EndFunc   ;==>ProcessGetWindow

有时您必须检查该过程是否从正确的路径开始。

Global $Pfad = _ProcessGetPath("firefox.exe")

MsgBox(0, "", "Prozesspfad: " & $Pfad)

Func _ProcessGetPath($PID)
    If IsString($PID) Then $PID = ProcessExists($PID)
    Local $Path, $dll, $handle, $ret
    $Path = DllStructCreate("char[1000]")
    $dll = DllOpen("Kernel32.dll")
    $handle = DllCall($dll, "int", "OpenProcess", "dword", 0x0400 + 0x0010, "int", 0, "dword", $PID)
    $ret = DllCall("Psapi.dll", "long", "GetModuleFileNameEx", "long", $handle[0], "int", 0, "ptr", DllStructGetPtr($Path), "long", DllStructGetSize($Path))
    $ret = DllCall($dll, "int", "CloseHandle", "hwnd", $handle[0])
    DllClose($dll)
    Return DllStructGetData($Path, 1)
EndFunc   ;==>_ProcessGetPath
© www.soinside.com 2019 - 2024. All rights reserved.