private void OnSourceInitialized(object sender, EventArgs eventArgs) { if (!NativeMethods.DwmIsCompositionEnabled()) { return; } var hwnd = new WindowInteropHelper(this).Handle; var hwndSource = HwndSource.FromHwnd(hwnd); var sizeFactor = hwndSource.CompositionTarget.TransformToDevice.Transform(new Vector(1.0, 1.0)); Background = System.Windows.Media.Brushes.Transparent; hwndSource.CompositionTarget.BackgroundColor = Colors.Transparent; using (var path = new GraphicsPath()) { path.AddEllipse(0, 0, (int)(ActualWidth * sizeFactor.X), (int)(ActualHeight * sizeFactor.Y)); using (var region = new Region(path)) using (var graphics = Graphics.FromHwnd(hwnd)) { var hRgn = region.GetHrgn(graphics); var blur = new NativeMethods.DWM_BLURBEHIND { dwFlags = NativeMethods.DWM_BB.DWM_BB_ENABLE | NativeMethods.DWM_BB.DWM_BB_BLURREGION | NativeMethods.DWM_BB.DWM_BB_TRANSITIONONMAXIMIZED, fEnable = true, hRgnBlur = hRgn, fTransitionOnMaximized = true }; NativeMethods.DwmEnableBlurBehindWindow(hwnd, ref blur); region.ReleaseHrgn(hRgn); } } }
void OnGlass() { PresentationSource p = PresentationSource.FromVisual(this); HwndSource s_hwndSource = p as HwndSource; if (s_hwndSource != null) { s_hwndSource.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0); handle = s_hwndSource.Handle; } NativeMethods.RECT r = new NativeMethods.RECT(); UnsafeNativeMethods.GetClientRect(handle, ref r); IntPtr hrgn = UnsafeNativeMethods.CreateRectRgn(0, 0, 1, 1); NativeMethods.DWM_BLURBEHIND bb = new NativeMethods.DWM_BLURBEHIND(); bb.dwFlags = NativeMethods.DWM_BB_ENABLE | NativeMethods.DWM_BB_BLURREGION; bb.fEnable = true; bb.hRgnBlur = hrgn; UnsafeNativeMethods.DwmEnableBlurBehindWindow(handle, ref bb); NativeMethods.MARGINS mar = new NativeMethods.MARGINS(); mar.cyTopHeight = 37; UnsafeNativeMethods.DwmExtendFrameIntoClientArea(handle, ref mar); //Need to make the Window size dirty. this.WindowState = WindowState.Minimized; this.WindowState = WindowState.Normal; }
private void SetHrgn() { var hwnd = new WindowInteropHelper(this).Handle; var hwndSource = HwndSource.FromHwnd(hwnd); var sizeFactor = hwndSource.CompositionTarget.TransformToDevice.Transform(new Vector(1.0, 1.0)); var dwmIsCompositionEnabled = NativeMethods.DwmIsCompositionEnabled(); Background = Brushes.Transparent; hwndSource.CompositionTarget.BackgroundColor = Colors.Transparent; var blur = new NativeMethods.DWM_BLURBEHIND(); blur.dwFlags = NativeMethods.DWM_BB.DWM_BB_ENABLE | NativeMethods.DWM_BB.DWM_BB_BLURREGION; blur.fEnable = true; var path = new GraphicsPath(); path.AddEllipse(0, 0, (int)(ActualWidth * sizeFactor.X), (int)(ActualHeight * sizeFactor.Y)); var region = new Region(path); //NativeMethods.DwmEnableBlurBehindWindow(hwnd, ref blur); /*using (var path = new GraphicsPath()) * { * path.AddEllipse(0, 0, (int)(ActualWidth * sizeFactor.X), (int)(ActualHeight * sizeFactor.Y)); * * using (var region = new Region(path)) * using (var graphics = Graphics.FromHwnd(hwnd)) * { * var hRgn = region.GetHrgn(graphics); * * User32.SetWindowRgn(hwnd, hRgn, true); * * var blur = new NativeMethods.DWM_BLURBEHIND * { * dwFlags = NativeMethods.DWM_BB.DWM_BB_ENABLE | NativeMethods.DWM_BB.DWM_BB_BLURREGION | NativeMethods.DWM_BB.DWM_BB_TRANSITIONONMAXIMIZED, * fEnable = true, * hRgnBlur = hRgn, * fTransitionOnMaximized = true * }; * * NativeMethods.DwmEnableBlurBehindWindow(hwnd, ref blur); * * * region.ReleaseHrgn(hRgn); * } * }*/ }
private static void DeinitializeGlass(IntPtr hwnd) { // fill the background with glass var margins = new NativeMethods.MARGINS(); margins.cxLeftWidth = margins.cxRightWidth = margins.cyBottomHeight = margins.cyTopHeight = -1; NativeMethods.DwmExtendFrameIntoClientArea(hwnd, ref margins); // initialize blur for the window var bbh = new NativeMethods.DWM_BLURBEHIND { fEnable = false, dwFlags = NativeMethods.DWM_BB.DWM_BB_ENABLE }; NativeMethods.DwmEnableBlurBehindWindow(hwnd, ref bbh); }
/// <summary> /// Enable or disable the blur effect on the form. /// </summary> private void ResetDwmBlurBehind(bool enable, Graphics graphics) { try { NativeMethods.DWM_BLURBEHIND bbh = new NativeMethods.DWM_BLURBEHIND(); if (enable) { bbh.dwFlags = NativeMethods.DWM_BLURBEHIND.DWM_BB_ENABLE; bbh.fEnable = true; if (this.BlurRegion != null) { bbh.hRegionBlur = this.BlurRegion.GetHrgn(graphics); } else { // Apply the blur glass effect to the entire window. bbh.hRegionBlur = IntPtr.Zero; } } else { bbh.dwFlags = NativeMethods.DWM_BLURBEHIND.DWM_BB_ENABLE | NativeMethods.DWM_BLURBEHIND.DWM_BB_BLURREGION; // Turn off the glass effect. bbh.fEnable = false; // Apply the blur glass effect to the entire window. bbh.hRegionBlur = IntPtr.Zero; } NativeMethods.DwmEnableBlurBehindWindow(this.Handle, bbh); } catch (System.Exception ex) { MessageShow.ShowException(this, ex); } }