Exemple #1
0
        private static bool _ExtendGlassFrameInternal(Window window, Thickness margin)
        {
            Assert.IsNotNull(window);
            Assert.IsTrue(window.CheckAccess());

            // Expect that this might be called on OSes other than Vista.
            if (!Utility.IsOSVistaOrNewer)
            {
                // Not an error.  Just not on Vista so we're not going to get glass.
                return(false);
            }

            IntPtr hwnd = new WindowInteropHelper(window).Handle;

            if (IntPtr.Zero == hwnd)
            {
                throw new InvalidOperationException("Window must be shown before extending glass.");
            }

            HwndSource hwndSource = HwndSource.FromHwnd(hwnd);

            bool isGlassEnabled = NativeMethods.DwmIsCompositionEnabled();

            if (!isGlassEnabled)
            {
                window.Background = SystemColors.WindowBrush;
                hwndSource.CompositionTarget.BackgroundColor = SystemColors.WindowColor;
            }
            else
            {
                // Apply the transparent background to both the Window and the HWND
                window.Background = Brushes.Transparent;
                hwndSource.CompositionTarget.BackgroundColor = Colors.Transparent;

                // Thickness is going to be DIPs, need to convert to system coordinates.
                Point deviceTopLeft     = DpiHelper.LogicalPixelsToDevice(new Point(margin.Left, margin.Top));
                Point deviceBottomRight = DpiHelper.LogicalPixelsToDevice(new Point(margin.Right, margin.Bottom));

                var dwmMargin = new MARGINS
                {
                    // err on the side of pushing in glass an extra pixel.
                    cxLeftWidth    = (int)Math.Ceiling(deviceTopLeft.X),
                    cxRightWidth   = (int)Math.Ceiling(deviceBottomRight.X),
                    cyTopHeight    = (int)Math.Ceiling(deviceTopLeft.Y),
                    cyBottomHeight = (int)Math.Ceiling(deviceBottomRight.Y),
                };

                NativeMethods.DwmExtendFrameIntoClientArea(hwnd, ref dwmMargin);
            }

            // Even if glass isn't currently enabled, add the hook so we can appropriately respond
            // if that changes.

            bool addHook = !_extendedWindows.ContainsKey(hwnd);

            if (addHook)
            {
                HwndSourceHook hook = delegate(IntPtr innerHwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
                {
                    if (WM.DWMCOMPOSITIONCHANGED == (WM)msg)
                    {
                        _ExtendGlassFrameInternal(window, margin);
                        handled = false;
                    }
                    return(IntPtr.Zero);
                };

                _extendedWindows.Add(hwnd, hook);
                hwndSource.AddHook(hook);
                window.Closing += _OnExtendedWindowClosing;
            }

            return(isGlassEnabled);
        }
        private void _InitializeCaptionHeight()
        {
            Point ptCaption = new Point(0, NativeMethods.GetSystemMetrics(SM.CYCAPTION));

            WindowCaptionHeight = DpiHelper.DevicePixelsToLogical(ptCaption).Y;
        }