当右键单击具有默认 Windows 上下文菜单的文本框时,我想知道用户是否选择copy
cut
或paste
选项,以便在用户选择特定的上下文菜单选项时执行辅助操作。
我没有代码,因为我不知道从哪里开始尝试识别用户在上下文菜单中选择了哪个选项,以及如何捕获左键单击,因为我试图捕获默认的上下文菜单鼠标左键单击文本框MouseDown/Mouseclick
事件没有成功,我知道这没有多大意义,因为它是上下文菜单鼠标单击,而不是文本框鼠标单击,但是...我不知道如何管理该外部上下文菜单。
当右键单击具有默认 Windows 上下文菜单的文本框时,我想知道用户是否选择copy
cut
或paste
选项,以便在用户选择特定的上下文菜单选项时执行辅助操作。
我没有代码,因为我不知道从哪里开始尝试识别用户在上下文菜单中选择了哪个选项,以及如何捕获左键单击,因为我试图捕获默认的上下文菜单鼠标左键单击文本框MouseDown/Mouseclick
事件没有成功,我知道这没有多大意义,因为它是上下文菜单鼠标单击,而不是文本框鼠标单击,但是...我不知道如何管理该外部上下文菜单。
您可以将这样的类添加到您的项目中:
Class MyTextBox : Inherits TextBox
Public Enum ContextCommands
WM_CUT = &H300
WM_COPY = &H301
WM_PASTE = &H302
End Enum
Public Class ContextCommandEventArgs
Inherits EventArgs
Public Property Command As ContextCommands
End Class
Event OnCut(sender As Object, e As ContextCommandEventArgs)
Event OnCopy(sender As Object, e As ContextCommandEventArgs)
Event OnPaste(sender As Object, e As ContextCommandEventArgs)
Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
Select Case m.Msg
Case ContextCommands.WM_CUT
RaiseEvent OnCut(Me, New ContextCommandEventArgs() With {.Command = ContextCommands.WM_CUT})
Case ContextCommands.WM_COPY
RaiseEvent OnCopy(Me, New ContextCommandEventArgs() With {.Command = ContextCommands.WM_COPY})
Case ContextCommands.WM_PASTE
RaiseEvent OnPaste(Me, New ContextCommandEventArgs() With {.Command = ContextCommands.WM_PASTE})
End Select
End Sub
End Class
然后,您可以用“MyTextBox”替换 Designer.vb 文件中所有出现的“TextBox”。然后,您将可以访问 3 个用于剪切、复制和粘贴的新事件。你可以像这样处理它们:
Private Sub TextBox1_OnTextCommand(sender As Object, e As MyTextBox.ContextCommandEventArgs) _
Handles TextBox1.OnCut, TextBox1.OnPaste, TextBox1.OnCopy
MessageBox.Show("Activated " & e.Command.ToString())
End Sub
请注意,在这种情况下,我如何选择在一个函数中处理所有 3 个事件,但您也可以在单独的函数中处理它们。我注意到 cut 命令似乎也会导致复制命令事件,但我现在假设您可以处理这种轻微的复杂情况。
如果有人需要这个,这是对@BlueMonkMN 代码的修改,以便与 CUT 选项一起正常工作,并且还添加了 DELETE 选项。
Class MyTextBox : Inherits TextBox
Private Last_Command As ContextCommands = Nothing
Private WithEvents CopyOrCut_Timer As New Timer _
With {.Interval = 5, .Enabled = False}
Public Enum ContextCommands
WM_CUT = &H300
WM_COPY = &H301
WM_PASTE = &H302
WM_DELETE = &H303
End Enum
Public Class ContextCommandEventArgs
Inherits EventArgs
Public Property Command As ContextCommands
End Class
Event OnCut(sender As Object, e As ContextCommandEventArgs)
Event OnCopy(sender As Object, e As ContextCommandEventArgs)
Event OnPaste(sender As Object, e As ContextCommandEventArgs)
Event OnDelete(sender As Object, e As ContextCommandEventArgs)
Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
Select Case m.Msg
Case ContextCommands.WM_COPY
Last_Command = ContextCommands.WM_COPY
CopyOrCut_Timer.Enabled = True
Case ContextCommands.WM_CUT
Last_Command = ContextCommands.WM_CUT
Case ContextCommands.WM_PASTE
RaiseEvent OnPaste(Me, New ContextCommandEventArgs() _
With {.Command = ContextCommands.WM_PASTE})
Case ContextCommands.WM_DELETE
RaiseEvent OnDelete(Me, New ContextCommandEventArgs() _
With {.Command = ContextCommands.WM_DELETE})
End Select
End Sub
Private Sub Cut_Timer_Tick(sender As Object, e As EventArgs) _
Handles CopyOrCut_Timer.Tick
sender.enabled = False
Select Case Last_Command
Case ContextCommands.WM_COPY
RaiseEvent OnCopy(Me, New ContextCommandEventArgs() _
With {.Command = ContextCommands.WM_COPY})
Case ContextCommands.WM_CUT
RaiseEvent OnCut(Me, New ContextCommandEventArgs() _
With {.Command = ContextCommands.WM_CUT})
End Select
Last_Command = Nothing
End Sub
End Class