删除最大化WPF自定义窗口的DropShadow
发布时间:2020-12-14 05:35:45 所属栏目:Windows 来源:网络整理
导读:我有一个带有自定义窗口边框的 WPF应用程序(.NET Framework 4).我使用 WPF Shell Integration Library禁用了玻璃边框,并绘制了自己的边框.但是我想在未最大化时在窗口边框周围添加一个DropShadow.我添加了这样的阴影: private static bool DropShadow(Windo
我有一个带有自定义窗口边框的
WPF应用程序(.NET Framework 4).我使用
WPF Shell Integration Library禁用了玻璃边框,并绘制了自己的边框.但是我想在未最大化时在窗口边框周围添加一个DropShadow.我添加了这样的阴影:
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; } } 有关详细信息,请参阅:DropShadow for WPF Borderless Window 使用WindowState.Normal时,此解决方案正常工作!但是,当我最大化应用程序并禁用DWMWA_NCRENDERING_POLICY时,窗口的背景变得稍微透明,并且我的大多数控件渲染完全不同于我以前的. 在下图中,您可以看到最初的最大化状态,以及阴影代码.正如您所看到的,它完全改变了窗口的透明度和阴影代码:o 有什么我想念的吗?我一直在阅读DWM Function library,但找不到答案…… 解决方法
过了一会儿,我从另一个角度重新审视了这个问题并找到了一个更好的解决方案:
public class GlassWindow : Window { [SuppressUnmanagedCodeSecurity] internal static class DwmNativeMethods { [StructLayout(LayoutKind.Sequential)] internal struct DwmMargins { public int cxLeftWidth; public int cxRightWidth; public int cyTopHeight; public int cyBottomHeight; public DwmMargins(bool fullWindow) { this.cxLeftWidth = this.cxRightWidth = this.cyTopHeight = this.cyBottomHeight = fullWindow ? -1 : 0; } } [DllImport("DwmApi.dll")] internal static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd,ref DwmMargins m); [DllImport("DwmApi.dll")] internal static extern int DwmSetWindowAttribute(IntPtr hwnd,int attr,ref int attrValue,int attrSize); } private IntPtr windowHandle; protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); WindowInteropHelper interopHelper = new WindowInteropHelper(this); this.windowHandle = interopHelper.Handle; this.ToggleAreoGlass(this.WindowState != WindowState.Maximized); this.StateChanged += this.GlassWindowStateChanged; } private void ToggleAreoGlass(bool value) { // Enable NcRenderingPolicy int attrValue = 2; int result = DwmNativeMethods.DwmSetWindowAttribute(this.windowHandle,ref attrValue,4); if (result == 0) { // Extend DwmFrame DwmNativeMethods.DwmMargins margins = new DwmNativeMethods.DwmMargins(value); DwmNativeMethods.DwmExtendFrameIntoClientArea(this.windowHandle,ref margins); } } private void GlassWindowStateChanged(object sender,EventArgs e) { this.ToggleAreoGlass(this.WindowState != WindowState.Maximized); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- windows-server-2012 – RDP适用于有线连接,但它在“估计连
- reactjs – window.scrollTo()在反应组件中?
- 用于类似Office 2013的Windows的Windows GUI库?
- windows – PE – 区分数据与函数导出
- Windows更新 – Windows Server停留在检查更新
- windows – 按GUID搜索AD
- windows-runtime – WinRT Xaml Toolkit列系列错误?
- Windows版本vCenter Server升级至6.7问题(不断更新)
- 在Windows 2008中,我应该重命名管理员帐户还是禁用它?
- 如何优化升级RStudio和R并保留项目文件和所有设置(Windows机
推荐文章
站长推荐
- windows-7 – 为什么我的log4net日志条目不会出现
- windows-server-2008 – windows 2008始终保留本
- WINDOWS配置WSUS。
- 如何删除ListView的添加项动画?
- windows-server-2008-r2 – 将组策略环回应用于特
- win10下搭建python3+scarpy虚拟环境
- windows-server-2008 – NTBackup(在WS2k3上)无法
- winapi – 在窗口模式下在Windows 10上等待VBLAN
- 停止屏幕刷新 – INT 0x10(视频服务) – 在装配中
- winapi – SetWindowText慢,Win32 C.
热点阅读