Exemple #1
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
            {
                NativeMethods.DwmIsCompositionEnabled(out isGlassEnabled);
            }
            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;
        }
Exemple #2
0
 public static extern void SetWindowThemeAttribute([In] IntPtr hwnd, [In] WINDOWTHEMEATTRIBUTETYPE eAttribute, [In] ref WTA_OPTIONS pvAttribute, [In] uint cbAttribute);
        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);
        }