pythonwin-win 32 guiウィンドウの検索と遍歴

1638 ワード

#coding=utf-8

__author__ = 'Administrator'

__doc__ = '''
pythonwin win32gui   
        win32gui             ,
              
'''

import win32gui
from pprint import pprint

def gbk2utf8(s):
    return s.decode('gbk').encode('utf-8')

def show_window_attr(hWnd):
    '''
           
    :return:
    '''
    if not hWnd:
        return

    #      title gb2312   
    title = win32gui.GetWindowText(hWnd)
    title = gbk2utf8(title)
    clsname = win32gui.GetClassName(hWnd)

    print '    :%s ' % (hWnd)
    print '    :%s' % (title)
    print '    :%s' % (clsname)
    print ''

def show_windows(hWndList):
    for h in hWndList:
        show_window_attr(h)

def demo_top_windows():
    '''
                 
    :return:
    '''
    hWndList = []
    win32gui.EnumWindows(lambda hWnd, param: param.append(hWnd), hWndList)
    show_windows(hWndList)

    return hWndList

def demo_child_windows(parent):
    '''
                
    :return:
    '''
    if not parent:
        return

    hWndChildList = []
    win32gui.EnumChildWindows(parent, lambda hWnd, param: param.append(hWnd),  hWndChildList)
    show_windows(hWndChildList)
    return hWndChildList


hWndList = demo_top_windows()
assert len(hWndList)

parent = hWndList[20]
#               ,           
hWndChildList = demo_child_windows(parent)

print('-----top windows-----')
pprint(hWndList)

print('-----sub windows:from %s------' % (parent))
pprint(hWndChildList)