2013-01-05 69 views
1

這是Linux。我創建了一個窗口,我想將其背景顏色更改爲綠色。這就是我的代碼的樣子:用C++更改Xlib窗口背景顏色

Window xwin = XCreateSimpleWindow(dis, 
            DefaultRootWindow(dis), 
            0, 0, 
            500, 300, 
            0, 
            WhitePixel(dis, 0), 
            WhitePixel(dis, 0)); 
GC gc = XCreateGC(dis, xwin, 0, NULL); 
XColor color; 
Colormap colormap; 
char green[] = "#00FF00"; 

colormap = DefaultColormap(dis, 0); 
XParseColor(dis, colormap, green, &color); 
XAllocColor(dis, colormap, &color); 

XSetBackground(dis, gc, color.pixel); 

XMapWindow(dis, xwin); 
XFlush(dis); 

我看到的窗口是白色的。使用X11可以在Linux中更改窗口背景顏色嗎?謝謝!

回答

3

如果你只是想要一個綠色的背景,XCreateSimpleWindow的最後一個參數是背景色等等...

XColor color; 
    Colormap colormap; 
    char green[] = "#00FF00"; 

    colormap = DefaultColormap(dis, 0); 
    XParseColor(dis, colormap, green, &color); 
    XAllocColor(dis, colormap, &color); 


    Window xwin = XCreateSimpleWindow(dis, 
        DefaultRootWindow(dis), 
        0, 0, 
        500, 300, 
        0, 
        WhitePixel(dis, 0), 
        color.pixel); 

XMapWindow(dis, xwin); 
XFlush(dis);