我想知道如何禁用(而不是删除/隐藏)WPF 窗口中的“关闭”按钮。我知道如何隐藏它,使窗口的标题栏看起来像这样:
但我想禁用它意味着它应该如下所示:
我正在使用 C# 编写脚本并使用 WPF (Windows Presentation Foundation)。
最佳答案
试试这个:
public partial class MainWindow : Window
{
[DllImport("user32.dll")]
static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll")]
static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
const uint MF_BYCOMMAND = 0x00000000;
const uint MF_GRAYED = 0x00000001;
const uint SC_CLOSE = 0xF060;
public MainWindow()
{
InitializeComponent();
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
// Disable close button
IntPtr hwnd = new WindowInteropHelper(this).Handle;
IntPtr hMenu = GetSystemMenu(hwnd, false);
if (hMenu != IntPtr.Zero)
{
EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
}
}
}
取自here .
确保将 ResizeMode
设置为 NoResize
。
关于c# - 禁用 WPF 窗口标题栏中的关闭按钮 (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17962429/