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)