c# 嵌入一个外部程序 如何让这个外部程序启动时,窗口就最大化

2025-03-12 22:41:01
推荐回答(1个)
回答1:

在你的class中加入:
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);
[DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]
private static extern void SetForegroundWindow(IntPtr hwnd);
const int WM_SYSCOMMAND = 0x0112;
const int SC_CLOSE = 0xF060;
const int SC_MINIMIZE = 0xF020;
const int SC_MAXIMIZE = 0xF030;
在启动代码中加入
IntPtr mainHandle = FindWindow("a.exe main window class name", "a.exe main window name");
if (mainHandle != null)
{
SetForegroundWindow(mainHandle);
SendMessage(mainHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0); // 最大化
}

可以用Visual studio提供的spy,找到你a.exe程序的主窗口的类名称和窗口名称。