在Windows默认上下文菜单中捕获用户选择的选项?

时间:2013-10-21 06:34:46

标签: .net vb.net winforms textbox contextmenu

右键单击具有默认Windows上下文菜单的文本框时,我想知道用户是否选择copy cutpaste选项,以便在用户选择时执行辅助操作特定的上下文选项。

我没有代码'因为我不知道从哪里开始尝试识别用户在上下文菜单中选择了什么选项,以及如何捕获左键单击'因为我试图捕获默认的contextmenu mouseleft点击文本框MouseDown/Mouseclick事件没有成功,我知道这没什么意义'因为它是一个上下文鼠标点击,而不是文本框鼠标点击,但是好......我不知道如何管理外部上下文菜单。

2 个答案:

答案 0 :(得分:3)

您可以在项目中添加这样的类:

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命令似乎也会导致复制命令事件,但我现在假设你可以处理那种轻微的复杂情况。

答案 1 :(得分:0)

如果有人需要这个,这是对@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