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 这样的直接位图操作包来解决,并带有必要的modifications.