在Xlib中,XWindowAttributes结构包含指向窗口所在屏幕的指针。但是在XCB对应的结构(xcb_get_window_attributes_reply_t)中没有这样的成员。
我该怎么办?
我该怎么办?
xcb_screen_t* screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data;
请您自行前往上述链接查看相关技术内容。我认为没有直接获取窗口屏幕的方法。
你可以找到你的窗口的根窗口祖先,然后迭代所有屏幕,直到找到拥有你的根窗口的那个屏幕。
正如@peoro所指出的,你应该先获取包含该窗口的屏幕的根窗口,然后循环遍历所有屏幕,直到找到与之前提到的根窗口id匹配的屏幕。
这里是一个例子:
/* compile: cc main.c -o main -lxcb */
#include <stdio.h>
#include <stdlib.h>
#include <xcb/xcb.h>
#include <xcb/xproto.h>
int
main(void)
{
xcb_connection_t *conn;
xcb_window_t window;
xcb_get_geometry_cookie_t geometry_cookie;
xcb_get_geometry_reply_t *geometry_reply;
xcb_screen_t *screen;
xcb_screen_iterator_t it;
/* put the id of the window here */
window = 0x1000002;
conn = xcb_connect(NULL, NULL);
geometry_cookie = xcb_get_geometry_unchecked(conn, window);
geometry_reply = xcb_get_geometry_reply(conn, geometry_cookie, NULL);
it = xcb_setup_roots_iterator(xcb_get_setup(conn));
for (screen = it.data; it.rem != 0; xcb_screen_next(&it), screen = it.data) {
if (screen->root == geometry_reply->root) {
printf("window %lu found on screen with %hux%hu dimensions\n",
window, screen->width_in_pixels, screen->height_in_pixels);
break;
}
}
free(geometry_reply);
xcb_disconnect(conn);
return 0;
}