【问题标题】:vb.net keypress event between 2 solutions (visual studio 2010)2 个解决方案之间的 vb.net 按键事件(Visual Studio 2010)
【发布时间】:2016-01-20 08:50:53
【问题描述】:

我是 Vb 新手,请指导我以下问题:

不同的解决方案有2种不同的形式(例如:SolutionA\Form1和SolutionB\Form2)。

两种形式都有多个文本框。

首先我们在SolutionA\Form1 的文本框中输入数据。然后,通过 KeyPress 事件,光标移动到 SolutionB\Form2 中的文本框。

之后,通过使用SolutionB\Form2 中的Keypress 事件,光标移回SolutionA\Form1 中的另一个文本框。

可以这样打点吗?如果可能,请让我知道如何进行编码。我正在使用 Visual Studio 2010。谢谢。


我创建了一个测试表单(表单 1),其中包含一个名为 txtPart 的文本框。

点击“Enter”按钮后,我希望将 txtPart 中的数据传输到记事本。 如何从这里调用 NotepadSendAndReceive 类?

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 的文本框。

点击“Enter”按钮后,我希望将 txtPart 中的数据传输到记事本。如何从这里调用类 NotepadSendAndReceive? 公开课形式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

结束类

请帮忙。谢谢。

【问题讨论】:

  • 您的 KeyPress 事件中有 Form2.TextBox1.Focus() 吗?
  • 不,因为我不确定如何将 Focus() 放在不同的解决方案中。但是,如果在同一解决方案中以两种形式使用,我总是使用这种方式。谢谢。

标签: c# vb.net winforms visual-studio-2010


【解决方案1】:

这可以通过使用 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++ 找到)。

最好的问候。

【讨论】:

  • 感谢 SiriSch 先生!我将研究这种方法并尝试进行编码。我在许多论坛中搜索过,但以前从未找到过这个想法。再次感谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多