You have to import the following method:
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
Then you can call this method this way:
ShowWindow(process.MainWindowHandle, 0);//Hide
ShowWindow(process.MainWindowHandle, 1);//Show
NOTE: The window can just be shown if it is minimized. It won't show it if it is in the background of an other window.
If you want to show a window that is in the background of an other one you have to import this method:
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
Then call it in the same way as ShowWindow
:
SetForegroundWindow(process.MainWindowHandle);
NOTE: You can just set the foreground window if it is not minimized.
You can also combine both methods with IsIconic
to call the right method:
[DllImport("user32.dll")]
static extern bool IsIconic(IntPtr hWnd);//Returns false if the window is minimized
The full code to show the mainwindow:
static void GotoProcess(Process process)
{
if (IsIconic(process.MainWindowHandle))
{
ShowWindow(process.MainWindowHandle, 1);
}
else
{
SetForegroundWindow(process.MainWindowHandle);
}
}