微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

vb.net – 从外部应用程序中获取特定文本框中的文本 – Visual Basic .Net

我可以从外部应用程序文本框中获取文本,但现在我想从外部应用程序中获取所需文本框中的文本.
我的英语不太好,这就是为什么看下面的图像.

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

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,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

解决方法

您必须循环浏览主窗口(外部应用程序)的子项并获取属性.
您将使用以下内容

<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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐