SevenSoftware.Windows.AeroGlass.EnableGlass C# (CSharp) Method

EnableGlass() public static method

Enables Aero Glass on a WPF window, no exception thrown if OS does not support DWM.
public static EnableGlass ( Window window, Margins margins ) : void
window System.Windows.Window The window to enable glass.
margins Margins The region to add glass.
return void
        public static void EnableGlass(Window window, Margins margins)
        {
            if (Environment.OSVersion.Version.Major < 6)
            {
                return;
            }

            if (margins.TopHeight == 0 && margins.BottomHeight == 0 && margins.RightWidth == 0 && margins.LeftWidth == 0)
            {
                margins = new Margins(true);
            }

            if (window == null)
            {
                throw new ArgumentNullException("window");
            }

            IntPtr windowHandle = new WindowInteropHelper(window).Handle;

            if (windowHandle == IntPtr.Zero)
            {
                return;
            }

            // add Window Proc hook to capture DWM messages
            HwndSource source = HwndSource.FromHwnd(windowHandle);
            if (source == null)
            {
                throw new FormatException();
            }

            source.AddHook(WndProc);

            // Set the Background to transparent from Win32 perspective
            HwndSource handleSource = HwndSource.FromHwnd(windowHandle);
            if (handleSource != null)
            {
                if (handleSource.CompositionTarget != null)
                {
                    handleSource.CompositionTarget.BackgroundColor = Colors.Transparent;
                }
            }

            // Set the Background to transparent from WPF perspective
            window.Background = Brushes.Transparent;

            ResetAeroGlass(margins, windowHandle);
        }