【问题标题】:How can I use AutoHotKey to get the text in a control?如何使用 AutoHotKey 获取控件中的文本?
【发布时间】:2020-09-24 06:02:55
【问题描述】:

我使用的簿记系统发生了轻微的崩溃。现在我正在清理,并使用 AutoHotKey 1.1.26.00 来完成它。该过程涉及浏览一个很长的项目列表框,并且对于每个项目,我必须使用键盘和鼠标右键单击一系列窗口。这一切都在工作......大部分时间。

在最后一个窗口关闭之前,会弹出一个对话框,在该对话框上单击“否”按钮。我已经添加了代码来做到这一点。但是大约 5% 的时间对话框有不同的消息,这会阻止 AHK 脚本继续。我正在尝试获取消息的文本,但我无法确定如何获取。控件的名称是Static2,但似乎没有一个 AHK 函数能够访问该文本。

我使用在 AHK 帮助中找到的以下脚本来确定控件的名称:

#Persistent
SetTimer, WatchCursor, 100
Return

WatchCursor:
MouseGetPos, X, Y, id, control
WinGetTitle, title, ahk_id %id%
WinGetClass, class, ahk_id %id%
ToolTip, ahk_id %id%`nahk_class %class%`n%title%`nControl: %control%`nXpos = %X%`nYpos = %Y%
Return

脚本返回以下内容:

ahk_id 0x170586
ahk_class #32770
Control: Static2

我可以使用此信息获取Static2.. 中的文本吗?如果是这样,如何..?

不幸的是,对话框没有自己的窗口标题。我试图获取标题,但发现它与父窗口匹配。所以...我不知道下一步该尝试什么。

我的 AHK 脚本的完整代码如下,最后有截图。

我需要检查的对话框文本通常是Do you want to edit the accounting information at this time?,但如上所述,这偶尔会有所不同,我需要添加更多代码来处理它。这发生在脚本的最后一部分:

;Written for AutoHotkey v1.1.26.00

#NoEnv                          ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn                           ; Enable warnings to assist with detecting common errors.
SendMode Input                  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%     ; Ensures a consistent starting directory.

DelayMenu1  := 500  ; Wait for the menu to open.
DelayMenu2  := 75   ; As the selection moves down each item in the menu.
DelayMenu3  := 200  ; So the last item selected can be seen.
DelayWin1   := 200  ; After each window opens, a little time for the controls to populate.
ItemPosX    := 450  ; X-position of the first item in the list.
ItemPosY    := 104  ; Y-position of the first item in the list.
ItemAddY    := 14   ; Y-distance between each item.

;Activate the main window.
WinActivate, Dispatcher - Roadnet Transportation Suite
Sleep 500

;Get input from the user.
InputBox, C, Number of rows, How many rows?
If ErrorLevel
    {
    } Else {

        ; #################### BEGIN LOOP ####################

        Loop, %C%
            {
            
                ;Wait for the main window.
                WinWaitActive, Dispatcher - Roadnet Transportation Suite, , 3
                If ErrorLevel
                    {
                        MsgBox, Main window is not visible.
                        Exit
                    }
                Sleep %DelayWin1%

                ; #################### START ####################
            
                ;Right-click the item and wait for the menu to open.
                MouseMove, %ItemPosX%, %ItemPosY%
                Click, %ItemPosX%, %ItemPosY%, Right
                Sleep %DelayMenu1%
                
                ;Press the down key to "Start".
                Loop, 8
                    {
                        SendInput, {DOWN}
                        Sleep %DelayMenu2%
                    }
                Sleep %DelayMenu3%
                SendInput, {ENTER}

                ;Wait for "Start Route" window.
                WinWaitActive, Start Route, , 3
                If ErrorLevel
                    {
                        MsgBox, Start Route window is not visible.
                        Exit
                    }
                Sleep %DelayWin1%

                ;Use the Save keystroke. On other windows this does not work; mouse must be used.
                SendInput ^s
                Sleep 250

                ;Wait for the main window.
                WinWaitActive, Dispatcher - Roadnet Transportation Suite, , 3
                If ErrorLevel
                    {
                        MsgBox, Main window is not visible.
                        Exit
                    }
                Sleep %DelayWin1%

                ; #################### STOP COMPLETE ####################

                ;Right-click the item and wait for the menu to open.
                MouseMove, %ItemPosX%, %ItemPosY%
                Click, %ItemPosX%, %ItemPosY%, Right
                Sleep %DelayMenu1%
                
                ;Press the down key to "Stop Complete".
                Loop, 2
                    {
                        SendInput, {DOWN}
                        Sleep %DelayMenu2%
                    }
                Sleep %DelayMenu3%
                SendInput, {ENTER}

                ;Wait for "Stop Complete" window.
                WinWaitActive, Stop Complete, , 3
                If ErrorLevel
                    {
                        MsgBox, Stop Complete window is not visible.
                        Exit
                    }
                Sleep %DelayWin1%

                ;Click the "As Projected" button. The keyboard shortcut does not work; mouse must be used.
                Click, 120, 36
                Sleep 250

                ;Click the "Save" button. The keyboard shortcut does not work; mouse must be used.
                ;Note: At this point the button sticks in, and the mouse becomes an hourglass.
                Click, 24, 36
                Sleep 250

                ;Wait for the main window.
                WinWaitActive, Dispatcher - Roadnet Transportation Suite, , 3
                If ErrorLevel
                    {
                        MsgBox, Main window is not visible.
                        Exit
                    }
                Sleep %DelayWin1%

                ; #################### COMPLETE ####################

                ;Right-click the item and wait for the menu to open.
                MouseMove, %ItemPosX%, %ItemPosY%
                Click, %ItemPosX%, %ItemPosY%, Right
                Sleep %DelayMenu1%
                
                ;Press the down key to "Complete".
                Loop, 9
                    {
                        SendInput, {DOWN}
                        Sleep %DelayMenu2%
                    }
                Sleep %DelayMenu3%
                SendInput, {ENTER}

                ;Wait for "Complete Route" window.
                WinWaitActive, Complete Route, , 3
                If ErrorLevel
                    {
                        MsgBox, Complete Route window is not visible.
                        Exit
                    }
                Sleep %DelayWin1%

                ;For troubleshooting.
                ;MsgBox, %ClassID%`n%ControlName%
                ;WinGet, active_id, ID, A

                ;Click the "Save" button. The keyboard shortcut does not work; mouse must be used.
                Click, 24, 36
                Sleep 250
                
                ;Check if the accounting information dialog appeared.
                ;The message is usually: "Do you want to edit the accounting information at this time?"
                DialogAppeared := 0
                Loop, 10 ;Check 10 times.
                    {
                        ;Check for 
                        MouseMove, 340, 120
                        MouseGetPos, , , ClassID, ControlName
                        If (ControlName = "Button2") ;Use this button to check for the popup dialog.
                            {
                                DialogAppeared := 1
                                Break
                            } Else {
                                Sleep 250
                            }
                    }
                If (DialogAppeared = 1)
                    {
                        ;Click the "No" button. There is no keyboard shortcut; mouse must be used.
                        MouseMove, 340, 120
                        Click, 340, 120
                    } Else {
                        MsgBox, "The accounting information dialog could not be handled."
                        Exit
                    }
                Sleep 250

                ; #################### DONE ####################

                ;Increment the row-Y value before repeating the loop.
                ItemPosY := ItemPosY + ItemAddY
            }
    }

MsgBox, Done.
Exit

【问题讨论】:

  • 您能显示程序和/或弹出窗口的屏幕截图吗?对于“似乎没有一个 AHK 函数能够访问文本”,到目前为止您尝试了哪些方法?
  • 好的,我在我的 OP 末尾添加了一个屏幕截图。这不是原来的对话框;我无法复制原件,但它是完全相同的演示文稿。背景中有主窗口,然后是弹出窗口,然后是对话框。我尝试了 WinGetText 和 ControlGetText,但都没有给出任何输出。我认为这是因为对话框没有可以检测到的标题,但我不确定。我没有代码了,因为我已经将其编辑出来并进行了轮询循环,以监视对话框上的“否”按钮(屏幕截图中的对话框只有一个确定按钮)。

标签: autohotkey


【解决方案1】:

不一定是答案(至少),但不能真正将其发布在 cmets 中。
因此,通过快速查看图像,我猜您应该能够使用普通的 WinAPI 方法检索控件的内容(它似乎不是一些将在某些特殊框架中构建的应用程序,尽管它是超级老,所以我可能是错的。对超级老的不太熟悉)

您尝试ControlGetText 时可能出了点问题。 这样做有什么结果?

F1::
    MouseGetPos, , , window, ctrl
    ControlGetText, text, % ctrl, % "ahk_id " window
    MsgBox, % "Window's HWND: " window "`nControl's name: " Ctrl "`nControl's text: " text
return

要使用它,只需将鼠标放在要从中检索文本的控件上,然后按 F1。
可以先在记事本中尝试一下,您应该会看到它是如何工作的。

如果这不起作用,我建议下载“Winspector Spy”并尝试更深入地查看窗口和控件。手动查看是否在某处找到了您要查找的文本。

【讨论】:

    猜你喜欢
    • 2018-10-09
    • 2015-02-08
    • 2016-04-03
    • 2016-11-14
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多