2012-01-25 36 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中。任何提示?