关于c#winform禁用关闭按钮的方法
2010-05-04 12:39
706 查看
关于c#winform禁用关闭按钮的方法
窗口样式中的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 winFormTest
{
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;
hMenu = GetSystemMenu(this.Handle.ToInt32(), 0);
RemoveMenu(hMenu, SC_CLOSE, MF_REMOVE);
}
}
}
//来自http://hi.baidu.com/flydragon1125/blog/item/946158ed4381c6d1b21cb1a9.html
[DllImport("user32.dll")]
static extern IntPtr GetSystemMenu(IntPtr hwnd, bool bRevert);
[DllImport("user32.dll")]
static extern bool DeleteMenu(IntPtr hMenu, uint uPosition, uint uFlags);
[DllImport("user32.dll")]
static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
const uint SC_MOVE = 0xF010; //移动
const uint SC_CLOSE = 0xF060;//关闭
const uint MF_BYCOMMAND = 0x00; //按命令方式
const uint MF_GRAYED = 0x01; //灰掉
const uint MF_DISABLED = 0x02; //不可用
private void Form1_Load(object sender, EventArgs e)
{
IntPtr hMenu = GetSystemMenu(this.Handle, false); //获取程序窗体的句柄
if (hMenu != IntPtr.Zero)
{
DeleteMenu(hMenu, SC_MOVE, MF_BYCOMMAND); //删除移动菜单,禁用移动功能
EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED | MF_DISABLED); //禁用关闭功能
}
}
//来自http://hi.baidu.com/stevenlimin/blog/item/296092af0cbe1cc57cd92aa7.html
在C#中的Form属性没有禁用关闭按钮的属性了。但我们可能通知设置构造参数来进行禁用,方法如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ECT
...{
public partial class NoCloseForm : FormBase
...{
/**//// <summary>
/// 获取已设置无法关闭窗口创建参数。就是这里
/// </summary>
protected override CreateParams CreateParams
...{
get
...{
int CS_NOCLOSE = 0x200;
CreateParams parameters = base.CreateParams;
parameters.ClassStyle |= CS_NOCLOSE;
return parameters;
}
}
public NoCloseForm()
...{
InitializeComponent();
}
}
}
窗口样式中的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 winFormTest
{
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;
hMenu = GetSystemMenu(this.Handle.ToInt32(), 0);
RemoveMenu(hMenu, SC_CLOSE, MF_REMOVE);
}
}
}
//来自http://hi.baidu.com/flydragon1125/blog/item/946158ed4381c6d1b21cb1a9.html
[DllImport("user32.dll")]
static extern IntPtr GetSystemMenu(IntPtr hwnd, bool bRevert);
[DllImport("user32.dll")]
static extern bool DeleteMenu(IntPtr hMenu, uint uPosition, uint uFlags);
[DllImport("user32.dll")]
static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
const uint SC_MOVE = 0xF010; //移动
const uint SC_CLOSE = 0xF060;//关闭
const uint MF_BYCOMMAND = 0x00; //按命令方式
const uint MF_GRAYED = 0x01; //灰掉
const uint MF_DISABLED = 0x02; //不可用
private void Form1_Load(object sender, EventArgs e)
{
IntPtr hMenu = GetSystemMenu(this.Handle, false); //获取程序窗体的句柄
if (hMenu != IntPtr.Zero)
{
DeleteMenu(hMenu, SC_MOVE, MF_BYCOMMAND); //删除移动菜单,禁用移动功能
EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED | MF_DISABLED); //禁用关闭功能
}
}
//来自http://hi.baidu.com/stevenlimin/blog/item/296092af0cbe1cc57cd92aa7.html
在C#中的Form属性没有禁用关闭按钮的属性了。但我们可能通知设置构造参数来进行禁用,方法如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ECT
...{
public partial class NoCloseForm : FormBase
...{
/**//// <summary>
/// 获取已设置无法关闭窗口创建参数。就是这里
/// </summary>
protected override CreateParams CreateParams
...{
get
...{
int CS_NOCLOSE = 0x200;
CreateParams parameters = base.CreateParams;
parameters.ClassStyle |= CS_NOCLOSE;
return parameters;
}
}
public NoCloseForm()
...{
InitializeComponent();
}
}
}
相关文章推荐
- [转]关于c#winform禁用关闭按钮的方法
- 关于winForm窗体禁用关闭按钮的使用方法及禁用最大化、最小化窗口
- C# winform中窗口的关闭按钮的隐藏与禁用的几种方式说明
- c# winform取消右上角关闭按钮的实现方法
- winform 窗体禁用关闭按钮的三种方法
- C# Winform实现捕获窗体最小化、最大化、关闭按钮事件的方法,可通过重写WndProc来实现
- c# winform取消右上角关闭按钮的实现方法
- C# 禁用 关闭窗口 按钮的方法
- C#中Winform窗体Form的关闭按钮变灰色的方法
- C# 禁用 关闭窗口 按钮的方法
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- C# Winform 禁用窗体关闭按钮
- Winform窗体关闭按钮禁用(失效)的四种方法
- [C#]winform 取消右上角关闭按钮方法
- C#禁用关闭按钮的另一种方法
- 如何实现“WinForm窗体禁用关闭按钮”方法功能及源代码
- 关于C#Winform线程调用窗体的使用方法以及窗体的单一显示
- C#禁用窗体的关闭按钮
- C#之WinForm基础 运行多个窗体时,点击按钮,一次关闭所有窗体
- C# WinForm和WPF关闭所有子窗口的方法