2012-12-30 12 views
5

È possibile ottenere testo dalla casella di testo dell'applicazione esterna, ma ora voglio ottenere il testo dalla casella di testo desiderata da un'applicazione esterna. Il mio inglese non è così buono, ecco perché vedi l'immagine qui sotto.Ottieni testo da caselle di testo specifiche da applicazioni esterne - Visual Basic .Net

enter image description here

Il sottostante Codice di ritorno Il primo Casella di testo Valore Solo.

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 
+0

Su un lato nota - è necessario assicurarsi di chiamare [ Marshal.FreeHGlobal] (http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.freehglobal.aspx) con le variabili 'Handle' e' Hndl' per liberare la memoria che hai allocato. Questa memoria non viene allocata utilizzando i meccanismi .NET standard, quindi la garbage collection di .NET non la ripulirà automaticamente. – prprcupofcoffee

+0

Si consiglia di controllare questo post: [Estrai tutte le finestre figlio della finestra] (http://stackoverflow.com/questions/13345267/extract-all-child-windows-of-window/14408950#14408950). Lì includo un programma VB.NET di esempio in grado di rilevare e leggere il testo (tra le altre cose) di ogni singola finestra. – xfx

risposta

1

È necessario scorrere i figli della finestra principale (Applicazione esterna) e ottenere le relative proprietà. Potrai utilizzare il seguente:

<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 

Per per i dettagli, controllare questo How can I get properties of controls contained in a popup message box using VB.Net

0

'provare questo su 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 
+0

Fornisci contesto alla tua risposta, solo il codice può risolvere il problema ma probabilmente non aiuta l'OP a capire cosa ha fatto di sbagliato. –

Problemi correlati