VB实现的一种方法,楼主参考Private Declare Function GetSystemMenu Lib "User32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long Private Declare Function RemoveMenu Lib "User32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long Private Declare Function DrawMenuBar Lib "User32" (ByVal hwnd As Long) As Long Private Declare Function GetMenuItemCount Lib "User32" (ByVal hMenu As Long) As Long Private Const MF_BYPOSITION = &H400& Private Const MF_DISABLED = &H2&Private Sub Form_Load() Call DisableX(Me) End SubPrivate Sub DisableX(Frm As Form) Dim hMenu As Long, nCount As Long hMenu = GetSystemMenu(Frm.hwnd, 0) nCount = GetMenuItemCount(hMenu) Call RemoveMenu(hMenu, nCount - 1, MF_DISABLED Or MF_BYPOSITION) DrawMenuBar Frm.hwnd End Sub
變灰:
EnableMenuItem(GetSystemMenu(Form1->Handle,False),SC_CLOSE,MF_BYCOMMAND|MF_GRAYED);恢復:
EnableMenuItem(GetSystemMenu(Form1->Handle,False),SC_CLOSE,MF_BYCOMMAND|MF_ENABLED);
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);
}
////////////////////////////////////////////////////////////
重写这个方法即不变灰,关闭也无效
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if(m.Msg==0x0010)
{
return;
}
base.WndProc(ref m);
}
Private Declare Function RemoveMenu Lib "User32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Private Declare Function DrawMenuBar Lib "User32" (ByVal hwnd As Long) As Long
Private Declare Function GetMenuItemCount Lib "User32" (ByVal hMenu As Long) As Long
Private Const MF_BYPOSITION = &H400&
Private Const MF_DISABLED = &H2&Private Sub Form_Load()
Call DisableX(Me)
End SubPrivate Sub DisableX(Frm As Form)
Dim hMenu As Long, nCount As Long
hMenu = GetSystemMenu(Frm.hwnd, 0)
nCount = GetMenuItemCount(hMenu)
Call RemoveMenu(hMenu, nCount - 1, MF_DISABLED Or MF_BYPOSITION)
DrawMenuBar Frm.hwnd
End Sub
中的DllImport不识别怎么办 ???
System.Runtime.InteropServices