【问题标题】:Get other running processes window sizes in Python在 Python 中获取其他正在运行的进程窗口大小
【发布时间】:2010-09-14 04:33:42
【问题描述】:

这并不像听起来那么恶意,我想获取他们窗口的当前大小,而不是查看其中的内容。目的是弄清楚如果每个其他窗口都是全屏的,那么我也应该这样启动。或者,如果所有其他进程只有 800x600,尽管分辨率很高,那么这可能就是用户想要的。为什么要让他们浪费时间和精力来调整我的窗口大小以匹配他们拥有的所有其他窗口?我主要是一名 Windows 开发人员,但如果有跨平台的方式来做到这一点,我一点也不会感到不安。

【问题讨论】:

    标签: python windows winapi pywin32


    【解决方案1】:

    使用来自WindowMover articleNattee Niparnan's blog post 的提示,我设法创建了这个:

    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)
            windows.append((hWnd, (rect[2] - rect[0], rect[3] - rect[1])))
        windows = []
        win32gui.EnumWindows(callback, windows)
        return windows
    
    for win in getWindowSizes():
        print win
    

    您需要Win32 Extensions for Python module 才能正常工作。

    编辑:我发现GetWindowRect 给出的结果比GetClientRect 更正确。来源已更新。

    【讨论】:

    • +1 用于提供您的原始来源。我总是喜欢了解其他人在读什么。
    • 关于如何在 linux(最好是 gnome)下做的任何建议?
    • 它给出了类似(1378864, (677, 522)),如何定位应用程序,例如notepad.exe
    【解决方案2】:

    我是AutoIt 的忠实粉丝。它们有一个 COM 版本,可让您在 Python 中使用它们的大部分功能。

    import win32com.client
    oAutoItX = win32com.client.Dispatch( "AutoItX3.Control" )
    
    oAutoItX.Opt("WinTitleMatchMode", 2) #Match text anywhere in a window title
    
    width = oAutoItX.WinGetClientSizeWidth("Firefox")
    height = oAutoItX.WinGetClientSizeHeight("Firefox")
    
    print width, height
    

    【讨论】:

    • AutoIt over win32com.client 的 API 是否略有不同?根据适用于 COM 的 AutoIt 函数参考,oAutoItX.Opt() 应该是 oAutoItX.AutoItSetOption(),并且 oAutoItX.WinGetClientSizeWidth() 和 oAutoItX.WinGetClientSizeHeight() 实际上应该是一个方法/函数 oAutoItX.WinGetClientSize(),它返回一个包含宽度(第一个索引)和高度(第二个索引)的 2 项元组或列表。
    • 如果我们在谈论 AutoIt 和 Python,还有一个不直接通过 win32com 的包装器:github.com/jacexh/pyautoit,另请参见 pypi.python.org/pypi/PyAutoIt/0.3
    • @David,我在 6 1/2 年前回答了这个问题!来自 AutoItSetOption 的 AutoItX 帮助文件:“您可以使用 Opt 作为 AutoItSetOption 的替代品。”并且没有 WinGetClientSize,只有 WinGetClientSizeWidth 和 WinGetClientSideHeight。您确定要查看 AutoItX 文档吗?
    • 也许 CHM 帮助文件与在线文档之间存在差异,或者 AutoIt 的更高版本发生了变化?我指的是版本 3。您的示例指的是哪个版本?我刚刚检查了v3的CHM文件和在线文档,只有WinGetClientSize。见autoitscript.com/autoit3/docs/functions/WinGetClientSize.htm。但是您对 Opt 功能是正确的。我忽略了 AutoItSetOption 中提到的替代别名:autoitscript.com/autoit3/docs/functions/AutoItSetOption.htm
    • 这些是 AutoIt 的文档,而不是 AutoItX。 AutoItX 是(来自帮助文件):“AutoIt v3 的 DLL 版本,它通过 ActiveX/COM 和 DLL 接口提供 AutoIt 功能的子集。” AutoItX 及其帮助文件包含在 AutoIt 主下载中。我指的是版本 3.3.12.0
    【解决方案3】:

    查看 Python 的 Windows 扩展中的 win32gui module。它可能会提供您正在寻找的一些功能。

    【讨论】:

      【解决方案4】:

      我更新了 GREAT @DZinX 代码,添加了窗口的标题/文本:

      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)
      

      【讨论】:

        【解决方案5】:

        我修改了某人的代码,

        这很好地帮助运行其他应用程序并获取 PID,

        import win32process
        import subprocess
        import win32gui
        import time 
         
        def get_hwnds_for_pid (pid):
          def callback (hwnd, hwnds):
            if win32gui.IsWindowVisible (hwnd) and win32gui.IsWindowEnabled (hwnd):
              _, found_pid = win32process.GetWindowThreadProcessId (hwnd)
              if found_pid == pid:
                hwnds.append (hwnd)
            return True
            
          hwnds = []
          win32gui.EnumWindows (callback, hwnds)
          return hwnds
        
        # This the process I want to get windows size. 
        notepad = subprocess.Popen ([r"C:\\Users\\dniwa\\Adb\\scrcpy.exe"]) 
        time.sleep (2.0)
        
        while True: 
          for hwnd in get_hwnds_for_pid (notepad.pid):
            rect = win32gui.GetWindowRect(hwnd)
            print(hwnd, "=>", win32gui.GetWindowText (hwnd))
        
            # You need to test if your resolution really get exactly because mine is doesn't . 
            # I use . 16:9 Monitor , Calculate the percent using this calculations , , (x * .0204082) and (y * .0115774)
            print((hwnd, (rect[2] - rect[0], rect[3] - rect[1])))
            x = rect[2] - rect[0]
            y = rect[3] - rect[1]
            print(type(x), type(y))
            time.sleep(1)
        

        【讨论】:

          猜你喜欢
          • 2021-04-26
          • 2018-07-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-03
          • 2012-07-31
          相关资源
          最近更新 更多