禁用WPF窗口(C#)标题栏中的关闭按钮 [英] Disable Close Button In Title Bar of a WPF Window (C#)

查看:131
本文介绍了禁用WPF窗口(C#)标题栏中的关闭按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to know how to disable (not remove/hide) the Close button in a WPF window. I know how to hide it which makes the window's title bar look like this:

但是我想禁用它,这意味着它应该像这样:

But I want to disable it meaning it should look like this:

我正在用C#编写脚本并使用WPF(Windows Presentation Foundation).

I'm scripting in C# and using 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);
        }
    }
}

来自确保将 ResizeMode 设置为 NoResize .

这篇关于禁用WPF窗口(C#)标题栏中的关闭按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆