Xlib:XGetWindowAttributes总是返回1x1? [英] Xlib: XGetWindowAttributes always returns 1x1?

查看:369
本文介绍了Xlib:XGetWindowAttributes总是返回1x1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要有当前聚焦窗口的宽度和高度。窗口的选择像一个魅力,而高度和宽度总是返回1。

I'd like to have width and height of the currently focussed window. The selection of the window works like a charm whereas the height and width are always returning 1.

#include <X11/Xlib.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    Display *display;
    Window focus;
    XWindowAttributes attr;
    int revert;

    display = XOpenDisplay(NULL);
    XGetInputFocus(display, &focus, &revert);
    XGetWindowAttributes(display, focus, &attr);
    printf("[0x%x] %d x %d\n", (unsigned)focus, attr.width, attr.height);

    return 0;
}

这不是真正的窗口,而是当前活动的组件文本框或按钮?)为什么会有1x1的大小呢?如果是这样,我如何得到应用程序的主窗口包含这个控件?意味着...顶层窗口,除了根窗口之外的最顶层窗口。

Is this not the "real" window but the currently active component (like a textbox or a button?) And why would it have the size of 1x1 anyways then? If this is the case, how do i get the main window of the application containig this control? Means... kinda the top-level window, the top-most window except the root window.

PS:不知道它是否真的很重要;我使用Ubuntu 10.04 32和64位。

PS: Don't know whether it's really important; I use Ubuntu 10.04 32 and 64 bit.

推荐答案

你是对的 - 你看到一个子窗口。 GTK应用程序,特别是在真实窗口下创建一个子窗口,它总是1x1,并且当应用程序具有焦点时总是获得焦点。如果你只是使用GNOME终端运行你的程序,你将总是看到一个GTK应用程序的焦点(终端)。

You're right - you're seeing a child window. GTK applications, in particular, create a child window under the "real" window, which is always 1x1, and that always gets the focus when the application has the focus. If you're just running your program using the GNOME terminal, you'll always be seeing a GTK application with the focus (the terminal).

如果你运行你的程序在这种方式下,非GTK程序碰巧有焦点,然后这不会发生,但你仍然可能最终找到一个具有焦点的子窗口,而不是顶级窗口。 (这样做的一种方法是在你的程序之前运行 sleep sleep 4; ./my_program 让您有机会更改焦点。)

If you run your program in such a way that a non-GTK program happens to have the focus, then this doesn't happen, but you could still end up finding a child window with the focus instead of the top-level window. (One way of doing this is to run sleep before your program like this: sleep 4; ./my_program - this gives you a chance to change the focus.)

要查找顶级窗口,我认为 XQueryTree 帮助 - 它返回父窗口。

To find the top-level window, I think XQueryTree will help - it returns the parent window.

这对我有用:

#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>

/*
Returns the parent window of "window" (i.e. the ancestor of window
that is a direct child of the root, or window itself if it is a direct child).
If window is the root window, returns window.
*/
Window get_toplevel_parent(Display * display, Window window)
{
     Window parent;
     Window root;
     Window * children;
     unsigned int num_children;

     while (1) {
         if (0 == XQueryTree(display, window, &root,
                   &parent, &children, &num_children)) {
             fprintf(stderr, "XQueryTree error\n");
             abort(); //change to whatever error handling you prefer
         }
         if (children) { //must test for null
             XFree(children);
         }
         if (window == root || parent == root) {
             return window;
         }
         else {
             window = parent;
         }
     }
}

int main(int argc, char *argv[])
{
    Display *display;
    Window focus, toplevel_parent_of_focus;
    XWindowAttributes attr;
    int revert;

    display = XOpenDisplay(NULL);
    XGetInputFocus(display, &focus, &revert);
    toplevel_parent_of_focus = get_toplevel_parent(display, focus);
    XGetWindowAttributes(display, toplevel_parent_of_focus, &attr);
    printf("[0x%x] %d x %d\n", (unsigned)toplevel_parent_of_focus, 
       attr.width, attr.height);

    return 0;
}

这篇关于Xlib:XGetWindowAttributes总是返回1x1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆