BrightnessTray.WindowPositioning.GetWindowClientAreaSize C# (CSharp) Метод

GetWindowClientAreaSize() публичный статический Метод

Returns a Rect containing the bounds of the specified window's client area (i.e. area excluding border).
public static GetWindowClientAreaSize ( IntPtr hWnd ) : Rect
hWnd System.IntPtr Handle of the window.
Результат System.Windows.Rect
        public static Rect GetWindowClientAreaSize(IntPtr hWnd)
        {
            NativeMethods.RECT result = new NativeMethods.RECT();
            if (NativeMethods.GetClientRect(hWnd, out result))
                return result;
            else
                throw new Exception(String.Format("Could not find client area bounds for specified window (handle {0:X})", hWnd));
        }

Usage Example

        /// <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();
                }
            }
        }