DwmIsCompositionEnabled() private method

private DwmIsCompositionEnabled ( ) : bool
return bool
Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        private static bool _SetWindowThemeAttribute(Window window, bool showCaption, bool showIcon)
        {
            bool isGlassEnabled;

            Assert.IsNotNull(window);
            Assert.IsTrue(window.CheckAccess());

            // This only is expected to work if Aero glass is enabled.
            try
            {
                isGlassEnabled = NativeMethods.DwmIsCompositionEnabled();
            }
            catch (DllNotFoundException)
            {
                // 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 we can modify attributes.");
            }

            var options = new WTA_OPTIONS
            {
                dwMask = (WTNCA.NODRAWCAPTION | WTNCA.NODRAWICON)
            };

            if (isGlassEnabled)
            {
                if (!showCaption)
                {
                    options.dwFlags |= WTNCA.NODRAWCAPTION;
                }
                if (!showIcon)
                {
                    options.dwFlags |= WTNCA.NODRAWICON;
                }
            }

            NativeMethods.SetWindowThemeAttribute(hwnd, WINDOWTHEMEATTRIBUTETYPE.WTA_NONCLIENT, ref options, WTA_OPTIONS.Size);

            bool addHook = !_attributedWindows.ContainsKey(hwnd);

            if (addHook)
            {
                HwndSourceHook hook = delegate(IntPtr unusedHwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
                {
                    if (WM.DWMCOMPOSITIONCHANGED == (WM)msg)
                    {
                        _SetWindowThemeAttribute(window, showCaption, showIcon);
                        handled = false;
                    }
                    return(IntPtr.Zero);
                };

                _attributedWindows.Add(hwnd, hook);
                HwndSource.FromHwnd(hwnd).AddHook(hook);
                window.Closing += _OnAttributedWindowClosing;
            }

            return(isGlassEnabled);
        }