方法一:
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
WindowsApplication242
{
public
partial
class
Form1 : Form
{
[DllImport(
"user32"
)]
static
extern
IntPtr GetSystemMenu(IntPtr hwnd,
int
bRevert);
[DllImport(
"user32"
)]
static
extern
int
EnableMenuItem(IntPtr hMenu,
int
wIDEnableItem,
int
wEnable);
int
MF_GRAYED = 1;
int
MF_BYCOMMAND = 0;
int
SC_CLOSE = 0xF060;
public
Form1()
{
InitializeComponent();
EnableMenuItem(GetSystemMenu(
this
.Handle, 0),
SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
}
}
}
方法二 :
重写基方法WndProc
//捕捉关闭窗体消息
protected override void WndProc(ref Message m)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_CLOSE = 0xF060;
if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE)
{
return;
}
base.WndProc(ref m);
}