2017-03-27 45 views

回答

1

有趣和有趣的問題。

如果你想移動窗口,你需要知道它的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沒有真正的控制檯。

+0

謝謝!這適用於我的情況。 –

1

爲了給你一個完整的例子有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 
相關問題