我正在嘗試使用TTM_GETTEXT
通過SendMessage
使用pywin32。問題是,應該存儲文本的lparam
的結構必須是TOOLINFO
,這在MSDN中有詳細記錄,但在pywin32中沒有對應文件。有沒有辦法使用python和pywin32創建相同的結構?將c中的結構轉換爲pywin32?
編輯:這裏是代碼我想出了使用。我爲TOOLINFO
做了一個Structure
,創建了一個緩衝區從它傳遞到pywin32的SendMessage
,然後將它轉換回TOOLINFO
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
文字,但我認爲它應該包含從我想提取工具提示文本。我如何使用有什麼問題嗎?我很確定我的wnd
和w_wnd
的值是正確的,所以我一定是做錯了。
'ctypes'很好看,但我陷得太深pywin32已經可以切換。但結構模塊看起來也不錯。我可能會用'struct'方法。謝謝! – maranas 2010-07-30 09:30:28