DropShadow for WPF无边界窗口
发布时间:2020-12-13 21:20:08 所属栏目:Windows 来源:网络整理
导读:我有一个WPF窗口,WindowStyle设置为none。有没有办法我可以强制这个窗口放下一个阴影(就像你在WindowStyle不是没有的时候)?我不想将AllowTransparency设置为true,因为它会影响性能。而且我也不想禁用硬件渲染(我读到透明度更好地禁用的地方)。 我已经写了
我有一个WPF窗口,WindowStyle设置为none。有没有办法我可以强制这个窗口放下一个阴影(就像你在WindowStyle不是没有的时候)?我不想将AllowTransparency设置为true,因为它会影响性能。而且我也不想禁用硬件渲染(我读到透明度更好地禁用的地方)。
我已经写了一个能够完成你想要的工具类:在无边框窗口上放置一个标准的阴影,但是将AllowTransparency设置为false。
你只需要调用DropShadowToWindow(Window窗口)方法。最好在窗口的构造函数InitializeComponent()之后进行此调用,但即使在窗口显示之后调用它也会起作用。 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; } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 批处理文件 – 将文本发送到Windows控制台应用程序的标准输
- 在Windows上从C调用R函数
- 防火墙 – 如何将app / exe列入白名单
- 在弹出窗口中重定向后,window.opener无效(JavaScript)
- G为64位Windows
- windows-phone-7 – 为什么我不能点击/点击Border / Conten
- windows-phone-7 – MvvmCross – 为多个视图共享视图模型
- windows – 在批处理文件中解析字符串
- windows-server-2008 – 如何检查远程SMTP服务器的TLS证书?
- .net – Windows 8 Embedded中缺少MSVCR120_CLR0400.dll
推荐文章
站长推荐
- macos – CGWindowListCopyWindowInfo,kCGWindow
- Force Scons输出(exe,obj,lib和dll)到特定的构建
- windows – 这两个GPO选项有什么区别?
- 如何查看响应端口号被个程序占用(Windows)
- Windows – CMD Echo命令仅保留最后一行文本
- 本地windows 修改mysql 的root密码无效,论 127.
- 怎样用命令行开启或关闭Windows服务
- 如何检测Windows移动设备是否有摄像头? C#
- windows-vista – Vista是否在DCOM调用中更严格地
- windows-server-2008-r2 – “显示桌面图标”的G
热点阅读