hello.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. CS 349 Hello X Windows
  3. - - - - - - - - - - - - - - - - - - - - - -
  4. Commands to compile and run:
  5. g++ -o drawing.min drawing.min.cpp -L/usr/X11R6/lib -lX11 -lstdc++
  6. ./drawing.min
  7. Note: the -L option and -lstdc++ may not be needed on some machines.
  8. */
  9. #include <unistd.h>
  10. #include <cstdlib>
  11. #include <iostream>
  12. #include <X11/Xlib.h>
  13. Display* display;
  14. Window window;
  15. int main( int argc, char* argv[] ) {
  16. // open display
  17. display = XOpenDisplay("");
  18. if (!display) exit (-1);
  19. int screen = DefaultScreen(display);
  20. int w = 200;
  21. int h = 100;
  22. window = XCreateSimpleWindow(display, DefaultRootWindow(display),
  23. 10, 10, w, h, 2,
  24. BlackPixel(display, screen), WhitePixel(display, screen));
  25. XMapRaised(display, window);
  26. XFlush(display);
  27. // give server 10ms to get set up before sending drawing commands
  28. usleep(10 * 1000);
  29. // drawing demo with graphics context here ...
  30. GC gc = XCreateGC(display, window, 0, 0); // create a graphics context
  31. XSetForeground(display, gc, BlackPixel(display, screen));
  32. XSetBackground(display, gc, WhitePixel(display, screen));
  33. // load a larger font
  34. XFontStruct * font;
  35. font = XLoadQueryFont (display, "12x24");
  36. XSetFont (display, gc, font->fid);
  37. // draw text
  38. std::string text("hello X Windows");
  39. XDrawImageString( display, window, gc,
  40. 10, h / 2, text.c_str(), text.length());
  41. XFlush(display);
  42. std::cout << "ENTER2exit"; std::cin.get(); // wait for input
  43. XCloseDisplay(display);
  44. }