0

デフォルトのWindowsコンテキストメニューを持つテキストボックスを右クリックすると、ユーザーが特定のコンテキストメニューオプションを選択したときに二次操作を実行するために、ユーザーがオプションを選択したcopy cutかどうかを知りたいです。paste

コンテキストメニューでユーザーが選択したオプションを認識しようとする場所と、その左クリックをキャプチャする方法がわからないため、コードがありません。デフォルトのコンテキストメニューマウスをキャプチャしようとしたためです。テキストボックスMouseDown/Mouseclickのイベントが成功しない場合、テキストボックスのマウスクリックではなく、コンテキストメニューのマウスクリックであるため、あまり意味がないことはわかっていますが、その外部コンテキストメニューを管理する方法がわかりません。

4

2 に答える 2

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

次に、Designer.vb ファイル内のすべての "TextBox" を "MyTextBox" に置き換えることができます。次に、カット、コピー、ペーストの 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 つのイベントすべてを 1 つの関数で処理することを選択したことに注意してください。ただし、それらを別の関数で処理することもできます。切り取りコマンドはコピー コマンド イベントも発生させるように見えますが、ここでは、そのわずかな複雑さに対処できると仮定します。

于 2013-10-23T19:01:19.910 に答える
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
于 2013-10-24T11:13:32.143 に答える