在QTP中有专门的一个对象TextUtil,可用于识别指定窗口中的文本。有时候使用其中的GetTextLocation方法可以解决某些控件识别和定位的问题。
The object used to recognize text within a specified window handle.
但是如果有其它方法的话,建议不要使用这种识别方式。例如,使用GetROProperty、GetVisibleText等。
When possible, it is recommended to use alternative methods of capturing text instead of using the TextUtil object. For example:
- Use the GetROProperty method or the Object property to retrieve the value of the text (or equivalent) property from an object in your application
- Use a text or text area output value step to retrieve text or a text or text area checkpoint step to verify a text value.
- Use the GetVisibleText or GetTextLocation methods of the appropriate test object.
GetText方法:
Returns the text from the specified window handle area. The area is defined by pairs of coordinates that designate two diagonally opposite corners of a rectangle.
例如:
Extern.Declare micLong,"FindWindow","User32","FindWindowA",micString,micString
hNotepad = Extern.FindWindow("Notepad", "无标题 - 记事本")
NotepadText = TextUtil.GetText(hNotepad)
MsgBox NotepadText
GetText支持查找指定句柄的窗口中的文本,也支持通过指定某个区域,例如:
MsgBox TextUtil.GetText(0, 20, 20, 200, 200)
第一个参数0表示不指定窗口,而是整个屏幕区域。
GetTextLocation方法:
Checks whether a specified text string is contained in a specified window area. If the text string is located, the location coordinates are also returned.
TextUtil.GetTextLocation(TextToFind, hWnd, Left, Top, Right, Bottom[, MatchWholeWords])
Important Information
- The text to capture must be visible in the application window when the step runs.
- This method returns True only if the the TextToFind argument value is found within a single line in the specified area. The text search restarts on each line of text.
- If the TextToFind argument value includes a space, then this method searches for that text as whole words, regardless of the value set in the MatchWholeWords argument. For example, if you search for "a b" and the text "bla bla" exists, the method will still return False. However, if the MatchWholeWords argument is set to False, then a search for "la" in an area where "bla bla" exists, would return True.
- If the text is found (return value = True) and if the Left, Top, Right, and Bottom arguments are supplied as variables, then the method also returns the exact coordinates of the specified text to the supplied arguments (the returned coordinates overwrite the supplied ones).
- The results of this method may be different depending on the settings selected in the Text Recognition pane of the Options dialog box (Tools menu > Options item > General node > Text Recognition node).
- The results of this method may be different in different run sessions depending on the operating system version you are using, service packs you have installed, other installed toolkits, the APIs used in your application, and so on. Therefore, when possible, it is highly recommended to use alternative ways retrieve the value of the text (or equivalent) property from an object in your application instead of using the GetText method, as described in the Important Information section of the TextUtil Object.
Syntax
例子:
l = -1
t = -1
r = -1
b = -1
Succeeded = TextUtil.GetTextLocation("文件",0,l,t,r,b,false)
If Not Succeeded Then
MsgBox "Text not found"
else
x = (l+r) / 2
y = (t+b) / 2
Set dr = CreateObject("Mercury.DeviceReplay")
dr.MouseMove x,y
dr.MouseClick x, y, 0
End If