有没有办法在Linux下获取当前焦点窗口的Geometry?我只需要窗口当前具有焦点或位于桌面顶部的位置(x和y)和大小(宽度和高度)。
我想在QT应用程序中使用此信息来拍摄此窗口的屏幕截图。
答案 0 :(得分:2)
显然,解决该问题的第一步是确定当前焦点的窗口。为此,您可以使用Xlib的XGetInputFocus()功能。之后,使用XGetWindowAttributes()获取窗口的位置和大小(甚至还有关于窗口的更多信息)。
答案 1 :(得分:1)
您可以使用libxdo
/**
* Like xdo_get_focused_window, but return the first ancestor-or-self window *
* having a property of WM_CLASS. This allows you to get the "real" or
* top-level-ish window having focus rather than something you may not expect
* to be the window having focused.
*
* @param window_ret Pointer to a window where the currently-focused window
* will be stored.
*/
int xdo_get_focused_window_sane(const xdo_t *xdo, Window *window_ret);
/**
* Get a window's size.
*
* @param wid the window to query
* @param width_ret pointer to unsigned int where the width is stored.
* @param height_ret pointer to unsigned int where the height is stored.
*/
int xdo_get_window_size(const xdo_t *xdo, Window wid, unsigned int *width_ret,
unsigned int *height_ret);
答案 2 :(得分: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);
}