Win11,使用Run target,,, &target_pid无法获得正确进程ID Topic is solved

遇到了问题?请先进行搜索(中文和英文),然后在此提问

Moderators: tmplinshi, arcticir

skydiver
Posts: 1
Joined: 24 Oct 2023, 03:33

Win11,使用Run target,,, &target_pid无法获得正确进程ID

24 Oct 2023, 03:49

刚刚接触autohotkey ,尝试下面的demo代码,但是发现无法正确获得PID,{}内的动作并没有没执行,并且MsgBox打印的notepad_pid,与任务管理里查到的notepad.exe的真实PID不一致

Code: Select all

Run "notepad.exe",,, &notepad_pid  ; 测试发现win11环境下,该命令获取的进程ID不准确
if notepad_id := WinWait("ahk_pid " notepad_pid,, 5)
{
    WinActivate "ahk_id " notepad_id
    WinMove 0, 0, A_ScreenWidth/4, A_ScreenHeight/2
}
MsgBox notepad_pid
MsgBox notepad_id
使用任务管理里查询的到的PID,运行如下代码,可正常执行{}内动作。

Code: Select all

notepad_pid_2 := "117596"
if notepad_id_2 := WinWait("ahk_pid " notepad_pid_2,, 5)
{
    WinActivate "ahk_id " notepad_id_2
    WinMove 0, 0, A_ScreenWidth/4, A_ScreenHeight/2
}
MsgBox notepad_pid_2
MsgBox notepad_id_2
求助,各位是否有遇到同样的问题?
谢谢~
WKen
Posts: 183
Joined: 21 Feb 2023, 00:01

Re: Win11,使用Run target,,, &target_pid无法获得正确进程ID  Topic is solved

24 Oct 2023, 06:52

viewtopic.php?p=541162#p541162
或者

Code: Select all

CaptureProgram(true)
Run "notepad.exe"
SetTimer CaptureProgram.Bind(false), -2000

_WinActivate(hwnd) {
    Critical
    try if (winGetTitle("ahk_id " hwnd) = "Notepad") {
        WinActivate hwnd
        WinMove 0, 0, A_ScreenWidth/4, A_ScreenHeight/2, hwnd
        CaptureProgram(false)
    }
}

; 捕获新窗口
CaptureProgram(sHook:=0) {
    if (sHook) {
        DllCall("RegisterShellHookWindow", "UInt", A_ScriptHwnd)
        OnMessage(DllCall("RegisterWindowMessage", "Str", "SHELLHOOK"), SuspendIfActive)
    } else
        DllCall("DeregisterShellHookWindow", "UInt", A_ScriptHwnd)
}

SuspendIfActive(event, hwnd, *) {
    if (event = 1 && hwnd)
        SetTimer _WinActivate.Bind(hwnd), -5
}

Return to “请求帮助”

Who is online

Users browsing this forum: No registered users and 54 guests