第一步,添加引用:
备注:d-display,w-window,s-DefaultScreen(d)下同
#include <X11/Xlib.h>
第二步,添加颜色:
XColor color;
Colormap colormap;
char green[] = "#00FF00";
char yellow[] = "#FFFF00";
char blue[] = "#0000FF";
colormap = DefaultColormap(d, 0);
XParseColor(d, colormap, blue, &color);
XAllocColor(d, colormap, &color);
第三步,创建窗口时改变背景色:
w = XCreateSimpleWindow(d,
RootWindow(d, s),
x,
y,
width,
height,
win_border_width,
BlackPixel(d, s),
color.pixel);
第四步:设置前景色:
GC gc = XCreateGC(d, w, 0, NULL);
ParseColor(d, colormap, green, &color);
XAllocColor(d, colormap, &color);
XSetForeground(d, gc, color.pixel);
......
XFillRectangle(d, w, gc, 1, 1, 90, 90);
调整窗口位置到中间(在生成窗口以后调整,代码适用于只有一个窗口):
int screenCount=XScreenCount(d);
Screen* screent = XScreenOfDisplay(d, screenCount-1);
int width_screen = screent->width;
int height_screen = screent->height;
XMoveWindow(d,w,(width_screen-width)/2,(height_screen-height)/2);
效果图展示: