Bạn cần xử lý Sự kiện phơi bày. This tutorial giải thích với một ví dụ làm thế nào để xử lý các sự kiện Expose:
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
/*Linux users will need to add -ldl to the Makefile to compile
*this example.
*/
Display *dis;
Window win;
XEvent report;
GC green_gc;
XColor green_col;
Colormap colormap;
/*
Try changing the green[] = below to a different color.
The color can also be from /usr/X11R6/lib/X11/rgb.txt, such as RoyalBlue4.
A # (number sign) is only needed when using hexadecimal colors.
*/
char green[] = "#00FF00";
int main() {
dis = XOpenDisplay(NULL);
win = XCreateSimpleWindow(dis, RootWindow(dis, 0), 1, 1, 500, 500, 0, BlackPixel (dis, 0), BlackPixel(dis, 0));
XMapWindow(dis, win);
colormap = DefaultColormap(dis, 0);
green_gc = XCreateGC(dis, win, 0, 0);
XParseColor(dis, colormap, green, &green_col);
XAllocColor(dis, colormap, &green_col);
XSetForeground(dis, green_gc, green_col.pixel);
XSelectInput(dis, win, ExposureMask | KeyPressMask | ButtonPressMask);
XDrawRectangle(dis, win, green_gc, 1, 1, 497, 497);
XDrawRectangle(dis, win, green_gc, 50, 50, 398, 398);
XFlush(dis);
while (1) {
XNextEvent(dis, &report);
switch (report.type) {
case Expose:
fprintf(stdout, "I have been exposed.\n");
XDrawRectangle(dis, win, green_gc, 1, 1, 497, 497);
XDrawRectangle(dis, win, green_gc, 50, 50, 398, 398);
XFlush(dis);
break;
case KeyPress:
/*Close the program if q is pressed.*/
if (XLookupKeysym(&report.xkey, 0) == XK_q) {
exit(0);
}
break;
}
}
return 0;
}
tôi có thể đã hiểu lầm câu hỏi. Nếu bạn muốn tạo các sự kiện phơi bày trong ứng dụng của mình, bạn có thể tạo và thiết lập expose event và gửi nó bằng cách sử dụng XSendEvent.
Ngoài ra nếu có ai có thể đề xuất tham chiếu XLib tuyệt vời, tôi thực sự sẽ đánh giá cao nó. Sách hướng dẫn XLib trên X.org dường như không giúp ích nhiều cho người mới bắt đầu, ít nhất là không dành cho tôi. – pauld
Có điều tương tự. Tôi khuyên bạn không nên sử dụng Xlib thô. Sử dụng bộ công cụ hiện đại như Qt, gtk hoặc wX. –
Ngoài ra, mặc dù Expose sẽ hoạt động, tôi khuyên bạn nên gọi lại chức năng vẽ lại của bạn trực tiếp. Nó dễ dàng hơn và đơn giản hơn. –