Пример #1
0
        /// <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();
                }
            }
        }