In the top of the class I have:
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("User32.dll")]
private static extern bool ShowWindow(IntPtr handle, int nCmdShow);
private const int SW_SHOWMAXIMIZED = 3;
Then in a method:
public static void BringToFront(IntPtr handle)
{
if (handle == IntPtr.Zero)
return;
// Maximize window
ShowWindow(handle, SW_SHOWMAXIMIZED);
SetForegroundWindow(handle);
}
But I don't want it to be maximized but normal size.
EDIT
This is working.
In form1:
Process[] processes = Process.GetProcessesByName(processName);
SetProcessWindow.BringToFront(processes[0].Id);
SetProcessWindow.CenterProcessWindow(processes[0].Id);
In the class:
public static void BringToFront(int processId)
{
Process process = Process.GetProcessById(processId);
IntPtr handle = process.MainWindowHandle;
if (handle == IntPtr.Zero)
return;
ShowWindow(handle, SW_SHOWNORMAL);
SetForegroundWindow(handle);
}