這是通過使用NamedPipeServer(高複雜性)或 以其他方式使用WINAPI可能(我會推薦這)。
看一看這樣的:
Public Class NotepadSendAndReceive
Public Const VK_RETURN As UInteger = &HD
Public Const WM_SETFOCUS As UInteger = &H7
Public Const WM_KILLFOCUS As UInteger = &H8
Public Const WM_CHAR As UInteger = &H102
Public Const WM_SETTEXT As UInteger = &HC
Public Const WM_KEYDOWN As UInteger = &H100
Public Const WM_KEYUP As UInteger = &H101
Public Const cap As Integer = 1048576
Public Const WM_CLICK As UInteger = &HF5
Public Const WM_GETTEXT As UInteger = &HD
Public Const WM_GETTEXTLENGTH As UInteger = &HE
<DllImport("User32.Dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function GetWindowTextLength(hWnd As IntPtr) As Integer
End Function
<DllImport("User32.Dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function GetWindowText(hWnd As IntPtr, lpString As StringBuilder, nMaxCount As Integer) As Integer
End Function
<DllImport("User32.Dll")> Private Shared Function SendMessage(hWnd As IntPtr, Msg As UInt32, wParam As IntPtr, lParam As String) As IntPtr
End Function
<DllImport("User32.Dll")> Private Shared Function SendMessage(hWnd As IntPtr, Msg As Integer, wParam As Integer, lParam As StringBuilder) As Integer
End Function
<DllImport("User32.Dll")> Public Shared Function PostMessage(hWnd As Integer, msg As UInteger, wParam As UInteger, lParam As Integer) As IntPtr
End Function
<DllImport("User32.Dll")> Public Shared Function PostMessage(hWnd As Integer, msg As UInteger, wParam As UInteger, lParam As UInteger) As IntPtr
End Function
<DllImport("User32.Dll")> Private Shared Function FindWindow(lpClassName As String, lpWindowName As String) As IntPtr
End Function
<DllImport("User32.Dll")> Private Shared Function FindWindowEx(parentHandle As IntPtr, childAfter As IntPtr, className As String, windowTitle As IntPtr) As IntPtr
End Function
Private Shared Function GetStringOfEditor() As String
Return GetText(GetProcNumber("Editor"))
End Function
Public Shared Sub SendStringToEditor(T As String)
SendMessage(GetProcNumber("Editor"), &HC, CType(cap, IntPtr), T)
End Sub
Public Shared Sub SendEnter(Hwndd As IntPtr)
SendMessage(Hwndd, WM_SETFOCUS, CType(cap, IntPtr), String.Empty)
Threading.Thread.Sleep(10)
PostMessage(Hwndd.ToInt32(), WM_KEYDOWN, &HD, &H1C0001)
PostMessage(Hwndd.ToInt32(), WM_CHAR, &HD, &H1C0001)
Threading.Thread.Sleep(10)
PostMessage(Hwndd.ToInt32(), WM_KEYUP, &HD, &HC01C0001UI)
SendMessage(Hwndd, WM_KILLFOCUS, CType(cap, IntPtr), String.Empty)
End Sub
Private Shared Function GetProcNumber(pTitle As String) As IntPtr
Dim Pzs As New List(Of String)
For Each p As Process In Process.GetProcesses
If p.MainWindowTitle.Contains(pTitle) Then Pzs.Add(p.MainWindowTitle)
Next
If Pzs.Count > 0 Then Return FindWindowEx(FindWindow("Notepad", Pzs(0)), IntPtr.Zero, "Edit", IntPtr.Zero)
End Function
Private Shared Function GetText(hWnd As IntPtr) As String
Dim nLen As Integer = CInt(SendMessage(hWnd, WM_GETTEXTLENGTH, IntPtr.Zero, String.Empty))
Dim pText As New StringBuilder(nLen + 1)
SendMessage(hWnd, WM_GETTEXT, nLen + 1, pText)
Return pText.ToString()
End Function
End Class
調用示例(記事本必須打開)
NotepadSendAndReceive.SendStringToEditor("This is a test")
你只需要自定義此一點點,你就必須 找出您的文本框的當前句柄。通過使用「FindWindowEx」
If Pzs.Count > 0 Then Return FindWindowEx(FindWindow("Notepad", Pzs(0)), IntPtr.Zero, "Edit", IntPtr.Zero)
該方案旨在爲「記事本」,然後在「編輯」元素的子把手當前手柄:
看看。
然後你有正確的當前句柄爲您的記事本編輯區域,你可以例如發送文本到它。
在你的情況下,它將是「Form2」,然後你必須知道你的文本框是如何命名的(這可以通過使用WinSpy ++找到)。
此致敬禮。
你的KeyPress事件中是否有'Form2.TextBox1.Focus()'? – GrindelOh
不,因爲我不確定如何將Focus()形成爲不同的解決方案。但是,如果以兩種形式使用相同的解決方案,我總是以這種方式使用。謝謝。 – orenmango