C#,winform中如何屏蔽右上角的X关闭按钮
实际上发出问题后两分钟我就找到答案了,呵呵第一种方法就是你们说的那样,用法:添加引用usingSystem.Runtime.InteropServices,在窗体的loa...
实际上发出问题后两分钟我就找到答案了,呵呵
第一种方法就是你们说的那样,用法:添加引用using System.Runtime.InteropServices,在窗体的load事件中引用方法CloseButtonEnable();
此外另一种方法(实现功能屏蔽,但可以正常显示):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);
} 用法:直接将代码放到窗体的cs文件中就可以了
不过,还是谢谢你们的热心啊 展开
第一种方法就是你们说的那样,用法:添加引用using System.Runtime.InteropServices,在窗体的load事件中引用方法CloseButtonEnable();
此外另一种方法(实现功能屏蔽,但可以正常显示):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);
} 用法:直接将代码放到窗体的cs文件中就可以了
不过,还是谢谢你们的热心啊 展开
3个回答
展开全部
调用Windows API试一下
[DllImport("user32.dll")]
internal static extern IntPtr GetSystemMenu(IntPtr hwnd,bool bRevert);
[DllImport("user32.dll")]
internal static extern int GetMenuItemCount(IntPtr hMenu);
[DllImport("user32.dll")]
internal static extern int RemoveMenu(IntPtr hMenu,int uPosition,int uFlags);
/// <summary>
/// 窗体的关闭按钮失效
/// </summary>
protected void CloseButtonEnable(){
// 默认窗口去除关闭按钮
const int MF_BYPOSITION = 0x00000400;
IntPtr hWindow = this.Handle;
IntPtr hMenu = GetSystemMenu(hWindow,false);
int count = GetMenuItemCount(hMenu);
RemoveMenu(hMenu,count - 1,MF_BYPOSITION);
RemoveMenu(hMenu,count - 2,MF_BYPOSITION);
}
[DllImport("user32.dll")]
internal static extern IntPtr GetSystemMenu(IntPtr hwnd,bool bRevert);
[DllImport("user32.dll")]
internal static extern int GetMenuItemCount(IntPtr hMenu);
[DllImport("user32.dll")]
internal static extern int RemoveMenu(IntPtr hMenu,int uPosition,int uFlags);
/// <summary>
/// 窗体的关闭按钮失效
/// </summary>
protected void CloseButtonEnable(){
// 默认窗口去除关闭按钮
const int MF_BYPOSITION = 0x00000400;
IntPtr hWindow = this.Handle;
IntPtr hMenu = GetSystemMenu(hWindow,false);
int count = GetMenuItemCount(hMenu);
RemoveMenu(hMenu,count - 1,MF_BYPOSITION);
RemoveMenu(hMenu,count - 2,MF_BYPOSITION);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System.Runtime.InteropServices;
public partial class Form1 : Form
{
[DllImport("USER32.DLL")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, UInt32 bRevert);
[DllImport("USER32.DLL")]
private static extern UInt32 RemoveMenu(IntPtr hMenu, UInt32 nPosition, UInt32 wFlags);
private const UInt32 SC_CLOSE = 0x0000F060;
private const UInt32 MF_BYCOMMAND = 0x00000000;
public Form1()
{
InitializeComponent();
IntPtr hMenu = GetSystemMenu(this.Handle, 0);
RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
}
public partial class Form1 : Form
{
[DllImport("USER32.DLL")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, UInt32 bRevert);
[DllImport("USER32.DLL")]
private static extern UInt32 RemoveMenu(IntPtr hMenu, UInt32 nPosition, UInt32 wFlags);
private const UInt32 SC_CLOSE = 0x0000F060;
private const UInt32 MF_BYCOMMAND = 0x00000000;
public Form1()
{
InitializeComponent();
IntPtr hMenu = GetSystemMenu(this.Handle, 0);
RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
能用属性解决为何写这么多代码?设置controlbox为false就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询