关于Winform中Disabled“X”按钮的问题

rqx110 2009-12-10 08:55:23
今天在CodeProject上看到一篇文章关于Winform中Disabled掉“X”按钮的文章,以前在网上所能找到的解决方法都是使用API来实现,在此文章中看到一种.net方式实现的方法,特共享一下:
API方法:
1. Add this using Statement in the header :
using System.Runtime.InteropServices;


2. In the class add :

private 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);



3. Finally to disable the close button during runtime Use :

IntPtr hMenu = GetSystemMenu(this.Handle, false);
int menuItemCount = GetMenuItemCount(hMenu);
RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION);


C#实现:

#region "Disable the 'X'"
protected override CreateParams CreateParams
{
get {
CreateParams cp = base.CreateParams;
const int CS_NOCLOSE = 0x200;
cp.ClassStyle = cp.ClassStyle | CS_NOCLOSE;
return cp;
}
}
#endregion


VB.NET实现:

#Region "Disable the 'X'"
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
Const CS_NOCLOSE As Integer = &H200
cp.ClassStyle = cp.ClassStyle Or CS_NOCLOSE
Return cp
End Get
End Property
#End Region


原文地址:http://www.codeproject.com/tips/46722/Disable-Close-Button-from-Title-bar-of-a-Window.aspx
...全文
133 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
rqx110 2009-12-22
  • 打赏
  • 举报
回复
#region "Disable the 'X'"
protected override CreateParams CreateParams
{
get {
CreateParams cp = base.CreateParams;
const int CS_NOCLOSE = 0x200;
cp.ClassStyle = cp.ClassStyle | CS_NOCLOSE;
return cp;
}
}
#endregion


使用这个 ALT+F4无效啊!
outou 2009-12-14
  • 打赏
  • 举报
回复
但LZ的方法可象不能适用于ALT + F4的方法。下面是X和ALT + F4不能使用的方法。代码来自http://bingning.net/free/source/form/disabledclosebutton.html

[VB.NET]

Protected Overrides Sub WndProc( _
ByRef m As System.Windows.Forms.Message)
Const WM_SYSCOMMAND As Integer = &H112
Const SC_CLOSE As Integer = &HF060

If m.Msg = WM_SYSCOMMAND And m.WParam.ToInt32() = SC_CLOSE Then
Return
End If

MyBase.WndProc(m)
End Sub



[C#]

protected override void WndProc(ref Message m)
{
const int WM_SYSCOMMAND = 0x112;
const int SC_CLOSE = 0xF060;

if (m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_CLOSE)
{
return;
}

base.WndProc (ref m);
}
云水千寻 2009-12-12
  • 打赏
  • 举报
回复
帮顶啦
gc_lys 2009-12-10
  • 打赏
  • 举报
回复
怎么Disable掉网页模态窗口的X 貌似怎么都不好使

17,740

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧