【问题标题】:Fetching specific text from a dialog box using autoit使用 autoit 从对话框中获取特定文本
【发布时间】:2019-01-03 14:18:55
【问题描述】:

我想从对话框中获取文本。我正在使用winGetTitle() 函数从对话框中获取标题。

我在autoit中的代码如下:

$pId = Run("C:/foldername/Dialogbox.exe")

Local $hWnd   = WinWait("[CLASS:#32770]", "", 10)
Local $sTitle = WinGetTitle("[ACTIVE]")

; Display the window title.
ConsoleWrite($sTitle & @CRLF)

; changing control from the class containg title to the class containing the text. 
Local $hSysTray_Handle = ControlGetHandle('[Class:#32770]', '', '[Class:SysListView32;Instance:1]')
Local $sText           = WinGetText("[ACTIVE]")
Local $sTextnew        = ControlGetText($hSysTray_Handle, "", $sText)

ConsoleWrite($sTextnew & @CRLF)

这只会返回标题而不是对话框中的文本。 #32770是对话框和标题的主类,文本在autoit基本控制信息的不同类中。

我是 autoit 新手,不知道如何从对话框中获取文本。或者我应该为此使用 sikuli?

【问题讨论】:

    标签: autoit


    【解决方案1】:
    ; Start Mp3tag if not running.
    If Not ProcessExists('Mp3tag.exe') Then
        Run('"C:\Program Files (x86)\Mp3tag\Mp3tag.exe"')
        If @error Then Exit 1
    EndIf
    
    ; Scans working directory for Mp3 files.
    WinWaitClose('Reading directory', '', 5)
    
    ; Main window.
    $hWnd = WinWait('Mp3tag')
    
    ; Get item count of 1st column from the listview.
    For $i1 = 1 To 5
        $iCount = ControlListView($hWnd, '', 'SysListView321', 'GetItemCount')
    
        If $iCount Then
            ConsoleWrite('Found ' & $iCount & ' items.' & @CRLF)
            ExitLoop
        EndIf
    
        Sleep(1000)
    Next
    
    If Not $iCount Then
        MsgBox(0x40000, @ScriptName, 'Count is 0')
        Exit 1
    EndIf
    
    ; Get text of each listview item in the 1st column.
    For $i1 = 0 To $iCount -1
        $sText = ControlListView($hWnd, '', 'SysListView321', 'GetText', $i1)
        ConsoleWrite(StringFormat('%3d  Text: %s', $i1, $sText) & @CRLF)
    Next
    

    我没有"C:/foldername/Dialogbox.exe" 可供测试。 我确实有Mp3tag,它有一个SysListView32 控件。

    ControlListView() 可以从 ListView 控件中获取文本等。 参数GetItemCount 将获取列表项的计数 GetText 将从每个列表项中获取文本。

    这是测试输出:

    Found 15 items.
      0  Text: 01 - Lively Up Yourself.mp3
      1  Text: 02 - Soul Rebel.mp3
      2  Text: 03 - Treat Yourself Right.mp3
      3  Text: 04 - Rebels Hop.mp3
      4  Text: 05 - Soul Almighty.mp3
      5  Text: 06 - Kaya.mp3
      6  Text: 07 - Trenchtown Rock.mp3
      7  Text: 08 - Soul Shakedown Party.mp3
      8  Text: 09 - Natural Mystic.mp3
      9  Text: 10 - Fussing And Fighting.mp3
     10  Text: 11 - African Herbsman.mp3
     11  Text: 12 - Keep On Moving.mp3
     12  Text: 13 - Go Tell It On The Mountain.mp3
     13  Text: 14 - How Many Times.mp3
     14  Text: 15 - Bonus Track.mp3
    

    列表项编号在左侧 列表项文本在右侧, 关注Text:


    猜测一下,测试您的Dialogbox.exe 的代码:

    Run("C:\foldername\Dialogbox.exe")
    $hWnd = WinWait("[CLASS:#32770]", "", 10)
    
    ; Allow time for SysListView321 to fully load.
    Sleep(1000)
    
    ; Get the window title.
    $sTitle = WinGetTitle("[ACTIVE]")
    ConsoleWrite($sTitle & @CRLF)
    
    ; Changing control from the class containing title to the class containing the text.
    $hLView = ControlGetHandle($hWnd, '', 'SysListView321')
    
    ; Get item count of 1st column from the listview.
    $iCount = ControlListView($hWnd, '', $hLView, 'GetItemCount')
    
    ; Get text of each listview item in the 1st column.
    For $i1 = 0 To $iCount -1
        $sText = ControlListView($hWnd, '', $hLView, 'GetText', $i1)
        ConsoleWrite(StringFormat('%3d  Text: %s', $i1, $sText) & @CRLF)
    Next
    

    即使它有效,它也可以使用一些改进。

    【讨论】:

    • 感谢您的回答。但是上面的代码不起作用。它没有在 - ConsoleWrite(StringFormat('%3d Text: %s', $i1, $sText) & @CRLF) 行上提供任何输出。对话框中有SysListView32类中的文字,但不可选择。此外,除了类 SysListView32 和实例 1 之外,autoit 工具不提供任何信息,如可见、隐藏文本。
    • Window Info Tool 无法从 ListViewsTreeviews 等复杂控件中合理地读取内部文本,因为它将是复杂的编程来实现它,并通过非常复杂的显示来显示信息,这可能没有用,因为您需要复杂的编程才能访问它。至于编辑或按钮等简单控件,可通过ControlGetText() 显示和阅读。代码在WinWait() 之后可能需要Sleep(1000) 或更长时间,因为ListView 可能需要一段时间才能完全加载数据并准备好读取。
    • 为了更好的调试,考虑使用$sTitle = WinGetTitle($hWnd)而不是$sTitle = WinGetTitle("[ACTIVE]"),这样你就可以从$hWnd的窗口句柄中获取标题,稍后使用。
    • 感谢您的帮助。 $iCount 变量的值为 0。因此,它不会从 SysListView32 类的对话框中获取任何文本。我想这是我正在测试的一个复杂的对话框。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    相关资源
    最近更新 更多