首页 > 后端开发 > C++ > 如何在 WPF 模式对话框中隐藏关闭按钮,同时保留

如何在 WPF 模式对话框中隐藏关闭按钮,同时保留

Susan Sarandon
发布: 2024-12-30 15:35:15
原创
626 人浏览过

How to Hide the Close Button in a WPF Modal Dialog While Retaining the

Wpf 窗口中隐藏关闭按钮

在 WPF 模态对话框中,我们有时希望隐藏窗口顶部的关闭按钮,但仍然保留标题栏。但是,现有的 ResizeMode、WindowState 和 WindowStyle 属性都不能同时满足这两个要求。下面介绍一种使用 P/Invoke 解决此问题的方案。

首先,在窗口类中添加以下声明:

private const int GWL_STYLE = -16;
private const int WS_SYSMENU = 0x80000;
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
登录后复制

然后,在窗口的 Loaded 事件中放入以下代码:

var hwnd = new WindowInteropHelper(this).Handle;
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
登录后复制

执行上述代码后,关闭按钮将被隐藏。需要注意的是,这只是隐藏了按钮,关闭窗口的快捷键 (Alt F4) 仍然有效。如果需要禁止用户关闭窗口,可以使用 Gabe 提出的方法,重写 OnClosing 事件并设置 Cancel 为 true。

以上是如何在 WPF 模式对话框中隐藏关闭按钮,同时保留的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板