| Unix Content | Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
|
| hi, I write a piece of code to display a xbm image onto a window, but it does not work. int main() { Display* display = XOpenDisplay(0); int screen = DefaultScreen(display); Window root = RootWindow(display, screen); Window wnd = ::XCreateSimpleWindow(display, root,0, 0, 200, 200, 2, 0, 0xffffff); GC gc = ::XCreateGC(display, wnd, 0, 0); std::cout<<"Window = "< { std::cout<<"bitmap = "< 0,0, 1); std::cout<<"XCopyPlane = "< ::XMapWindow(display, wnd); ::XFlush(display); ::sleep(5); } the program outputs XCopyPlane = 1, but the image is not printed on the window, I don't know the reason, could where am i wrong? Thanks in advance. Jinhao |
|
#2
|
| Jinhao wrote: > hi, I write a piece of code to display a xbm image onto a window, but > it does not work. > > int main() > { > Display* display = XOpenDisplay(0); I suggest using: if(NULL == display) { /* print some error message */ } Otherwise the macros and calls below will segfault. > int screen = DefaultScreen(display); > Window root = RootWindow(display, screen); > Window wnd = ::XCreateSimpleWindow(display, root,0, 0, 200, 200, 2, > 0, 0xffffff); It's generally better to use BlackPixel() or WhitePixel() rather than hard coding 0xffffff. > GC gc = ::XCreateGC(display, wnd, 0, 0); > std::cout<<"Window = "< Check for BitmapSuccess - not 0. Also, you probably want to use wnd instead of root. > &height, &bitmap, &x_hot, &y_hot)) > { > std::cout<<"bitmap = "< > 0,0, 1); > std::cout<<"XCopyPlane = "< > > ::XMapWindow(display, wnd); > ::XFlush(display); > > ::sleep(5); You're missing an integer to return from main. You could #include It would help to turn on -Wall if you're using gcc. > } > > the program outputs XCopyPlane = 1, but the image is not printed on > the window, I don't know the reason, could where am i wrong? > You need to keep the bitmap around until you receive an Expose event (after you map the window), and then XCopyPlane. My tutorial may help: http://www.xmission.com/~georgeps/do...eginner-7.html George |
|
#3
|
| On 9月18日, 下午12时31分, GPS > Jinhao wrote: > > hi, I write a piece of code to display a xbm image onto a window, but > > it does not work. > > > int main() > > { > > Display* display = XOpenDisplay(0); > > I suggest using: > > if(NULL == display) { > /* print some error message */ > } > > Otherwise the macros and calls below will segfault. > > > int screen = DefaultScreen(display); > > Window root = RootWindow(display, screen); > > Window wnd = ::XCreateSimpleWindow(display, root,0, 0, 200, 200, 2, > > 0, 0xffffff); > > It's generally better to use BlackPixel() or WhitePixel() rather than > hard coding 0xffffff. > > > GC gc = ::XCreateGC(display, wnd, 0, 0); > > std::cout<<"Window = "< > > Pixmap bitmap; > > unsigned width, height; > > int x_hot, y_hot; > > if(0 == XReadBitmapFile(display, root, "Test.xbm", &width, > > Check for BitmapSuccess - not 0. > > Also, you probably want to use wnd instead of root. > > > &height, &bitmap, &x_hot, &y_hot)) > > { > > std::cout<<"bitmap = "< > > 0,0, 1); > > std::cout<<"XCopyPlane = "< > > > ::XMapWindow(display, wnd); > > ::XFlush(display); > > > ::sleep(5); > > You're missing an integer to return from main. You could #include > > > It would help to turn on -Wall if you're using gcc. > > > } > > > the program outputs XCopyPlane = 1, but the image is not printed on > > the window, I don't know the reason, could where am i wrong? > > You need to keep the bitmap around until you receive an Expose event > (after you map the window), and then XCopyPlane. > > My tutorial may help:http://www.xmission.com/~georgeps/do...ls/Xlib_Beginn... > > George Thanks for your replay. I modified my program. int main() { Display* display = XOpenDisplay(NULL); if(display == NULL) { return 0; } int screen = DefaultScreen(display); Window root = RootWindow(display, screen); Window wnd = ::XCreateSimpleWindow(display, root,0, 0, 200, 200, 2, 0, 0xffffff); XSelectInput(display, wnd, ExposureMask); GC gc = ::XCreateGC(display, wnd, 0, 0); Pixmap bitmap; unsigned width, height; int x_hot, y_hot; if(BitmapSuccess != XReadBitmapFile(display, wnd, "Test.xbm", &width, &height, &bitmap, &x_hot, &y_hot)) { return 0; } ::XMapWindow(display, wnd); ::XFlush(display); while(true) { XEvent evt; XNextEvent(display, &evt); if(evt.type == Expose) { std::cout<<"Expose"< 0,0, 1); std::cout<<"XCopyPlane = "< } } return 0; } The program prints Expose XCopyPlane = 1 that means the program is right? but the window displays a black rectangle... |