Option Explicit
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As LongConst MF_DISABLED = &H2&
Const MF_GRAYED = &H1&
Const MF_ENABLED = &H0&Private Sub Command1_Click()
    Dim hMenu As Long
    Dim ret As Long
    hMenu = GetSystemMenu(Me.hwnd, False) '取得系统菜单句柄
    Debug.Print hMenu
    ret = GetMenuItemCount(hMenu) '取得系统菜单的菜单项个数
    Debug.Print ret   '返回7,正常
    ret = EnableMenuItem(hMenu, 1, MF_GRAYED) '使第二菜单项变灰
    Debug.Print ret   '返回-1,失败
End Sub

解决方案 »

  1.   

    关注!
    刚才可能CSDN出毛病了!我的也是!
      

  2.   

    EnableMenuItem
    The EnableMenuItem function enables, disables, or grays the specified menu item. BOOL EnableMenuItem(
      HMENU hMenu,         // handle to menu
      UINT uIDEnableItem,  // menu item to enable, disable, or gray
      UINT uEnable         // menu item flags
    );
     
    Parameters
    hMenu 
    Handle to the menu. 
    uIDEnableItem 
    Specifies the menu item to be enabled, disabled, or grayed, as determined by the uEnable parameter. This parameter specifies an item in a menu bar, menu, or submenu. 
    uEnable 
    Specifies flags that control the interpretation of the uIDEnableItem parameter and indicate whether the menu item is enabled, disabled, or grayed. This parameter must be a combination of either MF_BYCOMMAND or MF_BYPOSITION and MF_ENABLED, MF_DISABLED, or MF_GRAYED. Value Meaning 
    MF_BYCOMMAND Indicates that uIDEnableItem gives the identifier of the menu item. If neither the MF_BYCOMMAND nor MF_BYPOSITION flag is specified, the MF_BYCOMMAND flag is the default flag. 
    MF_BYPOSITION Indicates that uIDEnableItem gives the zero-based relative position of the menu item. 
    MF_DISABLED Indicates that the menu item is disabled, but not grayed, so it cannot be selected. 
    MF_ENABLED Indicates that the menu item is enabled and restored from a grayed state so that it can be selected. 
    MF_GRAYED Indicates that the menu item is disabled and grayed so that it cannot be selected. 
    Return Values
    The return value specifies the previous state of the menu item (it is either MF_DISABLED, MF_ENABLED, or MF_GRAYED). If the menu item does not exist, the return value is 0xFFFFFFFF.
      

  3.   

    拜托,这段东西是MSDN里的,我要是能看出结果来得话,我也不会来麻烦大家了
      

  4.   

    这是别人的一段代码:不知道我会不会因此而犯法?'-------------------------------------------
    '           使窗体右上角的X按钮失效
    '-------------------------------------------
    '     趣趣vb  http://hhd85.8u8.com
    '-------------------------------------------
    '程序说明:
    '本例利用API函数GetSystemMenu得到系统菜单的句柄
    'X按钮是系统菜单的一菜单项,然后用RemoveMenu函数
    '删去这一菜单项,也就是使X按钮失效了。
    '-------------------------------------------
    '【VB声明】
    '  Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long'【说明】
    '  取得指定窗口的系统菜单的句柄。在vb环境,“系统菜单”的正式名称为“控制菜单”,即单击窗口左上角的控制框时出现的菜单'【返回值】
    '  Long,如执行成功,返回系统菜单的句柄;零意味着出错。如bRevert设为TRUE,也会返回零(简单的恢复原始的系统菜单)'【备注】
    '  在vb里使用:系统菜单会向窗口发送一条WM_SYSCOMMAND消息,而不是WM_COMMAND消息'【参数表】
    '  hwnd -----------  Long,窗口的句柄'  bRevert --------  Long,如设为TRUE,表示接收原始的系统菜单Private Declare Function GetSystemMenu Lib "user32" ( _
    ByVal hwnd As Integer, _
    ByVal bRevert As Integer _
                                                        ) As Integer
    '-------------------------------------------
    '【VB声明】
    '  Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long'【说明】
    '  删除指定的菜单条目。如删除的条目属于一个弹出式菜单,那么这个函数不会同时删除弹出式菜单。首先应该用GetSubMenu函数取得弹出式菜单的句柄,再在以后将其删除'【返回值】
    '  Long,非零表示成功,零表示失败。会设置GetLastError'【备注】
    '  强烈建议大家使用vb菜单的visible属性从菜单中删除条目,而不要用这个函数,否则会造成指定菜单中其他菜单条目的visible属性对错误的菜单条目产生影响'【参数表】
    '  hMenu ----------  Long,菜单的句柄'  nPosition ------  Long,欲改变的菜单条目的标识符。如在wFlags参数中指定了MF_BYCOMMAND,这个参数就代表欲改变的菜单条目的命令ID。如设置的是MF_BYPOSITION,这个参数就代表菜单条目在菜单中的位置(第一个条目的位置为零)'  wFlags ---------  Long,常数MF_BYCOMMAND或MF_BYPOSITION,取决于nPosition参数
    Private Declare Function RemoveMenu Lib "user32" ( _
    ByVal hMenu As Integer, _
    ByVal nPosition As Integer, _
    ByVal wFlags As Integer _
                                             ) As Integer
    Const MF_BYCOMMAND = &H0&
    Const MF_BYPOSITION = &H400&Private Sub Command1_Click()
    Unload Me
    End SubPrivate Sub Form_Load()
     MyMenu = GetSystemMenu(Me.hwnd, 0)
     RemoveMenu MyMenu, &HF060, MF_BYCOMMAND
    End Sub
      

  5.   

    我觉得这样做的方法还是存在一些问题,不过还是可行的,只是依然可以用ALT+F4关闭,就谢谢了