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

回应您的评论:

-controlComboBox需要一些特殊处理,因为

当发送到组合框时,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 回答