更新时间:2022-04-11 23:56:55
您可以使用 findwindowbycaption
获取句柄,然后使用 showwindow
You can use findwindowbycaption
to get the handle then maximize or minimize with showwindow
private const int SW_MAXIMIZE = 3;
private const int SW_MINIMIZE = 6;
// more here: http://www.pinvoke.net/default.aspx/user32.showwindow
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow);
然后在您的代码中使用此代码:
Then in your code you use this:
IntPtr hwnd = FindWindowByCaption(IntPtr.Zero, "The window title");
ShowWindow(hwnd, SW_MAXIMIZE);
尽管看起来您已经通过使用 EnumWindows
拥有了窗口句柄,但在这种情况下,您只需要:
Although it seems you already have the window handle by using EnumWindows
in that case you would only need:
ShowWindow(windows[i].handle, SW_MAXIMIZE);
i
是窗口的索引.
要关闭窗口,您将使用:
to close the window you will use:
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool DestroyWindow(IntPtr hwnd);
在代码中:
DestroyWindow(hwnd) //or DestroyWindow(windows[i].handle)
这是 system.windows.forms.form.close()
或者您可以使用:
Process [] proc Process.GetProcessesByName("process name");
proc[0].Kill();
或者您可以使用:
or you can use:
static uint WM_CLOSE = 0x0010;
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
使用代码:
PostMessage(hWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);