分享

pythonwin-win32gui 窗口查找和遍历

 dinghj 2019-04-16

pythonwin-win32gui 窗口查找和遍历

2016年10月30日 22:55:27 liuyukuan 阅读数:9553
  1. #coding=utf-8
  2. __author__ = 'Administrator'
  3. __doc__ = '''
  4. pythonwin中win32gui的用法
  5. 本文件演如何使用win32gui来遍历系统中所有的顶层窗口,
  6. 并遍历所有顶层窗口中的子窗口
  7. '''
  8. import win32gui
  9. from pprint import pprint
  10. def gbk2utf8(s):
  11. return s.decode('gbk').encode('utf-8')
  12. def show_window_attr(hWnd):
  13. '''
  14. 显示窗口的属性
  15. :return:
  16. '''
  17. if not hWnd:
  18. return
  19. #中文系统默认title是gb2312的编码
  20. title = win32gui.GetWindowText(hWnd)
  21. title = gbk2utf8(title)
  22. clsname = win32gui.GetClassName(hWnd)
  23. print '窗口句柄:%s ' % (hWnd)
  24. print '窗口标题:%s' % (title)
  25. print '窗口类名:%s' % (clsname)
  26. print ''
  27. def show_windows(hWndList):
  28. for h in hWndList:
  29. show_window_attr(h)
  30. def demo_top_windows():
  31. '''
  32. 演示如何列出所有的顶级窗口
  33. :return:
  34. '''
  35. hWndList = []
  36. win32gui.EnumWindows(lambda hWnd, param: param.append(hWnd), hWndList)
  37. show_windows(hWndList)
  38. return hWndList
  39. def demo_child_windows(parent):
  40. '''
  41. 演示如何列出所有的子窗口
  42. :return:
  43. '''
  44. if not parent:
  45. return
  46. hWndChildList = []
  47. win32gui.EnumChildWindows(parent, lambda hWnd, param: param.append(hWnd), hWndChildList)
  48. show_windows(hWndChildList)
  49. return hWndChildList
  50. hWndList = demo_top_windows()
  51. assert len(hWndList)
  52. parent = hWndList[20]
  53. #这里系统的窗口好像不能直接遍历,不知道是否是权限的问题
  54. hWndChildList = demo_child_windows(parent)
  55. print('-----top windows-----')
  56. pprint(hWndList)
  57. print('-----sub windows:from %s------' % (parent))
  58. pprint(hWndChildList)

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多