模糊的半透明形式的背景(如航空玻璃) [英] Blurring the background of a semi-transparent form (like Aero glass)

查看:672
本文介绍了模糊的半透明形式的背景(如航空玻璃)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个无国界的,不可调整大小WPF形式(WindowStyle =无,AllowsTransparency = TRUE,ResizeMode = NoResize)用半透明背景。这里的形式,半透明的红色矩形,外观的图片,现在,对记事本上运行:

I have a borderless, non-resizable WPF form (WindowStyle=None, AllowsTransparency=True, ResizeMode=NoResize) with a semi-transparent background. Here's a picture of how the form, a semi-transparent red rectangle, looks right now, running on top of Notepad:

不过的形式,我想的背景模糊,像Aero的玻璃是怎么做的,只是没有所有的花哨的窗口边框和背景的彩色条纹 - 我想搞定自己。下面是我多么希望它看起来像一个样机:

However, I'd like the background to be blurred, like how Aero glass does it, except without all the fancy window borders and colored background with stripes - I'd like to handle that myself. Here's a mockup of how I want it to look like:

我怎样才能以最有效的方式实现这一可能。

How can I achieve this in the most efficient way possible?

的WinForms或WPF是罚款由我。但愿它应该使用同样的事情的Aero玻璃用途(我很好,使用它只有在使能的Aero),而不是一些疯狂喜欢捕捉在屏幕下方区域作为位图和模糊的。

WinForms or WPF is fine by me. Hopefully it should use the same thing Aero glass uses (I'm fine with it working only with Aero enabled), instead of something crazy like capturing the screen region below as a bitmap and blurring that.

下面是什么,我不希望有一个画面:

Here is a picture of what I DON'T want:

我知道这是可能的,我知道该怎么做,但我不想整个Aero玻璃窗口镶边,或边框和标题栏或窗口,让用户设置的Aero玻璃色,模糊无论是窗口/表格下方的JUST效果。

I know this is possible and I know how to do it, but I DON'T want the entire Aero glass window chrome, or the borders and title bar, or the window to have the user-set Aero glass color, JUST the effect of blurring whatever is below the window/form.

推荐答案

如果你想使用Aero模糊,那么你可以使用 DwmEnableBlurBehindWindow API。 。下面是利用这样一个例子派生窗口

If you want to use the Aero blur then you can use the DwmEnableBlurBehindWindow api. Here's an example derived Window that utilizes this.

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片段。

And here's a snippet of using the 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();

这篇关于模糊的半透明形式的背景(如航空玻璃)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆