c#winform禁用关闭按钮的方法

本文介绍两种在Windows Form应用程序中禁用窗体关闭按钮的方法:一是通过调用API移除关闭按钮;二是通过修改窗体创建参数来实现。这两种方法均可有效防止用户意外关闭窗口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、设置ControlBox为false
二、调用API实现了禁用关闭按钮
using System;     
using System.Collections.Generic;     
using System.ComponentModel;     
using System.Data;     
using System.Drawing;     
using System.Text;     
using System.Windows.Forms;     
using System.Runtime.InteropServices;     
   
namespace winFormDemo     
{     
public partial class Form2 : 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 Form2()     
{     
InitializeComponent();     
}     
private void Form2_Load(object sender, EventArgs e)     
{     
int hMenu = GetSystemMenu(this.Handle.ToInt32(), 0);     
RemoveMenu(hMenu, SC_CLOSE, MF_REMOVE);     
}     
   
}     
}   

 三、设置构造参数来进行禁用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace winFormDemo     
{
    public partial class NoCloseForm : FormBase
   {
        protected override CreateParams CreateParams
        {
            get
            {
                int CS_NOCLOSE = 0x200;
                CreateParams parameters = base.CreateParams;
                parameters.ClassStyle = CS_NOCLOSE;
               return parameters;
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值