我尝试使用 -app 标志打开 Chrome 实例,然后移动该窗口并对其进行一些操作。这是我的代码:
#!NumpadMult::
{
Gui, Show , w260 h150, Window title
Gui, Add, Edit, w100 vCustomUrl, http://
Gui, Add, Button, default, OK ; The label ButtonOK (if it exists) will be run when the button is pressed.
ButtonOk:
Gui, Submit
Gui, Destroy
URL = %customUrl%
MyWidth = 639
MyHeight = 389
Run C:\Program Files (x86)\Google\Chrome\Application\chrome.exe --app=data:text/html`,<html><body><script>window.resizeTo(%MyWidth%`,%MyHeight%)`;window.location='%URL%'`;</script></body></html>
return
}
这是迄今为止我发现的打开 Chrome 窗口并移动它的最佳方法,因为我不知道如何在不知道标题的特定 Chrome 窗口上实际执行 WinWait。如果我尝试输出Run
,pid
则pid
与任何现有的 Chrome 进程相同,因此我获得的窗口不一定是我创建的窗口。
有什么想法吗?如果需要,我很乐意提供更多信息。
答案1
MyWidth = 639
MyHeight = 389
InputBox, customUrl, Custom Url, Enter an URL., , 1000, 120
if ErrorLevel
return
run % "chrome.exe" ( winExist("ahk_class Chrome_WidgetWin_1") ? " --new-window " : " " ) customUrl ; https://autohotkey.com/board/topic/82062-open-google-chrome-in-new-instance/
ID := WinWaitCreated("ahk_class Chrome_WidgetWin_1")
WinWait, ahk_id %ID%
WinMove, ahk_id %ID%,,,,%MyWidth%,%MyHeight%
IfWinNotActive, ahk_id %ID%, ,WinActivate,ahk_id %ID%
return
WinWaitCreated( WinTitle:="", WinText:="", Seconds:=0, ExcludeTitle:="", ExcludeText:="" ) {
; HotKeyIt - http://ahkscript.org/boards/viewtopic.php?t=1274
static Found := 0, _WinTitle, _WinText, _ExcludeTitle, _ExcludeText
, init := DllCall( "RegisterShellHookWindow", "UInt",A_ScriptHwnd )
, MsgNum := DllCall( "RegisterWindowMessage", "Str","SHELLHOOK" )
, cleanup:={base:{__Delete:"WinWaitCreated"}}
If IsObject(WinTitle) ; cleanup
return DllCall("DeregisterShellHookWindow","PTR",A_ScriptHwnd)
else if (Seconds <> MsgNum){ ; User called the function
Start := A_TickCount, _WinTitle := WinTitle, _WinText := WinText
,_ExcludeTitle := ExcludeTitle, _ExcludeText := ExcludeText
,OnMessage( MsgNum, A_ThisFunc ), Found := 0
While ( !Found && ( !Seconds || Seconds * 1000 < A_TickCount - Start ) )
Sleep 16
Return Found,OnMessage( MsgNum, "" )
}
If ( WinTitle = 1 ; window created, check if it is our window
&& ExcludeTitle = A_ScriptHwnd
&& WinExist( _WinTitle " ahk_id " WinText,_WinText,_ExcludeTitle,_ExcludeText))
WinWait % "ahk_id " Found := WinText ; wait for window to be shown
}