2012-01-25 35 views
-1

我正在寻找一种方法来查找指定的窗口尺寸(宽度&高度)。到目前为止,我一直在使用AutoIt函数,只是编写窗口/框架名称的一部分,它的工作方式就像我想要的那样。唯一的问题是,它只适用于MS Windows。我需要它来跨平台工作(windows & linux)。查找应用程序的宽度和高度 - 跨平台

由于平台如此不同,如果系统是windows系统,而Linux系统则需要两个脚本。我不想依赖额外的程序(如AutoIt)。我不希望它在脚本中被“硬编码”选择什么框架。我需要/希望它能够像AutoIt一样工作,通过填充框架名称/或其部分。

+1

看看http://stackoverflow.com/questions/151846/get-other-running-processes-window-sizes-in-python – yurib

+0

@SLACKY:你是什么意思的程序或框架的维度?这是你感兴趣的窗口吗(如yurib建议)?或者,你想知道屏幕分辨率? – gsbabil

回答

0

我想通了,我怎么能在窗口做到这一点,得益于yurib ..

import win32con 
import win32gui 

def inText(haystack, needle, n): 
    parts= haystack.split(needle, n+1) 
    if len(parts)<=n+1: 
     return False 
    if len(haystack)-len(parts[-1])-len(needle): 
     return True 

def isRealWindow(hWnd): 
    '''Return True if 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(text): 
    '''Return a list of tuples (handler, (width, height)) for each real window.''' 
    def callback(hWnd, extra): 
     if not isRealWindow(hWnd): 
      return 
     title = win32gui.GetWindowText(hWnd) 
     rect = win32gui.GetWindowRect(hWnd) 
     isFrame = inText(title, text, 0) 
     if(isFrame): 
      windows.append((title, rect[2] - rect[0], rect[3] - rect[1], rect[0],rect[1])) 
    windows = [] 
    win32gui.EnumWindows(callback, windows) 
    return windows 

def findWindow(text): 
    window = getWindowSizes(text) 
    name = window[0][0] 
    w = window[0][1] 
    h = window[0][2] 
    x = window[0][3] 
    y = window[0][4] 

    return name,w,h,x,y 

现在我需要知道我可以做这样的事情在Linux中。任何提示?