Ejemplo n.º 1
0
        /// <summary>
        /// Sets glass background to custom margins in the window.
        /// </summary>
        /// <param name="win"></param>
        public static void GlassBackground(this Window win, int left, int right, int top, int bottom)
        {
            // Why would you read the inner workings? Why? If you need to know why...
            // DwmExtendFrameIntoClientArea http://msdn.microsoft.com/en-us/library/aa969512%28VS.85%29.aspx is the magical WINAPI call
            // rest is just crap to get its parameters populated.
            win.Loaded += delegate(object sender, RoutedEventArgs e)
            {
                try
                {
                    // Obtain the window handle for WPF application
                    IntPtr     mainWindowPtr = new WindowInteropHelper(win).Handle;
                    HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);

                    // Transparent shall be glassed!
                    mainWindowSrc.CompositionTarget.BackgroundColor = System.Windows.Media.Colors.Transparent;

                    // Margin for the DwmExtendFrameIntoClientArea WINAPI call.
                    NonClientRegionAPI.MARGINS margins = new NonClientRegionAPI.MARGINS();
                    margins.cxLeftWidth    = left;
                    margins.cxRightWidth   = right;
                    margins.cyBottomHeight = bottom;
                    margins.cyTopHeight    = top;

                    // Glass extend WINAPI thingie http://msdn.microsoft.com/en-us/library/aa969512%28VS.85%29.aspx form more details
                    int hr = NonClientRegionAPI.DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins);
                    if (hr < 0)
                    {
                        //DwmExtendFrameIntoClientArea Failed
                    }
                    else
                    {
                        win.Background = System.Windows.Media.Brushes.Transparent;
                    }
                }
                // If not glassing capabilities (Windows XP...), paint background white.
                catch (DllNotFoundException)
                {
                    Application.Current.MainWindow.Background = System.Windows.Media.Brushes.White;
                }
            };
        }
Ejemplo n.º 2
0
        void OnLoaded(object sender, RoutedEventArgs e)
        {
            try
            {
                // Obtain the window handle for WPF application
                IntPtr     mainWindowPtr = new WindowInteropHelper(this).Handle;
                HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
                mainWindowSrc.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0);

                // Get System Dpi
                System.Drawing.Graphics desktop = System.Drawing.Graphics.FromHwnd(mainWindowPtr);
                float DesktopDpiX = desktop.DpiX;
                float DesktopDpiY = desktop.DpiY;

                // Set Margins
                NonClientRegionAPI.MARGINS margins = new NonClientRegionAPI.MARGINS();

                // Extend glass frame into client area
                // Note that the default desktop Dpi is 96dpi. The  margins are
                // adjusted for the system Dpi.
                margins.cxLeftWidth    = Convert.ToInt32(5 * (DesktopDpiX / 96));
                margins.cxRightWidth   = Convert.ToInt32(5 * (DesktopDpiX / 96));
                margins.cyTopHeight    = Convert.ToInt32(((int)topBar.ActualHeight + 5) * (DesktopDpiX / 96));
                margins.cyBottomHeight = Convert.ToInt32(5 * (DesktopDpiX / 96));

                int hr = NonClientRegionAPI.DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins);
                //
                if (hr < 0)
                {
                    //DwmExtendFrameIntoClientArea Failed
                }
            }
            // If not Vista, paint background white.
            catch (DllNotFoundException)
            {
                Application.Current.MainWindow.Background = Brushes.White;
            }
        }