2

マウスクリックで右クリックと貼り付けオプションをキャプチャする方法を知りたいです。これは、winforms アプリケーションです。貼り付ける前にクリップボードの内容を変更します。ctrl+V でこれを実行できますが、マウスの右クリックを処理する方法が見つかりません。

私はこれまでにこれを試しました:

   Private Const WM_PASTE As Integer = &H302
   Protected Overrides Sub WndProc(ByRef msg As Message)
        If msg.Msg = WM_PASTE AndAlso Clipboard.ContainsText() Then
            Clipboard.SetText(Clipboard.GetText().Replace(vbCrLf, " "))
        End If
        MyBase.WndProc(msg)
    End Sub
4

2 に答える 2

2

WM_PASTEを使用して Windows メッセージを処理する必要がありますWndProc(すべてのメッセージのリストはここにあります)。

たとえば、これは、TextBox貼り付けられたすべてのテキストを(方法に関係なく)それ自体を表示する代わりにコンソールに出力します。

Class CapturePasteBox
    Inherits TextBox

    Protected Overrides Sub WndProc(ByRef m As Message)
        If m.Msg = &H302 AndAlso Clipboard.ContainsText() Then
            Dim text = Clipboard.GetText()
            '' do something with text
            Console.WriteLine(text)
            Return '' return so the text won't be pasted into the TextBox
        End If
        MyBase.WndProc(m)
    End Sub
End Class

あなたのコメントに応えて:

ComboBox-control には特別な処理が必要です。

コンボ ボックスに送信されると、WM_PASTE メッセージはそのエディット コントロールによって処理されます。

したがって、を使用して次の関数/クラスを使用できますNativeWindow

<System.Runtime.InteropServices.DllImport("user32.dll", SetLastError := True)> _
Private Shared Function FindWindowEx(hwndParent As IntPtr, hwndChildAfter As IntPtr, lpszClass As String, lpszWindow As String) As IntPtr
End Function

Public Class PasteHandler
Inherits NativeWindow
    Protected Overrides Sub WndProc(ByRef m As Message)
    If m.Msg = &H302 Then
        Clipboard.SetText(ClipBoard.GetText().Replace("e", "XXX"))
    End If

    MyBase.WndProc(m)
    End Sub
End Class

そしてあなたと一緒にそれを使用してくださいComboBox

'' Get the edit control of the combobox
Dim lhWnd As IntPtr = FindWindowEx(yourComboBox.Handle, IntPtr.Zero, "EDIT", Nothing)

'' assign the edit control to the Pastehandler
Dim p As New PasteHandler()
p.AssignHandle(lhWnd)
于 2013-07-31T08:24:03.043 に答える
0

私はこれが素晴らしいように機能することを発見しました:

    <System.Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True)> _
    Private Shared Function FindWindowEx(hwndParent As IntPtr, hwndChildAfter As IntPtr, lpszClass As String, lpszWindow As String) As IntPtr
    End Function

    Private Sub SearchCriteria_MouseDown(sender As Object, e As MouseEventArgs) Handles SearchCriteria.MouseDown
        Dim lhWnd As IntPtr = FindWindowEx(SearchCriteria.Handle, IntPtr.Zero, "EDIT", Nothing)
        If e.Button = Windows.Forms.MouseButtons.Right And lhWnd <> 0 Then
            Clipboard.SetText(Clipboard.GetText().Replace(vbCrLf, " "))
        End If
    End Sub
于 2013-08-02T08:31:27.950 に答える