恢复编辑控件的默认上下文菜单的正确方法?
Proper way to restore a default contextmenu for an edit control?
我处于一种情况,我想决定是禁用还是启用子类 TextBox
控件的默认编辑上下文菜单,我这样写:
Public Property DisableEditMenu As Boolean = False
''' <summary>
''' Raises the <see cref="E:System.Windows.Forms.Control.Enter"/> event.
''' </summary>
''' <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
Protected Overrides Sub OnEnter(e As EventArgs)
If Me.DisableEditMenu AndAlso MyBase.ContextMenuStrip Is Nothing Then
' Disable Copy/Cut/Paste contextmenu by creating a new empty one.
MyBase.ContextMenuStrip = New ContextMenuStrip
ElseIf Not Me.DisableEditMenu Then
' Restore default edit contextmenu...
End If
MyBase.OnEnter(e)
End Sub
问题是...如何恢复默认上下文菜单?
我考虑过用这种方式解决它,它工作正常,但我不确定我是否有效率或者我做了不必要的事情而不是我真正应该做的来恢复默认菜单:
...
ElseIf Not Me.DisableEditMenu Then
' Restore default edit contextmenu...
Using tb As New TextBox
MyBase.ContextMenuStrip = tb.ContextMenuStrip
End Using
...
因为.Net Framework Class库提供了默认值的参考,例如默认控件颜色(eg:TextBox.DefaultBackColor
),那么也许默认编辑上下文菜单是 contained/referred 在一些我还没有发现的 Type
中?或者有效地恢复它比我想象的更难?
到 "restore the default contextmenu",将 ContextMenuStrip
属性 设置为空。
Me.ContextMenuStrip = Nothing
但是,无需将 ContextMenu
设置为新的空实例。您所要做的就是抑制 WM_CONTEXTMENU
消息。 MSDN 将此消息描述为:
Notifies a window that the user clicked the right mouse button (right-clicked) in the window.
Public Class UITextBox
Inherits TextBox
Public Property DisableEditMenu As Boolean
Protected Overrides Sub WndProc(ByRef m As Message)
If ((m.Msg = WM_CONTEXTMENU) AndAlso Me.DisableEditMenu) Then
Exit Sub
Else
MyBase.WndProc(m)
End If
End Sub
Private Const WM_CONTEXTMENU As Integer = 123
End Class
我处于一种情况,我想决定是禁用还是启用子类 TextBox
控件的默认编辑上下文菜单,我这样写:
Public Property DisableEditMenu As Boolean = False
''' <summary>
''' Raises the <see cref="E:System.Windows.Forms.Control.Enter"/> event.
''' </summary>
''' <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
Protected Overrides Sub OnEnter(e As EventArgs)
If Me.DisableEditMenu AndAlso MyBase.ContextMenuStrip Is Nothing Then
' Disable Copy/Cut/Paste contextmenu by creating a new empty one.
MyBase.ContextMenuStrip = New ContextMenuStrip
ElseIf Not Me.DisableEditMenu Then
' Restore default edit contextmenu...
End If
MyBase.OnEnter(e)
End Sub
问题是...如何恢复默认上下文菜单?
我考虑过用这种方式解决它,它工作正常,但我不确定我是否有效率或者我做了不必要的事情而不是我真正应该做的来恢复默认菜单:
...
ElseIf Not Me.DisableEditMenu Then
' Restore default edit contextmenu...
Using tb As New TextBox
MyBase.ContextMenuStrip = tb.ContextMenuStrip
End Using
...
因为.Net Framework Class库提供了默认值的参考,例如默认控件颜色(eg:TextBox.DefaultBackColor
),那么也许默认编辑上下文菜单是 contained/referred 在一些我还没有发现的 Type
中?或者有效地恢复它比我想象的更难?
到 "restore the default contextmenu",将 ContextMenuStrip
属性 设置为空。
Me.ContextMenuStrip = Nothing
但是,无需将 ContextMenu
设置为新的空实例。您所要做的就是抑制 WM_CONTEXTMENU
消息。 MSDN 将此消息描述为:
Notifies a window that the user clicked the right mouse button (right-clicked) in the window.
Public Class UITextBox
Inherits TextBox
Public Property DisableEditMenu As Boolean
Protected Overrides Sub WndProc(ByRef m As Message)
If ((m.Msg = WM_CONTEXTMENU) AndAlso Me.DisableEditMenu) Then
Exit Sub
Else
MyBase.WndProc(m)
End If
End Sub
Private Const WM_CONTEXTMENU As Integer = 123
End Class