public IWindow Show(IntPtr hWnd) { PInvokes.ShowWindowAsync(hWnd, PInvokes.SW.Show); PInvokes.SetForegroundWindow(hWnd); return(new Window(hWnd)); }
public static void Show(IntPtr hWnd) { PInvokes.ShowWindowAsync(hWnd, PInvokes.SW.Show); PInvokes.SetForegroundWindow(hWnd); }
public static MessageToken <TipItem> ShowMessage(System.Windows.FrameworkElement balloon, ObservableCollection <TipItem> data, int?timeout, NotifyPosition position = NotifyPosition.ActiveScreen, PopupAnimation animation = PopupAnimation.None) { var dispatcher = balloon.Dispatcher; if (!dispatcher.CheckAccess()) { return(dispatcher.Invoke(DispatcherPriority.Normal, (Func <MessageToken <TipItem> >)(() => ShowMessage(balloon, data, timeout, position, animation))) as MessageToken <TipItem>); } if (balloon == null) { throw new ArgumentNullException("balloon"); } if (timeout.HasValue && timeout < 500) { var msg = "Invalid timeout of {0} milliseconds. Timeout must be at least 500 ms"; msg = String.Format(msg, timeout); throw new ArgumentOutOfRangeException("timeout", msg); } if (LogicalTreeHelper.GetParent(balloon) is Popup parent) { parent.Child = null; var msg = "Cannot display control [{0}] in a new balloon popup - that control already has a parent. You may consider creating new balloons every time you want to show one."; msg = String.Format(msg, balloon); throw new InvalidOperationException(msg); } var popup = new Popup(); popup.AllowsTransparency = true; popup.PopupAnimation = animation; popup.Placement = PlacementMode.AbsolutePoint; popup.StaysOpen = true; popup.DataContext = data; Point point; switch (position) { case NotifyPosition.Caret: { var rect = Utils.Window.GetCurrentWindowCaretPosition(); var X = (rect.Left + rect.Width / 2 - balloon.ActualWidth / 2); var Y = (rect.Bottom + rect.Height / 2 - balloon.ActualHeight / 2); if (X == 0 && Y == 0) { goto case NotifyPosition.ActiveWindowCenter; } point = new Point(X, Y); break; } case NotifyPosition.ActiveWindowCenter: { var rect = Utils.Window.GetCurrentWindowRect(); var X = (rect.X + rect.Width / 2 - balloon.ActualWidth / 2); var Y = (rect.Y + rect.Height / 2 - balloon.ActualHeight / 2); point = new Point(X, Y); break; } case NotifyPosition.ActiveScreen: { var screen = Screen.FromHandle(Utils.Window.CurrentWindowHandle); if (screen.Equals(Screen.PrimaryScreen)) { var p = TrayInfo.GetTrayLocation(); point = new Point(p.X, p.Y); break; } var bounds = screen.Bounds; var X = bounds.X + bounds.Width; var Y = bounds.Y + bounds.Height; point = new Point(X, Y); break; } case NotifyPosition.Default: { var p = TrayInfo.GetTrayLocation(); point = new Point(p.X, p.Y); break; } default: throw new ArgumentOutOfRangeException(nameof(position) + " not supported", position, null); } popup.Child = balloon; popup.HorizontalOffset = point.X + 1; popup.VerticalOffset = point.Y - 2; balloon.Focusable = true; IInputElement element = null; popup.Opened += (s, a) => { element = Keyboard.FocusedElement; var source = (HwndSource)PresentationSource.FromVisual(balloon); var handle = source.Handle; PInvokes.SetForegroundWindow(handle); Keyboard.Focus(balloon); }; popup.IsOpen = true; popup.Focus(); var r = new MessageToken <TipItem>(popup); popup.Closed += (s, a) => { Keyboard.Focus(element); r.Close(); }; void TimerTick(object sender, EventArgs e) { r.Timer.Tick -= TimerTick; r.Close(); } if (timeout.HasValue) { r.Timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(timeout.Value) }; r.Timer.Tick += TimerTick; r.Timer.Start(); } return(r); }