有沒有辦法在Linux下獲得當前焦點窗口的幾何圖形?我只需要窗口當前所關注或正在桌面上的位置(x和y)和大小(寬度和高度)。如何在C++中獲得Linux上當前關注窗口的幾何?
我想在我的QT應用程序中使用此信息來拍攝該窗口的屏幕截圖。
有沒有辦法在Linux下獲得當前焦點窗口的幾何圖形?我只需要窗口當前所關注或正在桌面上的位置(x和y)和大小(寬度和高度)。如何在C++中獲得Linux上當前關注窗口的幾何?
我想在我的QT應用程序中使用此信息來拍攝該窗口的屏幕截圖。
顯然,解決這個問題的第一步是確定當前焦點所在的窗口。要做到這一點,你可能會使用Xlib的XGetInputFocus()函數。之後,使用XGetWindowAttributes()來獲取窗口的位置和大小(甚至可以獲得更多關於窗口的信息)。
謝謝@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);
}