2011-01-13 71 views
2

我想要做一些我認爲會很簡單的事情:在使用XLib的簡單X窗口上繪製硬編碼的XBM圖像。我正在使用下面的代碼,但只是在窗口的左上角,而不是圖像的黑色矩形。幫幫我?使用XLib繪製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

我發現了這個問題。我需要調用:

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