您的位置:首页 > 编程语言 > C#

关于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();


}


}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐