如何从窗口获取文本内容 [英] How to obtain textual contents from a window

查看:189
本文介绍了如何从窗口获取文本内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示书的窗口.在下面的两个较小的框中,有打开的书的页码和卷信息.我可以很容易地获得这些信息,如下所示:

I have a window that displays a book. In two smaller boxes below, there is page number and volume information of the book that is open. I can get that information easily as follows:

ControlGetText, volume, ThunderRT6TextBox3      
ControlGetText, page, ThunderRT6TextBox2

然后我的脚本使我的鼠标指针移动到一个按钮.单击它,然后会打开一个新窗口.在该窗口中,有更多与该书有关的文本信息,例如出版商,名称作者,版本等.我想检索该信息.但是,当我尝试相同的策略时,它不起作用,例如:

Then my script makes my mouse pointer move to a button. It clicks it, and a new window pops open. In that window, there is more textual information related to the book, such as publisher, name author, edition etc. I want to retrieve that information. But when I try the same strategy it is not working, eg:

ControlGetText, data, RichTextWndClass3

这两种情况之间唯一的区别是,这两个小方框是可编辑的,您可以输入文本,而此窗口是静态的.

The only difference between the two cases, is that those two small boxes are editable, you can enter text whereas this window is static.

我尝试了许多其他选择,例如: SendEvent ^a 相当于控件+ a,应该选择所有内容.我试着暂停一下,但它永远不会选择.我尝试通过脚本手动双击该窗口.它有效,一个单词就可以像这样被选择.即使如此,SendEvent ^ a也不执行任何操作. 但是,如果我执行SendEvent ^ {insert},则所选单词将被复制到我的剪贴板中.

I tried many other options such as: SendEvent ^a Which is equivalent to control + a, which should select everything. I tried putting pauses but it would never select. I tried the script to manually double click on that window. It works, and one word gets select like that. Even then SendEvent ^a doesn't do anything. However, if I do SendEvent ^{insert}, then the selected word gets copied to my clipboard.

我尝试了更多组合:

ControlSend ahk_class ThunderRT6FormDC, ^a
ControlSend ClassNN RichTextWndClass3, ^a

ControlSend ThunderRT6FormDC, ^a
ControlSend RichTextWndClass3, ^a

它们都不起作用.所有文本选择都不会在该窗口中显示出来.

None of them work. All text selection does not manifest itself in that window.

对我来说,唯一剩下的选择就是让脚本手动选择整个文本.但是,这是缓慢且非常荒谬的.此外,在窗口间谍"下可见的窗口文本"部分下,文本就在那里.我尝试了许多其他可能性,但我精疲力尽.我如何直接获取该文本?

The only alternative remaining for me is to make the script do a manual selection of the entire text. However, this is slow and very ridiculous. Moreover, in Window Spy under the section: Visible Window Text, the text is all there. I tried many other possibilities and I am at the end of my wits. How am I to harvest that text directly?

编辑- 窗口的文本显示在Window Spy中,标题为:TitleMatchMode = slow Visible Text,而不是标题:Visible Window Text

EDIT-- The text of the window shows in Window Spy under the heading: TitleMatchMode=slow Visible Text, NOT the heading: Visible Window Text

编辑- 我跟你讲了两个窗户.我在其中获得卷和页码的第一个.第二个需要按一个按钮弹出. 这两个窗口具有相同的类名: ahk_class ThunderRT6MDIForm 这有什么问题吗?

EDIT-- I spoke to you about two windows. The first one in which i get volume and page number. The second one which needs to pop up by pressing a button. Both these windows have the same class-name: ahk_class ThunderRT6MDIForm Is that problematic in any way?

编辑- 结论是,我不可能直接从第二个窗口中获取该文本.因此,我选择了the脚,无聊的手动方法.我向活动窗口发送{shift down},然后在窗口底部单击.然后,将选择内容保存到剪贴板.它有效,但只是愚蠢.请帮助我找到比这更优雅的解决方案.

EDIT-- The conclusion is that it is impossible for me to get that text from the second window directly. As such, I opted for the lame, boring manual method. I send out a {shift down} to the active window and then do a click at the bottom of the window. Then I save the selection to my clipboard. It works, but it is just stupid. Please help me find a more elegant solution than this one.

这是我使用的代码:

ControlGetText, volume, ThunderRT6TextBox3 

ControlGetText, page, ThunderRT6TextBox2 

Click, 110, 70 

sleep 1000

SendInput {shift down}

click 29, 490

SendInput {shift up}

sleep 1000

SendInput, ^{ins} 

sleep 100

有趣的是,没有真正的键盘键(例如b c)是不可能的.但是我能够发送ctrl,shift和ins.如上所述,^ c也在发出问题,就像^ a在发出问题一样.

It is funny to note that real keyboard keys, such as a b c are not possible. But I am able to send a ctrl, shift and an ins. As I noted above, ^c was also giving issues just like ^a was giving issues.

推荐答案

此例程将完成从活动窗口获取和返回以下文本部分的工作: -EdtWindowTextFastVisible -EdtWindowTextSlowVisible -EdtWindowTextFastHidden -EdtWindowTextSlowHidden

This routine will do the job of getting and returning from the active window the following text sections: - EdtWindowTextFastVisible - EdtWindowTextSlowVisible - EdtWindowTextFastHidden - EdtWindowTextSlowHidden

MyGetWindowText(ByRef EdtWindowTextFastVisible, ByRef EdtWindowTextSlowVisible, ByRef EdtWindowTextFastHidden,ByRef EdtWindowTextSlowHidden)
{
; Source: https://code.google.com/p/autohotkey-cn/source/browse/trunk/Source/AHK_Window_Info/AHK_Window_Info_v1.7.ahk?r=6
EdtWindowTextFastVisible =
EdtWindowTextSlowVisible =
EdtWindowTextFastHidden =
EdtWindowTextSlowHidden =

WindowControlTextSize = 32767
VarSetCapacity(WindowControlText, WindowControlTextSize)
WinGet, WindowUniqueID, ID, A

;Suggested by Chris
WinGet, ListOfControlHandles, ControlListHwnd, ahk_id %WindowUniqueID% ; Requires v1.0.43.06+.
Loop, Parse, ListOfControlHandles, `n
{
    text_is_fast := true
    If not DllCall("GetWindowText", "uint", A_LoopField, "str", WindowControlText, "int", WindowControlTextSize)
    {
        text_is_fast := false
        SendMessage, 0xD, WindowControlTextSize, &WindowControlText,, ahk_id %A_LoopField% ; 0xD is WM_GETTEXT
    }
    If (WindowControlText <> ""){
        ControlGet, WindowControlStyle, Style,,, ahk_id %A_LoopField%
        If (WindowControlStyle & 0x10000000)
        { ; Control is visible vs. hidden (WS_VISIBLE).
            If text_is_fast
            EdtWindowTextFastVisible = %EdtWindowTextFastVisible%%WindowControlText%`r`n
            Else
            EdtWindowTextSlowVisible = %EdtWindowTextSlowVisible%%WindowControlText%`r`n
        } Else
        { ; Hidden text.
            If text_is_fast
            EdtWindowTextFastHidden = %EdtWindowTextFastHidden%%WindowControlText%`r`n
            Else
            EdtWindowTextSlowHidden = %EdtWindowTextSlowHidden%%WindowControlText%`r`n
        }
    }
}

;EdtWindowTextFastVisibleFull := ShowOnlyAPartInGui("EdtWindowTextFastVisible", EdtWindowTextFastVisible, 400)
;EdtWindowTextSlowVisibleFull := ShowOnlyAPartInGui("EdtWindowTextSlowVisible", EdtWindowTextSlowVisible, 400)
;EdtWindowTextFastHiddenFull := ShowOnlyAPartInGui("EdtWindowTextFastHidden", EdtWindowTextFastHidden, 400)
;EdtWindowTextSlowHiddenFull := ShowOnlyAPartInGui("EdtWindowTextSlowHidden", EdtWindowTextSlowHidden, 400)

Return
}

这篇关于如何从窗口获取文本内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆