win32でpythonとctypesを使用してリストボックスのコンテンツを取得する場合の問題

StackOverflow https://stackoverflow.com/questions/1466453

  •  13-09-2019
  •  | 
  •  

質問

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パラメーターが指すバッファーは、アイテムに関連付けられた値(アイテムデータ)を受け取ります。

受け取った4バイトは確かにポインターのように見えます。これは、1項目のデータに保存する典型的な値です。

他のヒント

結果に詰め込まれた構造を使用する必要があるようです。私はオンラインで例を見つけました、おそらくこれはあなたを支援するでしょう:

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