MikeRand
MikeRand

Reputation: 4828

Win32API replicate Spy++ window-information capabilities within Python

I have a third-party GUI program that I'm wrapping with a Python class (using ctypes).

Are there Win32 API functions that can do the following?

1) Obtain the window handle for a window at a given screen location.

2) Obtain the window handle for a Button or Static window with a given caption.

3) Send text to an Edit window.

4) Extract text from a RICHEDIT instance.

I have WinSpy (a Spy++-type app) and know that it's possible to obtain window handles and captions using that tool, but I need something that works within Python.

I assume that Python's ctypes gives me access to any function within the Win32 API, so I've been scanning MSDN (especially this windows/messages section). I can't seem to find anything that works.

Thanks,

Mike

Upvotes: 0

Views: 2315

Answers (2)

Beau
Beau

Reputation: 11358

I had trouble finding a very simple example for WM_GETTEXT with pywin32 and figured here might be a good place to add one, since it answers part of the question:

MAX_LENGTH = 1024

handle = # A handle returned from FindWindowEx, for example

buffer = win32gui.PyMakeBuffer(MAX_LENGTH)
length = win32gui.SendMessage(handle, win32con.WM_GETTEXT, MAX_LENGTH, buffer)

result = buffer[:length]

Upvotes: 2

adf88
adf88

Reputation: 4442

  1. WindowFromPoint

  2. FindWindowEx to find a child of a window with a given class and name (caption). Repeat operation to get through each parent-child indirection. EnumChildWindows can be helpful too.

  3. SendMessageTimeout + WM_SETTEXT

  4. SendMessageTimeout + WM_GETTEXT or EM_STREAMOUT

Upvotes: 3

Related Questions