2016-09-17 25 views

回答

2

顯然,解決這個問題的第一步是確定當前焦點所在的窗口。要做到這一點,你可能會使用Xlib的XGetInputFocus()函數。之後,使用XGetWindowAttributes()來獲取窗口的位置和大小(甚至可以獲得更多關於窗口的信息)。

0

謝謝@Striezel,您的反饋指出我的方向正確。調查您的解決方案後,我碰到這個帖子:Xlib: XGetWindowAttributes always returns 1x1?

調整從@Doug答案一點點我有以下的,這似乎是按預期工作:

Window getToplevelParent(Display* display, Window window) 
{ 
    Window parentWindow; 
    Window rootWindow; 
    Window* childrenWindows; 
    unsigned int numberOfChildren; 

    while (1) { 
     if (XQueryTree(display, window, &rootWindow, &parentWindow, &childrenWindows, 
         &numberOfChildren) == 0) { 
      qCritical("ImageGrabber::getToplevelParent: XQueryTree Error"); 
      return 0; 
     } 
     if (childrenWindows) { 
      XFree(childrenWindows); 
     } 
     if (window == rootWindow || parentWindow == rootWindow) { 
      return window; 
     } else { 
      window = parentWindow; 
     } 
    } 
} 

QRect ImageGrabber::getActiveWindowRect() 
{ 

    Display* display = XOpenDisplay(NULL); 
    Window focusWindow, parentOfFocusedWindow; 
    XWindowAttributes attrributes; 
    int revert; 

    XGetInputFocus(display, &focusWindow, &revert); 
    parentOfFocusedWindow = getToplevelParent(display, focusWindow); 
    if (!parentOfFocusedWindow) { 
     qCritical("ImageGrabber::getActiveWindowRect: Unable to get window, returning screen."); 
     return getCurrectScreenRect(); 
    } 

    XGetWindowAttributes(display, parentOfFocusedWindow, &attrributes); 
    return QRect(attrributes.x, attrributes.y, attrributes.width, attrributes.height); 
} 
相關問題