使用XLib绘制XBM,获得黑色矩形

时间:2011-01-13 22:57:08

标签: xlib

我正在尝试做一些我认为非常简单的事情:使用XLib在简单的X窗口上绘制硬编码的XBM图像。我使用以下代码,但只在窗口的左上角而不是图像中获得黑色矩形。帮助

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

/* From http://commons.wikimedia.org/wiki/File:2008-07-25_Geese_over_00.svg
   Creative Commons BY-SA */
#define goose_width 32
#define goose_height 31
static const unsigned char goose_bits[] = {
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x1c,
   0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x80, 0x0f,
   0x00, 0x00, 0xc0, 0x0f, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xe0, 0x07,
   0x00, 0x00, 0xf0, 0x03, 0x00, 0x00, 0xf8, 0x01, 0x00, 0x00, 0xfc, 0x01,
   0x00, 0x00, 0xfc, 0x01, 0x00, 0x07, 0xfe, 0x01, 0x10, 0x07, 0xff, 0x00,
   0x78, 0x0e, 0xff, 0x00, 0x80, 0x9f, 0x7f, 0x00, 0x00, 0xfe, 0x3f, 0x00,
   0x00, 0xfc, 0x1f, 0x00, 0x00, 0xf8, 0x1f, 0x00, 0x00, 0xf0, 0x0f, 0x00,
   0x00, 0xe0, 0x3f, 0x00, 0x00, 0x80, 0xff, 0x01, 0x00, 0x00, 0xf8, 0x01,
   0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00 };

void windowinit(Display *display, Window *window) {
    int screennum = DefaultScreen(display);
    long background = WhitePixel(display, screennum);
    long foreground = BlackPixel(display, screennum);
    *window = XCreateSimpleWindow(display, DefaultRootWindow(display), 10, 10, 800, 600, 2, foreground, background);
    XMapRaised(display, *window);
    XSync(display, False);

    /* Register for events */
    XSelectInput(display, *window, ButtonPressMask|ButtonReleaseMask|ExposureMask|ButtonMotionMask|KeyPressMask);
}

int main(void) {
    Display* display;
    Window window;
    GC gc;
    Pixmap i;

    /* Open display from $DISPLAY, handle errors */
    if(!(display = XOpenDisplay(NULL))) {
        fprintf(stderr, "Cannot connect to X display\n");
        return EXIT_FAILURE;
    }

    /* Set up window */
    windowinit(display, &window);

    /* Creat GC, handel errors */
    if((int)(gc = XCreateGC(display, window, 0, 0)) < 0) {
        fprintf(stderr, "XCreateGC: \n");
        return EXIT_FAILURE;
    }

    /* Draw image -- why does this draw a black rectangle ?? */
    i = XCreateBitmapFromData(display, DefaultRootWindow(display), goose_bits, goose_width, goose_height);
    XCopyPlane(display, i, window, gc, 0, 0, goose_width, goose_height, 0, 0, 1);

    XSync(display, False);
    sleep(3);

    /* Cleanup */
    XFreeGC(display, gc);
    XDestroyWindow(display, window);
    XCloseDisplay(display);

    return EXIT_SUCCESS;
}

1 个答案:

答案 0 :(得分:1)

我发现了问题。我需要打电话:

XSetBackground(display,gc,WhitePixel(display,DefaultScreen(display)));