gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 20:33:12 25 4
gpt4 key购买 nike

我正在按如下方式运行命令。

Start-Process dotnet -ArgumentList "run"

窗口 can be managed使用 -WindowStyle要最大化、最小化、隐藏和正常的标志。但是,我通常做的是将框架向左推(向右推第二个)。

是否可以告诉 PowerShell 将窗口 float 到边缘?像这个一厢情愿的伪代码之类的东西?
Start-Process dotnet -ArgumentList "run" -WindowStyle FloatLeft

最佳答案

试试这个,它使用 -Passthru Start-Process 的选项获取进程信息。然后,我们使用一点 pInvoke将我们刚刚创建的窗口移动到其他地方的魔法。

此示例将使您能够将生成的窗口捕捉到窗口当前屏幕的边缘。您可以指定 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

关于powershell - 使用 PowerShell (Start-Process) 启动进程时是否可以定位窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41229932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com