vb.net 2 个解决方案之间的按键事件 (Visual Studio 2010)
本文关键字:事件 Visual Studio 2010 net 解决方案 之间 vb | 更新日期: 2023-09-27 18:31:10
我是Vb的新手,请指导我以下事项:
来自不同解决方案的2种不同形式(例如:解决方案A''形式1和解决方案B''形式2)。
这两种窗体都有多个文本框。
首先,我们在解决方案A''Form1的文本框中输入数据。然后,通过使用 KeyPress 事件,光标移动到解决方案 B''Form2 中的文本框。
之后,通过使用解决方案B''Form2中的按键事件,光标将移回解决方案A''Form1中的另一个文本框。
可以这样打点吗?如果可能的话,请让我知道如何编码。我正在使用Visual Studio 2010。谢谢。
我创建了一个测试表单(表单 1),其中包含一个名为 txtPart 的文本框。
点击"输入"按钮后,我希望将txtPart中的数据传输到记事本。如何从这里调用类记事本发送和接收?
Public Class Form1
Private Sub txtPart_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPart.KeyPress
Try
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
' How to call NotepadSendAndReceive class here?
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
End Class
请帮忙。谢谢。
==================================================================================================================================================================================================================================================
我创建了一个测试表单(表单 1),其中包含一个名为 txtPart 的文本框。
点击"输入"按钮后,我希望将txtPart中的数据传输到记事本。如何从这里调用类记事本发送和接收?公开课表格1
Private Sub txtPart_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPart.KeyPress
Try
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
' How to call NotepadSendAndReceive class here?
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
结束类
请帮忙。谢谢。
这可以
通过使用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")
你只需要稍微定制一下,你只需要以找出文本框的当前句柄。
看看 :
If Pzs.Count > 0 Then Return FindWindowEx(FindWindow("Notepad", Pzs(0)), IntPtr.Zero, "Edit", IntPtr.Zero)
该程序使用"FindWindowEx"查找"记事本"的当前句柄,然后查找"编辑"元素的子句柄。
然后,您拥有记事本编辑区域的正确当前句柄,例如可以向其发送文本。
在您的情况下,它将是"Form2",然后您必须知道文本框的命名方式(这可以通过使用WinSpy++找到)。
此致敬意。