c# - 禁用 WPF 窗口标题栏中的关闭按钮 (C#)

标签 c# wpf

我想知道如何禁用(而不是删除/隐藏)WPF 窗口中的“关闭”按钮。我知道如何隐藏它,使窗口的标题栏看起来像这样:

enter image description here

但我想禁用它意味着它应该如下所示:

enter image description here

我正在使用 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/

相关文章:

c# - MainPageModel 的构造函数中的 App.Current.MainPage NullReferenceException

c# - 从资源中声明一个 const 字符串

c# - 将 inkCanvas 中的图像保存为 png 或 jpeg 文件

c# - 无法使用 RenciSSH 连接到 Mac

c# - 如何在 lambda 表达式中使用数组?

.net - WPF 数据绑定(bind) - 从父级获取值

c# - 相对于元素存储数据字符串的最佳方式

WPF:如何丢弃所有继承的样式?

c# - 如何使我的 LINQ To SQL 语句动态化?

c# - 如何在聚焦时删除 WPF 控件周围的虚线