在Win32上使用python和ctypes获取列表框的内容时出现问题 [英] Problem when getting the content of a listbox with python and ctypes on win32

查看:359
本文介绍了在Win32上使用python和ctypes获取列表框的内容时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于python和ctypes,我想获取列表框的内容。

I would like to get the content of a list box thanks to python and ctypes.

item_count = ctypes.windll.user32.SendMessageA(hwnd, win32con.LB_GETCOUNT, 0, 0)
items = []
for i in xrange(item_count):
    text_len = ctypes.windll.user32.SendMessageA(hwnd, win32con.LB_GETTEXTLEN, i, 0)
    buffer = ctypes.create_string_buffer("", text_len+1)
    ctypes.windll.user32.SendMessageA(hwnd, win32con.LB_GETTEXT, i, buffer)
    items.append(buffer.value)
print items

项目数正确但文本错误。所有text_len均为4,文本值类似于'0\xd9\xee\x02\x90'

The number of items is correct but the text is wrong. All text_len are 4 and the text values are something like '0\xd9\xee\x02\x90'

我尝试将unicode缓冲区与类似的结果。

I have tried to use a unicode buffer with a similar result.

我找不到我的错误。任何的想法?

I don't find my error. Any idea?

推荐答案

如果有问题的列表框是所有者绘制的,则此段落来自 LB_GETTEXT 文档可能是相关的:

If the list box in question is owner-drawn, this passage from the LB_GETTEXT documentation may be relevant:

如果使用所有者绘制样式创建列表框,但没有LBS_HASSTRINGS样式,则由lParam参数指向的缓冲区将接收与该项目关联的值(项目数据)。 / p>

If you create the list box with an owner-drawn style but without the LBS_HASSTRINGS style, the buffer pointed to by the lParam parameter will receive the value associated with the item (the item data).

您收到的四个字节肯定看起来像是一个指针,这是存储在每个项目数据中的典型值。

The four bytes you received certainly look like they may be a pointer, which is a typical value to store in the per-item data.

这篇关于在Win32上使用python和ctypes获取列表框的内容时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆