从别人那里抄的。

  • 代码
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DISPLAY_TEXT "Taishan Office"

void start_window()
{
Display *display;
Window window;
XEvent event;
int screen;

display = XOpenDisplay(NULL);
if (display == NULL)
{
fprintf(stderr, "Cannot open display\n");
exit(1);
}

screen = DefaultScreen(display);
window = XCreateSimpleWindow(display, RootWindow(display, screen),
100, 100, 500, 500,
1, 777215, 111111);
XSelectInput(display, window, ExposureMask | KeyPressMask);
XMapWindow( display, window);

while (1)
{
XNextEvent(display, &event);
if (event.type == Expose)
{
XFillRectangle(display, window, DefaultGC(display, screen),
20, 20, 50, 50);
XDrawString( display, window, DefaultGC(display, screen),
90, 90, DISPLAY_TEXT, strlen(DISPLAY_TEXT));
}

//任意键退出
if (event.type == KeyPress)
{
break;
}
}

XCloseDisplay(display);
}


int main(int argc, char** argv)
{
start_window();
return 0;
}
  • 构建
#!/bin/bash

COMPILE_LINK=" -lX11"

OUTPUT_FILE=x11
if [ -f ${OUTPUT_FILE} ]; then
rm ${OUTPUT_FILE}
fi
echo ${COMPILE_INCLUDE_LINK}
gcc \
-o ${OUTPUT_FILE} \
x11.cpp \
${COMPILE_LINK}