在Windows默认上下文菜单中捕获用户选择的选项?
右键单击具有默认Windows上下文菜单的文本框时,我想知道用户是否选择 copy
cut
或粘贴
选项,以在用户选择特定的上下文菜单选项时执行辅助操作。
When right-click on a textbox which has the default Windows contextmenu I want to know if the user selects copy
cut
or paste
option, to perform secondary operations when user has selected an specific contextmenu option.
我没有代码因为我不知道从哪里开始尝试识别上下文菜单中用户选择的选项,以及如何捕获该左键单击,因为我试图捕获默认的上下文菜单鼠标左键单击文本框 MouseDown / Mouseclick
事件没有成功,我知道这没有多大意义,因为它是上下文菜单mouseclick,而不是Textbox mouseclick,但是...我不知道如何管理该外部上下文菜单。
I have no code 'cause I don't know where to start trying to recognize what option was selected by the user in the contextmenu, and how to capture that left click 'cause I've tried to capture the default contextmenu mouseleft click on the textbox MouseDown/Mouseclick
events without success, I know that has not much sense 'cause it is a contextmenu mouseclick, not a Textbox mouseclick, but well... I don't know how to manage that external contextmenu.
您可以将这样的类添加到您的项目中:
You can add a class like this to your project:
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
然后,您可以替换所有出现的 TextBox在带有MyTextBox的Designer.vb文件中。然后,您将可以访问3个有关剪切,复制和粘贴的新事件。您可以像这样处理它们:
Then you can replace all occurrences of "TextBox" in the Designer.vb files with "MyTextBox". Then you will have access to 3 new events for Cut, Copy and Paste. You can handle them like this:
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命令似乎也引起了复制命令事件,但是我现在假设您可以处理这种轻微的复杂情况。
Notice how I've chosen to handle all 3 events in one function in this case, but you could handle them in separate functions as well. I noticed that the cut command seems to also cause a copy command event, but I will assume for now that you can deal with that slight complication.