如何取消打开应用程序的系统菜单?

时间:2013-11-11 23:46:20

标签: .net vb.net winforms menu menuitem

当用户点击它时,我需要一种隐藏或取消打开系统菜单的方法(完全禁用菜单是我的最后手段,我不敢隐藏它)

enter image description here

在MSDN中搜索有关此内容的信息我看过DestroyMenu API函数:http://msdn.microsoft.com/en-us/library/windows/desktop/ms647631%28v=vs.85%29.aspx

<DllImport("user32.dll")>
Private Shared Function DestroyMenu(
    ByVal hMenu As IntPtr)
End Function

但是当我尝试它时,应用程序的系统菜单会松开“样式”并且不会被“破坏”(删除)。

enter image description here

无论如何我会记住,我想要做的是隐藏菜单或避免菜单打开而不是完全禁用它。

2 个答案:

答案 0 :(得分:2)

在表单设计器中,将Form.ControlBox属性设置为false。

这使菜单完全消失:

enter image description here

您可以在代码中禁用不同的元素,但按钮似乎与菜单相关联,因此在不停止等效按钮工作的情况下似乎无法摆脱菜单:

Public Class Form1

    Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Integer, ByVal revert As Integer) As Integer
    Private Declare Function EnableMenuItem Lib "user32" (ByVal menu As Integer, ByVal ideEnableItem As Integer, ByVal enable As Integer) As Integer
    Private Declare Function RemoveMenu Lib "user32" Alias "RemoveMenu" (ByVal hMenu As IntPtr, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer

    Private Const SC_CLOSE = &HF060&        'Closes the window
    Private Const MF_BYCOMMAND = &H0&       'Specifies that the parameter gives the command ID of the existing menu item
    Private Const SC_SIZE = &HF000&         'Sizes the window
    Private Const SC_MOVE = &HF010&         'Move the window
    Private Const SC_MINIMIZE = &HF020&     'Minimizes Window
    Private Const SC_MAXIMIZE = &HF030&     'Maximizes window
    Private Const SC_RESTORE = &HF120&      'Restores window
    Private Const MF_SEPARATOR = &H800&     ' Menu Separator

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        EnableMenuItem(GetSystemMenu(Me.Handle.ToInt32, 0), SC_CLOSE, MF_BYCOMMAND)

        RemoveMenu(GetSystemMenu(Me.Handle, False), 0, MF_SEPARATOR)
        RemoveMenu(GetSystemMenu(Me.Handle, False), SC_CLOSE, MF_BYCOMMAND)
        RemoveMenu(GetSystemMenu(Me.Handle, False), SC_SIZE, MF_BYCOMMAND)
        RemoveMenu(GetSystemMenu(Me.Handle, False), SC_MOVE, MF_BYCOMMAND)
        RemoveMenu(GetSystemMenu(Me.Handle, False), SC_MINIMIZE, MF_BYCOMMAND)
        RemoveMenu(GetSystemMenu(Me.Handle, False), SC_MAXIMIZE, MF_BYCOMMAND)
        RemoveMenu(GetSystemMenu(Me.Handle, False), SC_RESTORE, MF_BYCOMMAND)
    End Sub
End Class

答案 1 :(得分:2)

您可以禁用左键单击图标,然后右键单击标题栏中的任意位置。

Dim WM_SYSCOMMAND As Integer = &H112
Dim WM_NCRBUTTONDOWN As Integer = &HA4
Dim WM_NCRBUTTONUP As Integer = &HA5

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

    'disables right clicking on title bar
    If m.Msg = WM_NCRBUTTONDOWN Then
        m.Result = IntPtr.Zero
        Return
    End If
    If m.Msg = WM_NCRBUTTONUP Then
        m.Result = IntPtr.Zero
        Return
    End If

    'disables left click on icon in title bar
    If m.Msg = WM_SYSCOMMAND Then
        If m.WParam = &HF093 Then
            m.Result = IntPtr.Zero
            Return
        End If
    End If

    MyBase.WndProc(m)
End Sub