用于 WPF 无边框窗口的 DropShadow [英] DropShadow for WPF Borderless Window
问题描述
我有一个 WPF 窗口,WindowStyle 设置为 none.有什么方法可以强制这个窗口投下阴影(就像当 WindowStyle 不是 none 时你得到的那个)?我不想将 AllowTransparency 设置为 true,因为它会影响性能.而且我也不想禁用硬件渲染(我在某处读到过禁用透明度效果更好).
I have a WPF Window with WindowStyle set to none. Is there some way I can force this window to drop a shadow (like the one you get when WindowStyle is not none)? I don't want to set AllowTransparency to true, because it affects the performance. And I also don't want to disable hardware rendering (I read somewhere that transparency performs better with it disabled).
推荐答案
我写了一个小工具类,它能够完全按照你的要求做:在无边框的 Window
上放置一个标准的阴影,但是将 AllowsTransparency
设置为 false
.
I have written a little utility class that is able to do exactly what you want: drop a standard shadow over a borderless Window
but having AllowsTransparency
set to false
.
您只需要调用DropShadowToWindow(Window window)
方法.最好在窗口的构造函数的 InitializeComponent()
之后进行此调用,但即使在显示窗口之后调用它也能正常工作.
You just have to call the DropShadowToWindow(Window window)
method. It is preferred that you make this call just after the window's constructor's InitializeComponent()
, but it will work even if you call it after the window is shown.
using System;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
public static class DwmDropShadow
{
[DllImport("dwmapi.dll", PreserveSig = true)]
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
[DllImport("dwmapi.dll")]
private static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref Margins pMarInset);
/// <summary>
/// Drops a standard shadow to a WPF Window, even if the window is borderless. Only works with DWM (Windows Vista or newer).
/// This method is much more efficient than setting AllowsTransparency to true and using the DropShadow effect,
/// as AllowsTransparency involves a huge performance issue (hardware acceleration is turned off for all the window).
/// </summary>
/// <param name="window">Window to which the shadow will be applied</param>
public static void DropShadowToWindow(Window window)
{
if (!DropShadow(window))
{
window.SourceInitialized += new EventHandler(window_SourceInitialized);
}
}
private static void window_SourceInitialized(object sender, EventArgs e)
{
Window window = (Window)sender;
DropShadow(window);
window.SourceInitialized -= new EventHandler(window_SourceInitialized);
}
/// <summary>
/// The actual method that makes API calls to drop the shadow to the window
/// </summary>
/// <param name="window">Window to which the shadow will be applied</param>
/// <returns>True if the method succeeded, false if not</returns>
private static bool DropShadow(Window window)
{
try
{
WindowInteropHelper helper = new WindowInteropHelper(window);
int val = 2;
int ret1 = DwmSetWindowAttribute(helper.Handle, 2, ref val, 4);
if (ret1 == 0)
{
Margins m = new Margins { Bottom = 0, Left = 0, Right = 0, Top = 0 };
int ret2 = DwmExtendFrameIntoClientArea(helper.Handle, ref m);
return ret2 == 0;
}
else
{
return false;
}
}
catch (Exception ex)
{
// Probably dwmapi.dll not found (incompatible OS)
return false;
}
}
}
这篇关于用于 WPF 无边框窗口的 DropShadow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!