public static bool ActivateWindow(IntPtr handle)
{
if (IsDesktopWindow(handle))
{
Shell32.Shell shell = new Shell32.Shell();
shell.ToggleDesktop();
return true;
}
int style = User32Interop.GetWindowLong(handle, User32Interop.GWL_STYLE);
if ((style & User32Interop.WS_MAXIMIZE) == User32Interop.WS_MAXIMIZE)
{
//It's maximized
User32Interop.SetForegroundWindow(handle);
User32Interop.ShowWindow(handle.ToInt32(), User32Interop.SW_SHOW);
User32Interop.BringWindowToTop(handle);
}
else if ((style & User32Interop.WS_MINIMIZE) == User32Interop.WS_MINIMIZE)
{
//It's minimized
User32Interop.SetForegroundWindow(handle);
User32Interop.ShowWindow(handle.ToInt32(), User32Interop.SW_RESTORE);
User32Interop.BringWindowToTop(handle);
}
else
{
// don't give up just yet!
User32Interop.SetForegroundWindow(handle);
User32Interop.ShowWindow(handle.ToInt32(), User32Interop.SW_SHOW);
User32Interop.BringWindowToTop(handle);
}
return true;
}