By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

Reading text labels from a Win32 window

P: n/a
Does anyone know of a way to read text labels from a Win32 application.
I am familiar with using pywin32 and the SendMessage function to
capture text from Buttons,text boxex, comboboxes, etc, however, the
text I am would like to capture doesn't appear to be in a control.

Nov 28 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

ge********@hotmail.com schrieb:
Does anyone know of a way to read text labels from a Win32 application.
I am familiar with using pywin32 and the SendMessage function to
capture text from Buttons,text boxex, comboboxes, etc, however, the
text I am would like to capture doesn't appear to be in a control.

This article tells the whole story

"The secret life of GetWindowText"
http://blogs.msdn.com/oldnewthing/ar.../21/54675.aspx

And for the headaches, see

"How to retrieve text under the cursor"
http://blogs.msdn.com/oldnewthing/ar...23/118893.aspx
Anyway, msdn is the better address for problems like these.

Regards Jürgen

Nov 28 '06 #2

P: n/a
jU****@arcor.de schrieb:
ge********@hotmail.com schrieb:
>Does anyone know of a way to read text labels from a Win32 application.
I am familiar with using pywin32 and the SendMessage function to
capture text from Buttons,text boxex, comboboxes, etc, however, the
text I am would like to capture doesn't appear to be in a control.


This article tells the whole story

"The secret life of GetWindowText"
http://blogs.msdn.com/oldnewthing/ar.../21/54675.aspx
Appended is a script that is a fairly straight-forward conversion of the
code in the above article to Python. Needs ctypes and comtypes.

Python 2.5 already includes ctypes; comtypes can be installed
with 'easy_install comtypes'.

Thomas

import sys, time
from ctypes import windll, oledll, WinError, byref, POINTER
from ctypes.wintypes import POINT

from comtypes import COMError
from comtypes.automation import VARIANT
from comtypes.client import GetModule

# create wrapper for the oleacc.dll type library
GetModule("oleacc.dll")
# import the interface we need from the wrapper
from comtypes.gen.Accessibility import IAccessible

def GetCursorPos():
"Return the cursor coordinates"
pt = POINT()
if not windll.user32.GetCursorPos(byref(pt)):
raise WinError()
return pt.x, pt.y

def AccessibleObjectFromPoint(x, y):
"Return an accessible object and an index. See MSDN for details."
pacc = POINTER(IAccessible)()
var = VARIANT()
oledll.oleacc.AccessibleObjectFromPoint(POINT(x, y), byref(pacc), byref(var))
return pacc, var

if __name__ == "__main__":
while 1:
time.sleep(1)
x, y = GetCursorPos()

try:
pacc, index = AccessibleObjectFromPoint(x, y)
name = pacc.accName[index]
except (WindowsError, COMError), details:
print details
continue
if name is not None:
print "===", (x, y), "=" * 60
print name.encode(sys.stdout.encoding, "backslashreplace")

Nov 29 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.