[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
static extern bool MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
const int SWP_SHOWWINDOW = 0x0040;
static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
private void button1_Click(object sender, EventArgs e)
{
// string executablePath = @"C:\Users\Administrator\Desktop\WindowsFormsApp2\WindowsFormsApp2\bin\Debug\WindowsFormsApp2.exe";
string executablePath = Properties.Settings.Default.executablePath;
Process process = Process.Start(executablePath);
process.WaitForInputIdle(); // 等待进程变为可互动状态
Thread.Sleep(1000);
IntPtr hWnd = process.MainWindowHandle;
if (hWnd != IntPtr.Zero)
{
int screenWidth = Screen.PrimaryScreen.WorkingArea.Width;
int screenHeight = Screen.PrimaryScreen.WorkingArea.Height;
RECT rect;
GetWindowRect(hWnd, out rect);
// 计算窗口的宽度和高度
int windowWidth = rect.Right - rect.Left;
int windowHeight = rect.Bottom - rect.Top;
// 设置窗口位置和大小
SetWindowPos(hWnd, IntPtr.Zero, rect.Left, screenHeight - windowHeight, windowWidth, windowHeight, SWP_SHOWWINDOW);
}
}