简体   繁体   English

如何在 C++ 中获得 Linux 上当前聚焦窗口的几何?

[英]How to get Geometry of currently focused Window on Linux in C++?

Is there a way to get the Geometry of the currently focused window under Linux?有没有办法在Linux下获取当前聚焦窗口的几何图形? I just need the position (x and y) and size (width and height) of what ever Window is currently having focus or being on top of the desktop.我只需要 Window 当前具有焦点或位于桌面顶部的位置(x 和 y)和大小(宽度和高度)。

I want to use this information in my QT application to take a screen shot of this window.我想在我的 QT 应用程序中使用此信息来拍摄此窗口的屏幕截图。

Obviously, the first step to solve that problem would be to determine the windows that is currently in focus.显然,解决该问题的第一步是确定当前处于焦点的窗口。 To do that, you might employ Xlib's XGetInputFocus() function.为此,您可以使用 Xlib 的XGetInputFocus()函数。 After that, use XGetWindowAttributes() to get the position and the size of the window (and even some more information about the window).之后,使用XGetWindowAttributes()获取窗口的位置和大小(甚至更多关于窗口的信息)。

Thanks @Striezel, your feedback pointed me in the right direction.谢谢@Striezel,您的反馈为我指明了正确的方向。 After investigating your solution, I run into this post: Xlib: XGetWindowAttributes always returns 1x1?在调查了您的解决方案后,我遇到了这篇文章: Xlib:XGetWindowAttributes 总是返回 1x1?

Tweaking the answer from @Doug a little bit I've got following, which seems to be working as expected:稍微调整@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);
} 

You could use libxdo你可以使用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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM