jU****@arcor.de schrieb:
ge********@hotm ail.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.automa tion import VARIANT
from comtypes.client import GetModule
# create wrapper for the oleacc.dll type library
GetModule("olea cc.dll")
# import the interface we need from the wrapper
from comtypes.gen.Ac cessibility import IAccessible
def GetCursorPos():
"Return the cursor coordinates"
pt = POINT()
if not windll.user32.G etCursorPos(byr ef(pt)):
raise WinError()
return pt.x, pt.y
def AccessibleObjec tFromPoint(x, y):
"Return an accessible object and an index. See MSDN for details."
pacc = POINTER(IAccess ible)()
var = VARIANT()
oledll.oleacc.A ccessibleObject FromPoint(POINT (x, y), byref(pacc), byref(var))
return pacc, var
if __name__ == "__main__":
while 1:
time.sleep(1)
x, y = GetCursorPos()
try:
pacc, index = AccessibleObjec tFromPoint(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.encodin g, "backslashrepla ce")