Get other running processes window sizes in Python

后端 未结 4 592
滥情空心
滥情空心 2020-11-30 04:40

This isn\'t as malicious as it sounds, I want to get the current size of their windows, not look at what is in them. The purpose is to figure out that if every other window

4条回答
  •  心在旅途
    2020-11-30 04:52

    I updated the GREAT @DZinX code adding the title/text of the windows:

    import win32con
    import win32gui
    
    def isRealWindow(hWnd):
        #'''Return True iff given window is a real Windows application window.'''
        if not win32gui.IsWindowVisible(hWnd):
            return False
        if win32gui.GetParent(hWnd) != 0:
            return False
        hasNoOwner = win32gui.GetWindow(hWnd, win32con.GW_OWNER) == 0
    lExStyle = win32gui.GetWindowLong(hWnd, win32con.GWL_EXSTYLE)
    if (((lExStyle & win32con.WS_EX_TOOLWINDOW) == 0 and hasNoOwner)
      or ((lExStyle & win32con.WS_EX_APPWINDOW != 0) and not hasNoOwner)):
        if win32gui.GetWindowText(hWnd):
            return True
    return False
    
    def getWindowSizes():
    
    Return a list of tuples (handler, (width, height)) for each real window.
    '''
    def callback(hWnd, windows):
        if not isRealWindow(hWnd):
            return
        rect = win32gui.GetWindowRect(hWnd)
        text = win32gui.GetWindowText(hWnd)
        windows.append((hWnd, (rect[2] - rect[0], rect[3] - rect[1]), text ))
    windows = []
    win32gui.EnumWindows(callback, windows)
    return windows
    
    for win in getWindowSizes():
    print(win)
    

提交回复
热议问题