參考資訊:
https://github.com/QMonkey/Xlib-demo
https://people.scs.carleton.ca/~claurend/Courses/COMP2401/Notes/COMP2401_Ch8_Graphics.pdf
main.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <X11/Xlib.h>
int main(int argc, char *argv[])
{
GC gc = 0;
Window win = 0;
Display *display = NULL;
display = XOpenDisplay(NULL);
win = XCreateSimpleWindow(
display,
RootWindow(display, 0),
0,
0,
320,
240,
0,
0,
0xffffff);
XStoreName(display, win, "main");
gc = XCreateGC(display, win, 0, NULL);
XMapWindow(display, win);
XFlush(display);
usleep(100000);
int x = 100;
int y = 100;
int ascent = 0;
int descent = 0;
int direction = 0;
XFontStruct *font = NULL;
XCharStruct overall = { 0 };
const char *fontname = "-*-courier-*-*-*--32-*-*-*-*-*-*-*";
font = XLoadQueryFont(display, fontname);
if (!font) {
font = XLoadQueryFont(display, "fixed");
}
XSetFont(display, gc, font->fid);
XTextExtents(font, "TEST", 4, &direction, &ascent, &descent, &overall);
XClearWindow(display, win);
x = (320 - overall.width) / 2;
y = 240 / 2 + (ascent - descent) / 2;
XDrawString(display, win, gc, x, y, "TEST", 4);
XFlush(display);
usleep(3000000);
XUnmapWindow(display, win);
XDestroyWindow(display, win);
XCloseDisplay(display);
return 0;
}
編譯、測試
$ gcc main.c -o main -lX11 $ ./main