我正在尝试TTM_GETTEXT
通过SendMessage
使用 pywin32 来使用。问题是,lparam
应该存储文本的结构必须是TOOLINFO
,这在 MSDN 中有很好的记录,但在 pywin32 中没有对应物。有没有办法使用 python 和 pywin32 创建相同的结构?
编辑:这是我想出的使用ctypes
. 我做了一个Structure
for 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
是正确的,所以我一定做错了什么。