使用PowerShell(Start-Process)启动进程时,是否可以将窗口定位



我正在运行以下命令。

Start-Process dotnet -ArgumentList "run"

可以使用-WindowStyle标志来管理窗口,以最大化,最小化,隐藏和正常。但是,我通常要做的是将框架推向左侧(右第二)。

是否可以告诉PowerShell将窗户浮动到边缘?像这样的一厢情愿的伪代码?

Start-Process dotnet -ArgumentList "run" -WindowStyle FloatLeft

尝试一下,它使用 -Passthru选项for Start-Process获取流程信息。然后,我们使用一些Pinvoke Magic将我们刚刚创建的窗口移动到其他地方。

此示例将使您可以将产卵窗口捕捉到Windows Current屏幕的边缘。您可以指定X或Y边缘,或两者兼而有之。顶部,如果指定了所有4个开关,则左获胜。

Add-Type -AssemblyName System.Windows.Forms
Add-Type @"
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
public struct RECT
{
    public int left;
    public int top;
    public int right;
    public int bottom;
}
public class pInvoke
{
    [DllImport("user32.dll", SetLastError = true)]
    public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
    public static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
}
"@
function Move-Window([System.IntPtr]$WindowHandle, [switch]$Top, [switch]$Bottom, [switch]$Left, [switch]$Right) {
  # get the window bounds
  $rect = New-Object RECT
  [pInvoke]::GetWindowRect($WindowHandle, [ref]$rect)
  # get which screen the app has been spawned into
  $activeScreen = [System.Windows.Forms.Screen]::FromHandle($WindowHandle).Bounds
  if ($Top) { # if top used, snap to top of screen
    $posY = $activeScreen.Top
  } elseif ($Bottom) { # if bottom used, snap to bottom of screen
    $posY = $activeScreen.Bottom - ($rect.bottom - $rect.top)
  } else { # if neither, snap to current position of the window
    $posY = $rect.top
  }
  if ($Left) { # if left used, snap to left of screen
    $posX = $activeScreen.Left
  } elseif ($Right) { # if right used, snap to right of screen
    $posX = $activeScreen.Right - ($rect.right - $rect.left)
  } else { # if neither, snap to current position of the window
    $posX = $rect.left
  }
  [pInvoke]::MoveWindow($app.MainWindowHandle, $posX, $posY, $rect.right - $rect.left, $rect.bottom - $rect.top, $true)
}
# spawn the window and return the window object
$app = Start-Process dotnet -ArgumentList "run" -PassThru
Move-Window -WindowHandle $app.MainWindowHandle -Bottom -Left

相关内容

最新更新