문제

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'과 비슷합니다.

비슷한 결과를 가진 유니 코드 버퍼를 사용하려고했습니다.

내 오류를 찾지 못했습니다. 아이디어가 있습니까?

도움이 되었습니까?

해결책

해당 목록 상자가 소유자로 작성되면이 구절은 lb_getText 문서화가 관련 될 수 있습니다.

LBS_HASSTRINGS 스타일이 없지만 LPARAM 매개 변수가 가리키는 버퍼는 항목 (항목 데이터)과 관련된 버퍼가 수신됩니다.

당신이받은 4 바이트는 분명히 포인터 인 것처럼 보이며, 이는 항목 별 데이터에 저장하는 일반적인 값입니다.

다른 팁

결과를 위해 포장 된 구조를 사용해야하는 것 같습니다. 온라인에서 예를 찾았습니다. 아마도 이것은 당신에게 도움이 될 것입니다 :

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