根据以下解释,我编写了一个使用x11的小型C程序,并应该具有自定义的色彩映射表。然而,我遇到了以下错误:
这是完整的xexample.c代码,使用“gcc xexample.c -lX11 -o xexample”进行编译。
非常感谢您的时间!
X Error of failed request: BadMatch (invalid parameter attributes)
Major opcode of failed request: 78 (X_CreateColormap)
Serial number of failed request: 15
Current serial number in output stream: 18
这是完整的xexample.c代码,使用“gcc xexample.c -lX11 -o xexample”进行编译。
/* include the X library headers */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
/* include some silly stuff */
#include <stdio.h>
#include <stdlib.h>
/* here are our X variables */
Display *dis;
int screen;
Window win;
GC gc;
Colormap cmap;
/* here are our X routines declared! */
void init_x();
void close_x();
void redraw();
void create_colormap();
int main () {
XEvent event; /* the XEvent declaration !!! */
KeySym key; /* a dealie-bob to handle KeyPress Events */
char text[255]; /* a char buffer for KeyPress Events */
init_x();
create_colormap();
/* look for events forever... */
while(1) {
/* get the next event and stuff it into our event variable.
Note: only events we set the mask for are detected!
*/
XNextEvent(dis, &event);
if (event.type==Expose && event.xexpose.count==0) {
/* the window was exposed redraw it! */
redraw();
}
if (event.type==KeyPress&&
XLookupString(&event.xkey,text,255,&key,0)==1) {
/* use the XLookupString routine to convert the invent
KeyPress data into regular text. Weird but necessary...
*/
if (text[0]=='q') {
close_x();
}
printf("You pressed the %c key!\n",text[0]);
}
if (event.type==ButtonPress) {
/* tell where the mouse Button was Pressed */
int x=event.xbutton.x,
y=event.xbutton.y;
strcpy(text,"X is FUN!");
int colortje = rand()%event.xbutton.x%255;
printf("colortje: %d\n", colortje);
XSetForeground(dis,gc,colortje);
XDrawString(dis,win,gc,x,y, text, strlen(text));
}
}
}
void init_x() {
/* get the colors black and white (see section for details) */
unsigned long black,white;
dis=XOpenDisplay((char *)0);
screen=DefaultScreen(dis);
black=BlackPixel(dis,screen),
white=WhitePixel(dis,screen);
win=XCreateSimpleWindow(dis,DefaultRootWindow(dis),0,0,
300, 300, 5,black, white);
XSetStandardProperties(dis,win,"Howdy","Hi",None,NULL,0,NULL);
XSelectInput(dis, win, ExposureMask|ButtonPressMask|KeyPressMask);
gc=XCreateGC(dis, win, 0,0);
XSetBackground(dis,gc,white);
XSetForeground(dis,gc,black);
XClearWindow(dis, win);
XMapRaised(dis, win);
};
void close_x() {
XFreeGC(dis, gc);
XDestroyWindow(dis,win);
XCloseDisplay(dis);
XFreeColormap(dis,cmap);
exit(1);
};
void redraw() {
XClearWindow(dis, win);
};
void create_colormap() {
int i;
XColor tmp[255];
for(i=0;i<255;i++) {
tmp[i].pixel=i;
tmp[i].flags=DoRed|DoGreen|DoBlue;
tmp[i].red=i*256;
tmp[i].blue=i*256;
tmp[i].green=i*256;
}
cmap=XCreateColormap(dis,RootWindow(dis,screen),
DefaultVisual(dis,screen),AllocAll);
XStoreColors(dis,cmap,tmp,255);
XSetWindowColormap(dis,win,cmap);
};
非常感谢您的时间!
.pixel
和.flags
解决了这个问题。 - Kingsley