How I could cancel opening the system menu of an a

2019-06-03 18:03发布

问题:

I need a way to hide or cancel opening the system menu when the user makes click on it (completely disable the menu is my last resort, I preffer to hide it)

Searching info about this in MSDN I've seen the DestroyMenu API function: 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

But when I try it the system menu of the application loose the "Style" and is not "destroyed" (deleted).

Anyways I will keep in mind that what I would like to do is hide the menu or avoid the menu openning instead of completely disable it.

回答1:

You can disable left clicking the icon, and right clicking anywhere in the title bar.

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


回答2:

In your form designer, set the Form.ControlBox property to false.

This makes the menu disappear completely:

You can disable the different elements in code but the buttons seem to be tied to the menu so it doesn't seem possible to get rid of the menu without stopping the equivalent button working:

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