1

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

在此处输入图像描述

在 MSDN 中搜索这方面的信息我看到了DestroyMenuAPI 函数: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

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

在此处输入图像描述

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

4

2 回答 2

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
于 2013-11-16T18:52:26.443 回答
2

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

这使菜单完全消失:

在此处输入图像描述

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

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
于 2013-11-12T03:20:13.943 回答