2009-09-23 2 views
0

파이썬 및 ctypes 덕분에 목록 상자의 내용을 가져오고 싶습니다.파이썬 및 ctypes 목록 상자의 내용을 win32에 가져 오는 경우

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'과 같습니다.

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

오류가 발견되지 않습니다. 어떤 생각?

답변

1

문제의 목록 상자 소유자가 그린 경우에서, LB_GETTEXT 문서에서이 구절이 관련이있을 수 :

소유자가 그린 스타일로 LBS_HASSTRINGS 스타일이없는 목록 상자를 만드는 경우 lParam 매개 변수가 가리키는 버퍼는 항목과 관련된 값 (항목 데이터)을받습니다.

받은 네 바이트는 항목 별 데이터에 저장되는 일반적인 값인 포인터 일 것입니다.

+0

나는 당신이 옳다고 생각합니다. 이것은 소유자가 그린 목록 상자입니다. 데이터 구조가 무엇인지 모르겠으므로 텍스트를 얻을 수 없습니다 .-( – luc

0

결과에 대해 압축 된 구조를 사용해야하는 것 같습니다. 나는 온라인 예를 발견, 아마도이 도움을 줄 것입니다 :

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

# Programmer : Simon Brunning - [email protected] 
# 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) 
관련 문제