在透明的WPF窗口后面模糊

我正在尝试使用半透明的无边框窗口创建一个WPF应用程序,该窗口模糊了它背后的背景。

这是我想要做的一个例子。 截图

我曾尝试使用仅适用于Windows Vista / 7的DwmEnableBlurBehindWindow

我试图找到一个适用于Windows 7,8和10的解决方案。

对于任何有兴趣的人我已经找到了适用于Windows 10的解决方案,看起来好像在Windows 8上不可能,就像David Heffernan提到的那样,DwmEnableBlurBehindWindow已从Windows 8中删除,但微软重新推出了一种在Windows中实现此效果的解决方案10。

  • Windows 10: 使用SetWindowCompositionAttribute的解决方案
  • Windows 8:没有解决方案
  • Windows 7:您可以继续使用DwmEnableBlurBehindWindow

我希望我不迟到。 您可以使用SetWindowCompositionAttribute,但是您必须将WindowStyle设置为“None”并实现您自己的本机Windowfunction和句柄。 inheritance自定义控件也非常复杂。 但是,长话短说。 有BlurryControls。

您可以通过浏览“BlurryControls”通过NuGet找到它,或者自己在GitHub上查看代码。 无论如何,我希望这是有帮助的。

在GitHub上,您还可以找到一个示例应用程序。

Windows 8

尽管最好完全忘记死胡同,但我们可能需要确保我们的程序在不同版本中表现一致。 如果不需要动态背景,特别是如果窗口相对较小(主要候选者将是我们自己的应用程序窗口上的消息框或类似),捕获窗口后面的屏幕并手动模糊的解决方案可能会起作用。 在Loaded事件处理程序中:

 var screen = window.Owner.PointToScreen(new System.Windows.Point((window.Owner.ActualWidth - window.ActualWidth) / 2, (window.Owner.ActualHeight - window.ActualHeight) / 2)); var rect = new Rectangle((int)screen.X, (int)screen.Y, (int)window.ActualWidth, (int)window.ActualHeight); var bitmap = new Bitmap(rect.Width, rect.Height); using (var graphics = Graphics.FromImage(bitmap)) graphics.CopyFromScreen(rect.Left, rect.Top, 0, 0, rect.Size); border.Background = new ImageBrush(ApplyGaussianBlur(bitmap.ToBitmapImage())) { TileMode = TileMode.None, Stretch = Stretch.None, AlignmentX = AlignmentX.Center, AlignmentY = AlignmentY.Center, }; 

其中border是最外Border ,通常是完全空的,仅用于Windows 8。

辅助函数是通常的位图转换:

 public static BitmapSource ToBitmapImage(this System.Drawing.Bitmap image) { var hBitmap = image.GetHbitmap(); var bitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); DeleteObject(hBitmap); return bitmap; } 

ApplyGaussianBlur()最好通过直接位图操作包(如WriteableBitmapEx ApplyGaussianBlur()进行解决,并进行必要的修改。

Microsoft从Windows 8开始删除了玻璃效果。这解释了为什么在Windows 8及更高版本中未观察到您期望的模糊效果。 你必须学会​​在没有它的情况下在这些操作系统上生活。