感谢Python和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'

我尝试使用具有类似结果的Unicode缓冲区。

我没有发现我的错误。任何想法?

有帮助吗?

解决方案

如果所讨论的列表框是所有者绘制的,则此段落 lb_getText 文档可能相关:

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

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

其他提示

看起来您需要使用包装结构进行结果。我在网上找到了一个例子,也许这将为您提供帮助:

http://www.brunningonline.net/simon/blog/archives/winguiauto.py.html

# Programmer : Simon Brunning - simon@brunningonline.net
# Date       : 25 June 2003
def _getMultipleWindowValues(hwnd, getCountMessage, getValueMessage):
    '''A common pattern in the Win32 API is that in order to retrieve a
    series of values, you use one message to get a count of available
    items, and another to retrieve them. This internal utility function
    performs the common processing for this pattern.

    Arguments:
    hwnd                Window handle for the window for which items should be
                        retrieved.
    getCountMessage     Item count message.
    getValueMessage     Value retrieval message.

    Returns:            Retrieved items.'''
    result = []

    VALUE_LENGTH = 256
    bufferlength_int  = struct.pack('i', VALUE_LENGTH) # This is a C style int.

    valuecount = win32gui.SendMessage(hwnd, getCountMessage, 0, 0)
    for itemIndex in range(valuecount):
        valuebuffer = array.array('c',
                                  bufferlength_int +
                                  " " * (VALUE_LENGTH - len(bufferlength_int)))
        valueLength = win32gui.SendMessage(hwnd,
                                           getValueMessage,
                                           itemIndex,
                                           valuebuffer)
        result.append(valuebuffer.tostring()[:valueLength])
    return result

def getListboxItems(hwnd):
    '''Returns the items in a list box control.

    Arguments:
    hwnd            Window handle for the list box.

    Returns:        List box items.

    Usage example:  TODO
    '''

    return _getMultipleWindowValues(hwnd,
                                     getCountMessage=win32con.LB_GETCOUNT,
                                     getValueMessage=win32con.LB_GETTEXT)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top