您可以使用MoveWindow - 更改窗口大小和GetWindowRect - 获取窗口的当前位置。
首先添加这个命名空间:
using System.Diagnostics;
using System.Runtime.InteropServices;
下一步 - 添加 P/Invoke 代码:
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, ref Rect lpRect);
[StructLayout(LayoutKind.Sequential)]
private struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
最后,在您的Process.Start 之后,您可以使用这个过程,它会改变 chrome 窗口的大小:
public void SetChromeSize(int width, int height)
{
var procsChrome = Process.GetProcessesByName("chrome"); // there are always many chrome processes, so we have to find the process with a WindowHandle
foreach (var chrome in procsChrome)
{
if (chrome.MainWindowHandle == IntPtr.Zero) // the chrome process must have a window
continue;
var rct = new Rect();
GetWindowRect(chrome.MainWindowHandle, ref rct); // find and use current position of chrome window
MoveWindow(chrome.MainWindowHandle, rct.Left, rct.Top, width, height, true); //use MoveWindow to change size and save current position
break;
}
}