Reading text labels from a Win32 window

Discussion in 'Python' started by geskerrett@hotmail.com, Nov 28, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Guest

    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/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
     
    , Nov 28, 2006
    #2
    1. Advertising

  3. schrieb:
    > 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/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")
     
    Thomas Heller, Nov 29, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tony collier
    Replies:
    2
    Views:
    368
    tony collier
    Jan 12, 2004
  2. Replies:
    1
    Views:
    240
    Dennis Lee Bieber
    Jun 9, 2005
  3. hurluberlu
    Replies:
    1
    Views:
    84
    A. Sinan Unur
    Dec 17, 2004
  4. hurluberlu
    Replies:
    1
    Views:
    189
    A. Sinan Unur
    Dec 20, 2004
  5. Replies:
    5
    Views:
    150
Loading...

Share This Page