如何使用C#编码在Windows窗体中禁用“关闭”按钮

时间:2011-04-07 11:46:25

标签: c# windows

每一个人。我只想使用C#.net在按钮单击事件上禁用“关闭”按钮。我正在尝试这个,但不能正确地确定它是正确的。

  private void button1_Click(object sender, EventArgs e)
    {
        this.ControlBox = false;
    }

2 个答案:

答案 0 :(得分:1)

添加以下库

using System.Runtime.InteropServices;

将以下内容声明为类级别变量

const int MF_BYPOSITION = 0x400;

[DllImport("User32")]
private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags);

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

[DllImport("User32")]
private static extern int GetMenuItemCount(IntPtr hWnd);

在Form_Load()事件中,编写以下代码

private void Form1_Load(object sender, EventArgs e)
{
        IntPtr hMenu = GetSystemMenu(this.Handle, false);
        int menuItemCount = GetMenuItemCount(hMenu);
        RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION);

}

Disable the Close 'X' button

只需为ClickEvent

修改它

答案 1 :(得分:0)

private const int dis_close_button = 0x200;
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams ObjCP = base.CreateParams;
            ObjCP.ClassStyle = ObjCP.ClassStyle | dis_close_button;
            return ObjCP;
        }
    }

试试,这......