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

时间:2012-07-08 20:29:46

标签: c# wpf winforms aero aero-glass

我有一个带有半透明背景的无边框,不可调整大小的WPF格式(WindowStyle = None,allowsTransparency = True,ResizeMode = NoResize)。这是一张图片,显示了一个半透明的红色矩形,现在看起来如何,在记事本上运行:

the form as it currently appears on top of Notepad

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

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

如何以最有效的方式实现这一目标?

WinForms或WPF很好。希望它应该使用Aero玻璃使用的相同的东西(我很好,它只适用于启用Aero),而不是疯狂的东西,如捕获下面的屏幕区域作为位图和模糊。

这是我不想要的照片:

I don't want the entire Aero glass window chrome

我知道这是可能的,我知道怎么做,但我不希望整个Aero玻璃窗口镀铬,或边框和标题栏,或窗口都有用户设置的Aero玻璃颜色,JUST模糊窗口/窗体下方的任何效果。

2 个答案:

答案 0 :(得分:10)

如果您想使用Aero模糊,那么您可以使用 DwmEnableBlurBehindWindow api。这是一个使用它的派生窗口示例。

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();

答案 1 :(得分:1)

我曾做过类似的事,但我不需要以下内容:

  1. 我不需要移动我的表格。
  2. 我的表格下没有动静。
  3. 我做了什么:

    1. 我过去常常将表单窗口最小化(以编程方式)。
    2. 用于捕捉其大小和相同坐标的图像片段的表格。
    3. 应用BlurBitmapEffect后,将该图片设置为背景。
    4. 我认为不是一个很好的答案,但我只是在写我做的事情!

      如果您对此方法感兴趣,本文将为您提供帮助:http://www.codeproject.com/Articles/91487/Screen-Capture-in-WPF-WinForms-Application