WinForm窗体禁用关闭按钮

 

禁用最大化窗口:MaximuzeBox 设为False
禁用最小化窗口:MinimizeBox 设为False

using System.Runtime.InteropServices; //禁用关闭按钮需要引用这个


    public partial class Form : Form
    {
        /***************************禁用关闭按钮需要***********************/
        [DllImport("USER32.DLL")]
        public static extern int GetSystemMenu(int hwnd, int bRevert);
        [DllImport("USER32.DLL")]
        public static extern int RemoveMenu(int hMenu, int nPosition, int wFlags);

        const int MF_REMOVE = 0x1000;

        const int SC_RESTORE = 0xF120; //还原
        const int SC_MOVE = 0xF010; //移动
        const int SC_SIZE = 0xF000; //大小
        const int SC_MINIMIZE = 0xF020; //最小化
        const int SC_MAXIMIZE = 0xF030; //最大化
        const int SC_CLOSE = 0xF060; //关闭

        /******************************************************************/

        public Form()
        {
            InitializeComponent();
        }

        private void Form_Load(object sender, EventArgs e)
        {
            /***************************禁用关闭按钮需要***********************/
            int hMenu = GetSystemMenu(this.Handle.ToInt32(), 0);
            RemoveMenu(hMenu, SC_CLOSE, MF_REMOVE);

            /******************************************************************/
        }
    }

posted @   星火燎猿*  阅读(143)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
· 面试官:你是如何进行SQL调优的?
点击右上角即可分享
微信分享提示