Xlib 中的窗口位置

发布于 2024-09-25 06:56:11 字数 57 浏览 4 评论 0原文

如何使用普通的 ol' xlib (或全新的 XCB)获取相对于根窗口(即整个屏幕)的顶级窗口位置?

How to get top-level window position relative to root window (i.e. whole screen) using plain ol' xlib (or brand new XCB)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月牙弯弯 2024-10-02 06:56:12

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 则将窗口保持在当前位置的值。因此,这些坐标被合理地认为是窗口的位置。

The x,y components of the structure returned by XGetWindowAttributes are relative to the origin of the window's parent. That's not the same as relative to the top left of the screen.

Calling XTranslateCoordinates passing the root window and 0,0 gives coordinates of the window relative to the screen.

I found that if I write:

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 );

The values displayed by the printf are those which, if passed to XMoveWindow, keep the window at its current position. Thus those coordinates are reasonably considered to be the position of the window.

活泼老夫 2024-10-02 06:56:12

使用 Xlib:

XWindowAttributes xwa;
XGetWindowAttributes(display, window, &xwa);
printf("%d %d\n", xwa.x, xwa.y);

XWindowAttributes 还附带许多其他信息。请参阅此处

Using Xlib:

XWindowAttributes xwa;
XGetWindowAttributes(display, window, &xwa);
printf("%d %d\n", xwa.x, xwa.y);

There are also lots of other informations that come with XWindowAttributes. See here.

白龙吟 2024-10-02 06:56:12

使用 XTranslateCoordinates(或 xcb 等效项)将窗口上的 0,0 转换为根窗口坐标。

Use XTranslateCoordinates (or xcb equivalent) to translate 0,0 on the window to root window coordinates.

鹿! 2024-10-02 06:56:12

这就是您将使用 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

This is what you would do with 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文