【问题标题】:How to make the Close button disabled in a windows Form using C# coding如何使用 C# 编码在 Windows 窗体中禁用关闭按钮
【发布时间】:2011-04-07 20:03:57
【问题描述】:

每一个。我只想使用 C#.net 在按钮单击事件上禁用关闭按钮。我正在为此尝试,但不确定它是否正确。

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

【问题讨论】:

  • 仅供参考,ControlBox 是左上角的图标,而不是关闭按钮
  • 你可以很容易地在网上找到这个:google.nl/… 第一次点击已经显示了一个例子。
  • 为什么不在Form关闭事件中处理
  • 不,我想通过单击按钮来实现,如果我在表单关闭中使用,那么我将无法关闭表单。
  • 顺便说一句,总有人可以关闭表单。如果您删除关闭按钮,您可以使用控制框。如果删除它,您可以使用 Alt+F4。如果删除它,您可以右键单击任务栏并选择“关闭”。如果你删除它,你可以使用任务管理器...

标签: c# windows


【解决方案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 修改它

【讨论】:

    【解决方案2】:
    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;
            }
        }
    

    试试这个……

    【讨论】:

      猜你喜欢
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 2015-05-09
      • 2013-07-20
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      相关资源
      最近更新 更多