我正在嘗試使用此答案中給出的腳本 在 PowerShell 5 和 6 中設定視窗大小和位置
設定多個視窗資源管理器視窗的高度和大小。不是網路瀏覽器......稱為「瀏覽器」的文件瀏覽器。
它與“記事本”程序一起使用。但不是「探索者」程序。
#works
Set-Window -ProcessName notepad-X 400 -Y 400 -Width 400 -Height 700
#doesnt work
Set-Window -ProcessName explorer -X 400 -Y 400 -Width 400 -Height 700
理想情況下我想要一個腳本:
- 開啟 3 個資源管理器視窗。
- 導航至檔案路徑 A、B、C
- 將每個視窗的大小調整到螢幕上的特定位置
如何在不安裝任何額外軟體的情況下僅使用原始 powershell 來做到這一點?
編輯:使用harrymc的建議後,我已經解決了問題的一半..我可以移動窗口,但我只需要弄清楚如何獲取3個資源管理器子進程的句柄...
$MethodDefinition = @'
[DllImport("user32.dll")]
public extern static bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw);
'@
$Kernel32 = Add-Type -MemberDefinition $MethodDefinition -Name 'Kernel32' -Namespace 'Win32' -PassThru
# How do I get 3 child explorer IDs here?
# i can't pass in 'explorer' name because that references the parent process running the whole GUI
$Handle = (Get-Process -Name "notepad").MainWindowHandle
$Return = [Window]::MoveWindow($Handle, 10, 20, 400, 400,$True)
編輯2:
我嘗試透過 Start-Process 函數取得資源管理器窗口,但收到錯誤:
$er3 = (Start-Process explorer -passthru)
PS C:\> (Get-Process -Id $er3.Id).MainWindowHandle
Get-Process : Cannot find a process with the process identifier 10572.At line:1 char:2
+ (Get-Process -Id $er3.Id).MainWindowHandle
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (10572:Int32) [Get-Process], ProcessCommandException
+ FullyQualifiedErrorId : NoProcessFoundForGivenId,Microsoft.PowerShell.Commands.GetProcessCommand
它說它已經退出...但資源管理器文件瀏覽器視窗保持打開狀態...不確定這裡發生了什麼。如果我用記事本嘗試它,它會起作用...
$er4 = (Start-Process notepad -passthru)
PS C:\> (Get-Process -Id $er4.Id).MainWindowHandle
9899994
編輯 3:我已經使用 ComObject 並訪問 item(0) 弄清楚了。
$ex4 = New-Object -ComObject Shell.Application
$ex4.open("C:\")
# $ex4.windows()[0].Width = 400 # breaks
$ex5 = $ex4.Windows()[0]
$ex6 = $ex5.Item(0) # not sure why i need to do this extra step
$ex6.Width = 400
$ex6.Navigate("file:///C:/Folder1/Folder2")
答案1
使用本機 Windows API 應該可以實作。像這樣的事情:
[DllImport("User32.dll")]
public extern static bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw);
...
$Handle = (Get-Process -Id $ProcessId).MainWindowHandle
$Return = [Window]::MoveWindow($Handle, $x, $y, $Width, $Height,$True)
由於此通用程式碼不適用於資源管理器,因此這裡有一個替代解決方案(已測試):
$ex1 = New-Object -ComObject Shell.Application
$ex1.open("C:\")
$ex1.windows()[0].Top = 10
# also assignable : Left, Width, Height
# if required : $handle = $ex1.windows()[0].HWND
答案2
PowerShell 其實不是一個 UI 自動化工具。根據您所指出的,為什麼不直接使用...
$w = Get-UIAWindow -ProcessName notepad
$w.Move(100, 100)
項目介紹
當您進行 GUI 測試時,UIAutomation 模組可以簡化軟體測試自動化。此模組基於 UI 自動化函式庫(自 3.0 起成為 .Net Framework 的一部分),旨在讓軟體工程師的生活盡可能輕鬆。
WASP 是一個 PowerShell 管理單元,用於執行 Windows 自動化任務,例如選擇視窗和控制項以及傳送滑鼠和鍵盤事件。我們有 cmdlet,例如 Select-Window、Select-Control、Send-Keys、Send-Click、Get-WindowPosition、Set-WindowPosition、Set-WindowActive、Remove-Window
注意:這些不再維護,但它仍然按設計工作,並且您使用的範例程式碼從未或永遠不會被維護。
另請參閱這個完全維護的解決方案:
最新版本的 AutoIt 腳本語言現在為 PowerShell 使用者帶來了好處。一組本機 PowerShell Cmdlet!這可讓您將 AutoIt 的獨特功能(視窗操作和按鍵模擬)新增至常用的 PowerShell 腳本。作為額外的好處,AutoIt PowerShell Cmdlet 和程序集經過數位簽名,因此可以與更嚴格的執行策略一起使用。 Cmdlet 也將與 x86 和 x64 版本的 PowerShell 一起本機運作!