没有办法从另一个应用程序的面板控件获取文本

时间:2013-03-11 10:42:31

标签: vb.net winforms winapi panel handle

我正在尝试从另一个应用程序的Windows窗体中获取信息。

我可以从此应用程序的文本框或标签中读取数据,但不能从PANEL读取数据,因为此面板不包含控件。

我需要你的建议。

提前致谢。 这里是我正在使用的代码:

  For Each top As windowsAPIoutils.ApiWindow In enumerator.GetTopLevelWindows()
        For Each child As windowsAPIoutils.ApiWindow In enumerator.GetChildWindows(top.hWnd)
            If top.MainWindowTitle.StartsWith("TITLE_Of_APPLICATION") Then
               'The class name of the control
                If child.ClassName = "TEdit"  Then

                    textbox1.Text = child.MainWindowTitle 

                End If
            End If


        Next child
    Next top

2 个答案:

答案 0 :(得分:4)

您可以使用Win32 API执行此操作的唯一方法是,您要获取其文本的项目是Win32控件,并由实际窗口支持。

这就是为什么如果其他项目是文本框或标签,它可以正常工作,因为它们分别使用Win32 EDITSTATIC控件实现。

我不确切地知道你对“面板”的意思,但我的猜测是它已被其他应用程序自定义绘制。因此,您需要向该应用程序询问其包含的文本。 Windows无法提供给您,因为它不是标准的Windows控件。如果您因任何原因无法询问其他申请,则需要研究alternative methods, like UI automation

如果通过“面板”,你的意思是group box,那么这只是一个标准的Windows按钮控件,它有一个标题(显示在顶部)。您可以像检索标签控件的标题一样检索它。在Win32术语中,这意味着向控件发送WM_GETTEXT message

答案 1 :(得分:0)

这是我使用的解决方案:  Tesseract开源OCR引擎,在这里获取它的链接:https://code.google.com/p/tesseract-ocr/

如何使用它:

Imports System.IO
Imports System.Threading
Imports System.Collections.Specialized

Public class myClass

Private ProcessList As New Hashtable

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)     Handles Button.Click
Dim croppedFile as String = "C:\image.tif"
Dim OCRProcess As Process = New Process()
OCRProcess.StartInfo.FileName = "C:\tesseract\tesseract.exe"
OCRProcess.StartInfo.Arguments = croppedFile & " " & croppedFile & " -l eng"
OCRProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
OCRProcess.StartInfo.CreateNoWindow = True
OCRProcess.EnableRaisingEvents = True
AddHandler OCRProcess.Exited, AddressOf Me.ProcessExited
OCRProcess.Start()
ProcessList.Add(OCRProcess.Id.ToString, croppedFile & ".txt")
Do While Not OCRProcess.HasExited
    Application.DoEvents()
Loop
End Sub

Friend Sub ProcessExited(ByVal sender As Object, ByVal e As System.EventArgs)
Dim Proc As DictionaryEntry
Dim oRead As StreamReader
Dim EntireFile As String = ""
For Each Proc In ProcessList
    If (sender.id.ToString = Proc.Key) Then
        oRead = File.OpenText(Proc.Value)
        EntireFile = oRead.ReadToEnd()
    End If
Next
MsgBox(EntireFile)
End Sub

End Class

希望它会帮助某人