嘗試使用PowerShell腳本將當前活動PowerShell窗口移動到屏幕左側。將PowerShell控制檯窗口移動到屏幕左側的最佳方式是什麼?
我找到了this function,但它並沒有真正的例子。
嘗試使用PowerShell腳本將當前活動PowerShell窗口移動到屏幕左側。將PowerShell控制檯窗口移動到屏幕左側的最佳方式是什麼?
我找到了this function,但它並沒有真正的例子。
有趣和有趣的問題。
如果你想移動窗口,你需要知道它的hwnd。對於控制檯,您可以通過kernel32.dll中的GetConsoleWindow函數來完成。
檢查這個腳本,它將PowerShell控制檯移動到左上角大小爲500,500
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int W, int H); '
$consoleHWND = [Console.Window]::GetConsoleWindow();
$consoleHWND
[Console.Window]::MoveWindow($consoleHWND,0,0,500,500);
如果你知道窗口的HWND,你可以做很多事情與此窗口。你可以在這裏找到所有的功能:https://msdn.microsoft.com/en-us/library/windows/desktop/ff468919(v=vs.85).aspx
但是這個腳本只是在真正的powershell控制檯上工作,如果你從Powershell ISE啓動它,hwnd將會是0,因爲Powershell ISE沒有真正的控制檯。
謝謝!這適用於我的情況。 –
爲了給你一個完整的例子有Set-Window
模仿手動窗口 + 左箭頭。
在我的1920 * 1200屏幕後窗口 + 左箭頭窗口尺寸是:
# Left Top Width Heigth
# -7 0 974 1207
所以,你需要先得到屏幕的尺寸,用於主顯示器,這將做到:
Add-Type -AssemblyName System.Windows.Forms
$ScreenSize = [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize
$MyLeft = -7
$MyTop = 0
$MyWidth = $ScreenSize.Width/2+2*7
$MyHeight= $ScreenSize.Height +7
"Left:{0} Top:{1} Width:{2} Height:{3}" -f $MyLeft,$MyTop,$MyWidth,$MyHeight
Get-Process powershell | Set-Window -X $MyLeft -Y $MyTop -Width $MyWidth -Height $MyHeight
有一些例子,例如'Get-Process powershell | Set-Window -X 2040 -Y 142 -Passthru' – Clijsters
謝謝。在我睡了一會然後回到這裏,我讀了代碼,並在腳本中找到了細節。我應該多睡一會兒。 :) –