【问题标题】:Get Text From Specific Textboxes From External Application - Visual Basic .Net从外部应用程序的特定文本框中获取文本 - Visual Basic .Net
【发布时间】:2012-12-14 22:57:31
【问题描述】:

我可以从外部应用程序文本框中获取文本,但现在我想从外部应用程序的所需文本框中获取文本。 我的英语不太好,所以请看下图。

以下代码仅返回第一个文本框值。

Imports System.Runtime.InteropServices


Public Class Form1

Private Const WM_GETTEXT As Integer = &HD
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, _
ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, _
                                 ByVal childAfter As IntPtr, _
                                 ByVal lclassName As String, _
                                 ByVal windowTitle As String) As IntPtr
End Function

Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'Find the running notepad window
    Dim Hwnd As IntPtr = FindWindow(Nothing, TextBox1.Text)

    'Alloc memory for the buffer that recieves the text
    Dim Handle As IntPtr = Marshal.AllocHGlobal(100)

    'send WM_GWTTEXT message to the notepad window
    Dim NumText As Integer = SendMessage(Hwnd, WM_GETTEXT, 50, Handle)

    'copy the characters from the unmanaged memory to a managed string
    Dim Text As String = Marshal.PtrToStringUni(Handle)

    'Display the string using a label
    Label1.Text = Text

    'Find the Edit control of the Running Notepad
    Dim ChildHandle As IntPtr = FindWindowEx(Hwnd, IntPtr.Zero, "Edit", Nothing)

    'Alloc memory for the buffer that recieves the text
    Dim Hndl As IntPtr = Marshal.AllocHGlobal(200)

    'Send The WM_GETTEXT Message
    NumText = SendMessage(ChildHandle, WM_GETTEXT, 200, Hndl)

    'copy the characters from the unmanaged memory to a managed string
    Text = Marshal.PtrToStringUni(Hndl)

    'Display the string using a label
    Label2.Text = Text


End Sub

End Class

【问题讨论】:

  • 附带说明 - 您应该确保使用您的 HandleHndl 变量调用 Marshal.FreeHGlobal 以释放您分配的内存。此内存不是使用标准 .NET 机制分配的,因此 .NET 的垃圾收集不会为您清理它。
  • 您可能想查看这篇文章:Extract all child windows of window。我在其中包含了一个示例 VB.NET 程序,它可以检测和读取每个窗口的文本(以及其他内容)。

标签: vb.net vb.net-2010 external-application


【解决方案1】:

您必须遍历主窗口(外部应用程序)的子窗口并获取它们的属性。 您将使用以下内容:

<DllImport("User32.dll")> _
    Public Function EnumChildWindows _
        (ByVal WindowHandle As IntPtr, ByVal Callback As EnumWindowProcess, _
        ByVal lParam As IntPtr) As Boolean
    End Function

    Public Delegate Function EnumWindowProcess(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean

Public Function GetChildWindows(ByVal ParentHandle As IntPtr) As IntPtr()
    Dim ChildrenList As New List(Of IntPtr)
    Dim ListHandle As GCHandle = GCHandle.Alloc(ChildrenList)
    Try
        EnumChildWindows(ParentHandle, AddressOf EnumWindow, GCHandle.ToIntPtr(ListHandle))
    Finally
        If ListHandle.IsAllocated Then ListHandle.Free()
    End Try
    Return ChildrenList.ToArray
End Function

详情请查看How can I get properties of controls contained in a popup message box using VB.Net

【讨论】:

    【解决方案2】:

    ' 在 excel vbe 上试试这个

    External_application_handle=findwindow(vbNullString,"External_application")
    textbox_1_handle=findwindowex(External_application_handle,0&,"Edit",vbNullString)
    next_handle=textbox_1_handle
    textbox_2_handle=findwindowex(External_application_handle,next_handle,"Edit",vbNullString")
    Length = SendMessage(textbox_2_handle, WM_GETTEXTLENGTH, 0,0)
    buffer$=space(Length)
    call sendmessage(textbox_2_handle,Length+1,buffer$)
    msgbox buffer
    

    【讨论】:

    • 请为您的答案提供上下文,只有代码可以解决问题,但可能无法帮助 OP 了解他做错了什么。
    猜你喜欢
    • 2011-11-25
    • 2023-03-12
    • 1970-01-01
    • 2010-09-06
    • 1970-01-01
    • 2013-08-15
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多