【发布时间】:2011-04-17 22:30:55
【问题描述】:
如何使用普通的 ol' xlib(或全新的 XCB)获取相对于根窗口(即整个屏幕)的顶级窗口位置?
【问题讨论】:
如何使用普通的 ol' xlib(或全新的 XCB)获取相对于根窗口(即整个屏幕)的顶级窗口位置?
【问题讨论】:
使用 XTranslateCoordinates(或 xcb 等效项)将窗口上的 0,0 转换为根窗口坐标。
【讨论】:
使用 Xlib:
XWindowAttributes xwa;
XGetWindowAttributes(display, window, &xwa);
printf("%d %d\n", xwa.x, xwa.y);
XWindowAttributes 还附带许多其他信息。见here。
【讨论】:
XGetWindowAttributes 返回的结构的 x,y 分量相对于窗口父级的原点。这与相对于屏幕左上角的位置不同。
调用 XTranslateCoordinates 传递根窗口和 0,0 给出窗口相对于屏幕的坐标。
我发现如果我写的话:
int x, y;
Window child;
XWindowAttributes xwa;
XTranslateCoordinates( display, window, root_window, 0, 0, &x, &y, &child );
XGetWindowAttributes( display, window, &xwa );
printf( "%d %d\n", x - xwa.x, y - xwa.y );
printf 显示的值是那些,如果传递给 XMoveWindow,则将窗口保持在其当前位置。因此,这些坐标被合理地认为是窗口的位置。
【讨论】:
这就是你要用 XCB 做的事情:
auto geom = xcb_get_geometry(xcb_connection(), window);
auto offset = xcb_translate_coordinate(xcb_connection(), window, rootwin, geom->x, geom->y);
offset->dst_x // top-level window's x offset on the screen
offset->dst_y // top-level window's y offset on the screen
geom->width // top-level window's width
geom->height // top-level window's height
【讨论】: