get hwnd of each window with python

coding:utf-8

print each window

import win32gui
import ctypes

EnumWindows = ctypes.windll.user32.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(
ctypes.c_bool,
ctypes.POINTER(ctypes.c_int),
ctypes.POINTER(ctypes.c_int))
GetWindowText = ctypes.windll.user32.GetWindowTextW
GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
IsWindowVisible = ctypes.windll.user32.IsWindowVisible

name = '姚峰杰'.decode('utf-8')
hwnd = win32gui.FindWindow(0, name)

titles = []
def foreach_window(hwnd, lParam):
if IsWindowVisible(hwnd):
length = GetWindowTextLength(hwnd)
buff = ctypes.create_unicode_buffer(length + 1)
GetWindowText(hwnd, buff, length + 1)
titles.append(hwnd) # , buff.value
return True
EnumWindows(EnumWindowsProc(foreach_window), 0)

for i in range(len(titles)):
print titles[i]

ctypes.windll.user32.MoveWindow(titles[5][0], 0, 0, 760, 500, True)

ctypes.windll.user32.SwitchToThisWindow(hwnd, True)

posted @ 2017-03-14 13:31  idlewith  阅读(556)  评论(0编辑  收藏  举报