SamKr
6/6/2015 - 2:48 PM

Bring form to foreground

Bring form to foreground

private static class User32
{
	[DllImport("User32.dll")]
	internal static extern IntPtr SetForegroundWindow(IntPtr hWnd);

	[DllImport("user32.dll")]
	internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

	internal static readonly IntPtr InvalidHandleValue = IntPtr.Zero;
	internal const int SW_MAXIMIZE = 3;
	internal const int SW_RESTORE = 9;
}

public void Activate()
{
	Process currentProcess = Process.GetCurrentProcess();
	IntPtr hWnd = currentProcess.MainWindowHandle;
	if (hWnd != User32.InvalidHandleValue)
	{
		User32.SetForegroundWindow(hWnd);
		User32.ShowWindow(hWnd, User32.SW_RESTORE);
	}
}

// You can also use this.Handle as hWnd!