BExplorer.Shell.CombinedWindowActivator.ActivateForm C# (CSharp) Method

ActivateForm() public method

public ActivateForm ( Window form, Window window, IntPtr hwnd ) : void
form System.Windows.Window
window System.Windows.Window
hwnd System.IntPtr
return void
		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);
			}
		}
	}

Usage Example

Example #1
0
 /// <summary>
 /// Rise on change of Main Window state
 /// </summary>
 /// <param name="sender">The main Window</param>
 /// <param name="e">The args</param>
 private void Win_StateChanged(Object sender, EventArgs e) {
   if ((sender as Window)?.WindowState != WindowState.Minimized) {
     ((Window)sender).Visibility = Visibility.Visible;
     CombinedWindowActivator windowsActivate = new CombinedWindowActivator();
     windowsActivate.ActivateForm(sender as Window, null, IntPtr.Zero);
   }
 }
All Usage Examples Of BExplorer.Shell.CombinedWindowActivator::ActivateForm
CombinedWindowActivator