我有一个无边框、不可调整大小的WPF表单(WindowStyle=None,AllowsTransparency=True,ResizeMode=NoResize),带有半透明背景。这是一个现在运行在记事本上的半透明红色矩形窗体的图片:
我该如何以最高效的方式实现这一目标?
WinForms或WPF都可以。希望它使用与Aero玻璃相同的东西(仅支持启用Aero),而不是像捕获下面屏幕区域作为位图并模糊处理那样的疯狂方法。
这里是一个我不想要的图片:
我有一个无边框、不可调整大小的WPF表单(WindowStyle=None,AllowsTransparency=True,ResizeMode=NoResize),带有半透明背景。这是一个现在运行在记事本上的半透明红色矩形窗体的图片:
我该如何以最高效的方式实现这一目标?
WinForms或WPF都可以。希望它使用与Aero玻璃相同的东西(仅支持启用Aero),而不是像捕获下面屏幕区域作为位图并模糊处理那样的疯狂方法。
这里是一个我不想要的图片:
如果您想使用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();
我曾经做过类似的事情,但我不需要以下内容:
我所做的:
BlurBitmapEffect
之后,将该图像设置为其背景。我想这不是一个很好的答案,但我只是写出了我所做的!
如果您对此方法感兴趣,这篇文章会对您有所帮助:http://www.codeproject.com/Articles/91487/Screen-Capture-in-WPF-WinForms-Application