[英]How to make the Close button disabled in a windows Form using C# coding
每一個。 我只想使用C#.net在按鈕單擊事件上禁用“關閉”按鈕。 我正在為此嘗試,但不能正確確定它是正確的。
private void button1_Click(object sender, EventArgs e)
{
this.ControlBox = false;
}
添加以下庫
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);
}
只需為您的ClickEvent進行修改
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;
}
}
嘗試這個...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.