Reading text labels from a Win32 window

G

geskerrett

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.
 
J

jUrner

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/archive/2003/08/21/54675.aspx

And for the headaches, see

"How to retrieve text under the cursor"
http://blogs.msdn.com/oldnewthing/archive/2004/04/23/118893.aspx


Anyway, msdn is the better address for problems like these.

Regards Jürgen
 
T

Thomas Heller

This article tells the whole story

"The secret life of GetWindowText"
http://blogs.msdn.com/oldnewthing/archive/2003/08/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")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top