You can use findwindowbycaption to get a 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);
Although it seems that you already have window captions using EnumWindows , in this case you only need:
ShowWindow(windows[i].handle, SW_MAXIMIZE);
i is the window index.
to close the window that you will use:
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError=true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool DestroyWindow(IntPtr hwnd);
in code:
DestroyWindow(hwnd) //or DestroyWindow(windows[i].handle)
this is an unmanaged version of system.windows.forms.form.close()
or you can use:
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);
in code:
PostMessage(hWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);