我有一个使用XLib的代码,该代码可以正常工作:

    Display *display = XOpenDisplay(NULL);
    int screen_num = DefaultScreen(display);
    Window root_win = RootWindow ( display, screen_num );

    // Create a GC (Graphics Context) for the line
    XGCValues gc_val;
    gc_val.function           = GXxor;
    gc_val.plane_mask         = AllPlanes;
    gc_val.foreground         = WhitePixel(display, screen_num);
    gc_val.background         = BlackPixel(display, screen_num);
    gc_val.line_width         = 4;
    gc_val.line_style         = LineSolid;
    gc_val.cap_style          = CapButt;
    gc_val.join_style         = JoinMiter;
    gc_val.fill_style         = FillOpaqueStippled;
    gc_val.fill_rule          = WindingRule;
    gc_val.graphics_exposures = False;
    gc_val.clip_x_origin      = 0;
    gc_val.clip_y_origin      = 0;
    gc_val.clip_mask          = None;
    gc_val.subwindow_mode     = IncludeInferiors;

    GC gc_line = XCreateGC(display, root_win, GCFunction | GCPlaneMask |  GCForeground | GCBackground | GCLineWidth | GCLineStyle |
                GCCapStyle  | GCJoinStyle  |  GCFillStyle  |  GCFillRule  |  GCGraphicsExposures |
                GCClipXOrigin |  GCClipYOrigin  |  GCClipMask  | GCSubwindowMode, &gc_val);

    //XSetForeground(display, gc_line, some_color);
    XDrawRectangle(display, root_win, gc_line, 50, 50, 400, 400);

    XFlush(display);

...并且我需要对XCB做同样的事情。

我用XCB编写了尽可能多的类似代码:
xcb_connection_t    *c;
xcb_screen_t        *screen;
xcb_generic_event_t *e;
uint32_t             mask = 0;
xcb_gcontext_t    gc = { 0 };    /* the returned default graphic context */
xcb_drawable_t draw;

xcb_rectangle_t rectangles[] = {
  {0, 0, 100, 100},
};

c = xcb_connect (NULL, NULL);

/* get the first screen */
screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;

gc = xcb_generate_id (c);

/* root window */
draw = screen->root;

mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FUNCTION | XCB_GC_CAP_STYLE | XCB_GC_JOIN_STYLE | XCB_GC_FILL_STYLE | XCB_GC_FILL_RULE | XCB_GC_GRAPHICS_EXPOSURES
        | XCB_GC_CLIP_ORIGIN_X | XCB_GC_CLIP_ORIGIN_Y | XCB_GC_CLIP_MASK | XCB_GC_SUBWINDOW_MODE;
uint32_t values[] = {
    screen->black_pixel,
    screen->white_pixel,
    XCB_GX_XOR,
    XCB_CAP_STYLE_BUTT,
    XCB_JOIN_STYLE_MITER,
    XCB_FILL_STYLE_OPAQUE_STIPPLED,
    XCB_FILL_RULE_WINDING,
    1,
    0,
    0,
    XCB_NONE,
    XCB_SUBWINDOW_MODE_INCLUDE_INFERIORS
};

xcb_create_gc (c, gc, draw, mask, values);
xcb_map_window (c, draw);
xcb_flush(c);

while (1)
{
    xcb_poly_rectangle (c, draw, gc, 1, rectangles);
    xcb_flush(c);
}

但是在程序开始时我看不到矩形。

我做错了什么?

最佳答案

我不太清楚,但是在搜索和修改您的代码后,我可以使用以下简单代码在根窗口上绘制:

#include <stdlib.h>
#include <stdio.h>
#include <xcb/xcb.h>

int
main ()
{
  xcb_connection_t    *c;
  xcb_screen_t        *screen;
  xcb_generic_event_t *e;
  uint32_t             mask = 0;
  xcb_gcontext_t    gc = { 0 };    /* the returned default graphic context */
  xcb_drawable_t draw;

  xcb_rectangle_t rectangles[] = {
    { 200, 200, 400, 400 },
  };

  c = xcb_connect (NULL, NULL);

  /* get the first screen */
  screen = xcb_setup_roots_iterator ( xcb_get_setup ( c ) ).data;

  gc = xcb_generate_id ( c );

  /* root window */
  draw = screen->root;

  mask = XCB_GC_FUNCTION | XCB_GC_FOREGROUND | XCB_GC_BACKGROUND |  XCB_GC_LINE_WIDTH| XCB_GC_LINE_STYLE | XCB_GC_GRAPHICS_EXPOSURES;
  uint32_t values[] = {
      XCB_GX_XOR,
      screen->white_pixel,
      screen->black_pixel,
      1,
      XCB_LINE_STYLE_ON_OFF_DASH,
      0
  };

  xcb_create_gc ( c, gc, draw, mask, values );
  xcb_poly_rectangle (c, draw, gc, 3, rectangles);
  xcb_map_window (c, draw);
  xcb_flush(c);
  pause();
  return 0;
}

我认为您的问题是如何根据需要更新根窗口上的图形...(也许)。如果找到解决方案,请向您显示代码。

我用于测试:
 Xephyr -br -noreset -screen "1024x640" :1&
 DISPLAY=:1.0 ./mylittletest

关于c++ - 如何使用XCB在根窗口上绘制?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28108697/

10-11 00:39