加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 禁用Windows窗体上的退出按钮?

发布时间:2020-12-15 19:41:23 所属栏目:百科 来源:网络整理
导读:有没有办法禁用 Windows窗体上的退出按钮,而无需导入一些外部.dll?我通过使用以下代码导入dll来禁用退出按钮,但我不喜欢它.有更简单的(内置)方式吗? public Form1() { InitializeComponent(); hMenu = GetSystemMenu(this.Handle,false); } private const
有没有办法禁用 Windows窗体上的退出按钮,而无需导入一些外部.dll?我通过使用以下代码导入dll来禁用退出按钮,但我不喜欢它.有更简单的(内置)方式吗?

public Form1()
    {
        InitializeComponent();
        hMenu = GetSystemMenu(this.Handle,false);
    }

    private const uint SC_CLOSE = 0xf060;
    private const uint MF_GRAYED = 0x01;
    private IntPtr hMenu;

    [DllImport("user32.dll")]
    private static extern IntPtr GetSystemMenu(IntPtr hWnd,bool bRevert);

    [DllImport("user32.dll")]
    private static extern int EnableMenuItem(IntPtr hMenu,uint wIDEnableItem,uint wEnable);

    // handle the form's Paint and Resize events 

    private void Form1_Paint(object sender,PaintEventArgs e)
    {
        EnableMenuItem(hMenu,SC_CLOSE,MF_GRAYED);
    }

    private void Form1_Resize(object sender,EventArgs e)
    {
        EnableMenuItem(hMenu,MF_GRAYED);
    }

解决方法

一点点探索发现了这个方便的助手类:

Disable Close Button and Prevent Form Being Moved (C# version)

它实际上比你正在寻找的更多,但基本上它与你在示例代码中的方式非常相似.帮助程序类为您挂钩加载/调整大小事件,因此您不必记住自己执行此操作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读