c# - 模糊半透明形式的背景(如 Aero 玻璃)

标签 c# wpf winforms aero aero-glass

我有一个无边框、不可调整大小的 WPF 窗体(WindowStyle=None、AllowsTransparency=True、ResizeMode=NoResize),背景是半透明的。下面是在记事本上运行的半透明红色矩形窗体的图片:

the form as it currently appears on top of Notepad

但是,我希望背景变得模糊,就像 Aero glass 那样,除了没有所有花哨的窗口边框和带条纹的彩色背景 - 我想自己处理。这是我希望它看起来像的模型:

the form as I want it to be - blur anything that's below it

我怎样才能以最有效的方式实现这一目标?

WinForms 或 WPF 对我来说没问题。希望它应该使用与 Aero glass 相同的东西(我对它只在启用 Aero 的情况下工作很好),而不是像将下面的屏幕区域捕获为位图并对其进行模糊处理这样疯狂的事情。

这是我不想要的图片:

I don't want the entire Aero glass window chrome

我知道这是可能的,我知道怎么做,但我不希望整个 Aero 玻璃窗口镶边、边框和标题栏或窗口具有用户设置的 Aero 玻璃颜色,只是模糊窗口/表单下方的任何内容的效果。

最佳答案

如果你想使用 Aero 模糊,那么你可以使用 DwmEnableBlurBehindWindow 接口(interface)。下面是一个利用它的示例派生窗口。

public class BlurWindow : Window
{
    #region Constants

    private const int WM_DWMCOMPOSITIONCHANGED = 0x031E;
    private const int DWM_BB_ENABLE = 0x1; 

    #endregion //Constants

    #region Structures
    [StructLayout( LayoutKind.Sequential )]
    private struct DWM_BLURBEHIND
    {
        public int dwFlags;
        public bool fEnable;
        public IntPtr hRgnBlur;
        public bool fTransitionOnMaximized;
    }

    [StructLayout( LayoutKind.Sequential )]
    private struct MARGINS
    {
        public int cxLeftWidth;
        public int cxRightWidth;
        public int cyTopHeight;
        public int cyBottomHeight;
    } 
    #endregion //Structures

    #region APIs

    [DllImport( "dwmapi.dll", PreserveSig = false )]
    private static extern void DwmEnableBlurBehindWindow(IntPtr hwnd, ref DWM_BLURBEHIND blurBehind);

    [DllImport( "dwmapi.dll" )]
    private static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMargins);

    [DllImport( "dwmapi.dll", PreserveSig = false )]
    private static extern bool DwmIsCompositionEnabled(); 

    #endregion //APIs

    #region Constructor
    public BlurWindow()
    {
        this.WindowStyle = System.Windows.WindowStyle.None;
        this.ResizeMode = System.Windows.ResizeMode.NoResize;
        this.Background = Brushes.Transparent;
    } 
    #endregion //Constructor

    #region Base class overrides
    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized( e );

        if ( Environment.OSVersion.Version.Major >= 6 )
        {
            var hwnd = new WindowInteropHelper( this ).Handle;
            var hs = HwndSource.FromHwnd( hwnd );
            hs.CompositionTarget.BackgroundColor = Colors.Transparent;

            hs.AddHook( new HwndSourceHook( this.WndProc ) );
            this.InitializeGlass( hwnd );
        }
    } 
    #endregion //Base class overrides

    #region Methods

    #region InitializeGlass
    private void InitializeGlass(IntPtr hwnd)
    {
        if ( !DwmIsCompositionEnabled() )
            return;

        // fill the background with glass
        var margins = new MARGINS();
        margins.cxLeftWidth = margins.cxRightWidth = margins.cyBottomHeight = margins.cyTopHeight = -1;
        DwmExtendFrameIntoClientArea( hwnd, ref margins );

        // initialize blur for the window
        DWM_BLURBEHIND bbh = new DWM_BLURBEHIND();
        bbh.fEnable = true;
        bbh.dwFlags = DWM_BB_ENABLE;
        DwmEnableBlurBehindWindow( hwnd, ref bbh );
    }
    #endregion //InitializeGlass

    #region WndProc
    private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        if ( msg == WM_DWMCOMPOSITIONCHANGED )
        {
            this.InitializeGlass( hwnd );
            handled = false;
        }

        return IntPtr.Zero;
    } 
    #endregion //WndProc 

    #endregion //Methods
}

这里是使用 BlurWindow 的片段。

var w = new BlurWindow();
w.Width = 100;
w.Height = 100;
w.MouseLeftButtonDown += (s1, e1) => {
    ((Window)s1).DragMove();
    e1.Handled = true;
};
w.Background = new SolidColorBrush( Color.FromArgb( 75, 255, 0, 0 ) );
w.Show();

关于c# - 模糊半透明形式的背景(如 Aero 玻璃),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11386452/

相关文章:

c# - 我将如何在设计时设置我的 UserControl 的标签?

c# - 在 C#.Net 中打开、编辑和保存 PDF 文件

c# - 在 WPF 文本框上调用 Select() 不执行任何操作

c# - 将 URI 打包到引用程序集中的资源

c# - 设置数据网格单元格背景颜色wpf

.net - 滚动条不会在更改滚动值时更新

c# - AJAX HtmlEditorExtender 并从文本框中获取文本?

c# - NUnit - 默认重试异常

c# - 无法从 'System.Web.UI.WebControls.Label' 转换为 'string'?

c# - 如何从另一个类访问表单元素