/// <summary> /// Hides the window /// </summary> private void HideWindow() { // note if mouse is over the notify icon when hiding the window // if it is, we will assume that the user clicked the icon to hide the window this.MouseClickToHideNotifyIcon = WindowPositioning.IsCursorOverNotifyIcon(this.NotifyIcon) && WindowPositioning.IsNotificationAreaActive; if (this.MouseClickToHideNotifyIcon) { this.MouseClickToHideNotifyIconPoint = WindowPositioning.GetCursorPosition(); } this.ReleaseHandlers(); this.Visibility = Visibility.Hidden; }
/// <summary> /// Notify icon clicked or double-clicked method. /// </summary> /// <param name="sender">The sender of the message.</param> /// <param name="args">System.Windows.Forms.MouseEventArgs (which mouse button was pressed, etc.).</param> private void NotifyIconClick(object sender, System.Windows.Forms.MouseEventArgs args) { // sorry if you swapped the primary mouse button if (args.Button == System.Windows.Forms.MouseButtons.Left && (!this.MouseClickToHideNotifyIcon || (WindowPositioning.GetCursorPosition().X != this.MouseClickToHideNotifyIconPoint.X || WindowPositioning.GetCursorPosition().Y != this.MouseClickToHideNotifyIconPoint.Y))) { if (!this.IsLoaded) { this.Show(); } this.UpdateWindowDisplay(true); } else { this.MouseClickToHideNotifyIcon = false; } }
/// <summary> /// Updates the display (position and appearance) of the window. /// </summary> /// <param name="activatewindow">True if the window should be activated, false if not.</param> public void UpdateWindowDisplay(bool activatewindow) { if (this.IsLoaded) { // set handlers if necessary this.SetHandlers(); // get the handle of the window HwndSource windowhandlesource = PresentationSource.FromVisual(this) as HwndSource; bool glassenabled = Compatibility.IsDWMEnabled; //// update location Rect windowbounds = (glassenabled ? WindowPositioning.GetWindowSize(windowhandlesource.Handle) : WindowPositioning.GetWindowClientAreaSize(windowhandlesource.Handle)); // work out the current screen's DPI Matrix screenmatrix = windowhandlesource.CompositionTarget.TransformToDevice; double dpiX = screenmatrix.M11; // 1.0 = 96 dpi double dpiY = screenmatrix.M22; // 1.25 = 120 dpi, etc. Point position = WindowPositioning.GetWindowPosition(this.NotifyIcon, windowbounds.Width, windowbounds.Height, dpiX); // translate wpf points to screen coordinates Point screenposition = new Point(position.X / dpiX, position.Y / dpiY); this.Left = screenposition.X; this.Top = screenposition.Y; // update borders if (glassenabled) { this.Style = (Style)FindResource("AeroBorderStyle"); } else { this.SetNonGlassBorder(this.IsActive); } // fix aero border if necessary if (glassenabled) { // set the root border element's margin to 1 pixel WindowBorder.Margin = new Thickness(1 / dpiX); this.BorderThickness = new Thickness(0); // set the background of the window to transparent (otherwise the inner border colour won't be visible) windowhandlesource.CompositionTarget.BackgroundColor = Colors.Transparent; // get dpi-dependent aero border width int xmargin = Convert.ToInt32(1); // 1 unit wide int ymargin = Convert.ToInt32(1); // 1 unit tall NativeMethods.MARGINS margins = new NativeMethods.MARGINS() { cxLeftWidth = xmargin, cxRightWidth = xmargin, cyBottomHeight = ymargin, cyTopHeight = ymargin }; NativeMethods.DwmExtendFrameIntoClientArea(windowhandlesource.Handle, ref margins); } else { WindowBorder.Margin = new Thickness(0); // reset the margin if the DWM is disabled this.BorderThickness = new Thickness(1 / dpiX); // set the window's border thickness to 1 pixel } if (activatewindow) { this.Show(); this.Activate(); } } }