public override void ActivateForm(Window form, Window window, IntPtr hwnd) {
var fHandle = (PresentationSource.FromVisual(form) as HwndSource).Handle;
var wHandle = window == null ? IntPtr.Zero : (PresentationSource.FromVisual(window) as HwndSource).Handle;
if (window == null || wHandle != fHandle) {
IntPtr Dummy = IntPtr.Zero;
IntPtr hWnd = fHandle;
if (User32.IsIconic(hWnd)) {
User32.ShowWindowAsync(hWnd, SW_RESTORE);
} else {
//User32.ShowWindowAsync(hWnd, SW_SHOW);
form.ShowActivated = true;
form.Show();
}
User32.SetForegroundWindow(hWnd);
form.Activate();
form.Topmost = true;
form.Topmost = false;
// Code from Karl E. Peterson, www.mvps.org/vb/sample.htm
// Converted to Delphi by Ray Lischner
// Published in The Delphi Magazine 55, page 16
// Converted to C# by Kevin Gale
IntPtr foregroundWindow = User32.GetForegroundWindow();
if (foregroundWindow != hWnd) {
uint foregroundThreadId = User32.GetWindowThreadProcessId(foregroundWindow, Dummy);
uint thisThreadId = User32.GetWindowThreadProcessId(hWnd, Dummy);
if (User32.AttachThreadInput(thisThreadId, foregroundThreadId, true)) {
form.Activate();
form.Topmost = true;
form.Topmost = false;
User32.BringWindowToTop(hWnd); // IE 5.5 related hack
User32.SetForegroundWindow(hWnd);
User32.AttachThreadInput(thisThreadId, foregroundThreadId, false);
}
}
if (User32.GetForegroundWindow() != hWnd) {
// Code by Daniel P. Stasinski
// Converted to C# by Kevin Gale
IntPtr Timeout = IntPtr.Zero;
User32.SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, Timeout, 0);
User32.SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, Dummy, SPIF_SENDCHANGE);
User32.BringWindowToTop(hWnd); // IE 5.5 related hack
User32.SetForegroundWindow(hWnd);
form.Activate();
form.Topmost = true;
form.Topmost = false;
User32.SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, Timeout, SPIF_SENDCHANGE);
}
Shell32.FlashWindow(fHandle, 0);
}
}
}