0

我正在尝试TTM_GETTEXT通过SendMessage使用 pywin32 来使用。问题是,lparam应该存储文本的结构必须是TOOLINFO,这在 MSDN 中有很好的记录,但在 pywin32 中没有对应物。有没有办法使用 python 和 pywin32 创建相同的结构?

编辑:这是我想出的使用ctypes. 我做了一个Structurefor TOOLINFO,从中创建了一个缓冲区以传递给 pywin32's SendMessage,然后将其转换回TOOLINFO ctypes Structure. 唯一的问题是,它不起作用:

# My TOOLINFO struct:
class TOOLINFO(Structure):
  _fields_ = [("cbSize", UINT),
              ("uFlags", UINT),
              ("hwnd", HWND),
              ("uId", POINTER(UINT)),
              ("rect", RECT),
              ("hinst", HINSTANCE),
              ("lpszText", LPWSTR),
              ("lpReserved", c_void_p)]

# send() definition from PythonInfo wiki FAQs
def send(self):
  return buffer(self)[:]

ti = TOOLINFO()
text = ""
ti.cbSize = sizeof(ti)
ti.lpszText = text                 # buffer to store text in
ti.uId = pointer(UINT(wnd))        # wnd is the handle of the tooltip
ti.hwnd = w_wnd                    # w_wnd is the handle of the window containing the tooltip
ti.uFlags = commctrl.TTF_IDISHWND  # specify that uId is the control handle
ti_buffer = send(ti)               # convert to buffer for pywin32

del(ti)

win32gui.SendMessage(wnd, commctrl.TTM_GETTEXT, 256, ti_buffer)

ti = TOOLINFO()              # create new TOOLINFO() to copy result to

# copy result (according to linked article from Jeremy)
memmove(addressof(ti), ti_buffer, sizeof(ti))

if ti.lpszText:
  print ti.lpszText          # print any text recovered from the tooltip

文本没有被打印,但我认为它应该包含我想从中提取的工具提示中的文本。我的使用方式有问题ctypes吗?我很确定我的价值观wnd和价值观w_wnd是正确的,所以我一定做错了什么。

4

1 回答 1

1

它不是特别漂亮,但是您可以使用struct模块将字段打包成具有适当字节顺序、对齐和填充的字符串。这有点棘手,因为您必须使用格式字符串以正确的顺序仅使用相应的基本数据类型来定义结构。

您还可以使用 ctypes 来定义结构类型或直接与 DLL 交互(而不是使用 pywin32)。ctypes 结构定义更接近 C 定义,因此您可能更喜欢它。

If you choose to use ctypes for structure defs along with pywin32, check out the following for a clue to how to serialize the structs into strings: How to pack and unpack using ctypes (Structure <-> str)

于 2010-07-30T01:48:05.113 回答