在X11显示编程的情况下,我想知道如何在显示窗口时擦除结构。让我们说我想画一条直线,矩形和圆形。但是,我希望在绘制矩形之前消除直线,依此类推。这样做有什么功能吗?
代码:
#include <stdlib.h>
#include <stdio.h>
#include <X11/Xlib.h>
#include <math.h>
#include "time.h"
#include "sys/time.h"
// Global variables
Display *display;
Window window;
XSetWindowAttributes attributes;
XGCValues gr_values;
XFontStruct *fontinfo;
GC gr_context;
Visual *visual;
int depth;
int screen;
XEvent event;
XColor color, dummy;
main (argc, argv)
char *argv[];
int argc;
{
display = XOpenDisplay(NULL);
screen = DefaultScreen(display);
visual = DefaultVisual(display,screen);
depth = DefaultDepth(display,screen);
attributes.background_pixel = XWhitePixel(display,screen);
window = XCreateWindow( display,XRootWindow(display,screen),
200, 200, 1050, 1200, 5, depth, InputOutput,
visual ,CWBackPixel, &attributes);
XSetStandardProperties(display,window,"Welcome","Hi",None,NULL,0,NULL);
XSelectInput(display,window,ExposureMask | KeyPressMask) ;
fontinfo = XLoadQueryFont(display,"6x10");//6*10
XAllocNamedColor(display, DefaultColormap(display, screen),"red",
&color,&dummy);
gr_values.font = fontinfo->fid;
gr_values.foreground = color.pixel;
gr_context=XCreateGC(display,window,GCFont+GCForeground, &gr_values);
// gr_context2=XCreateGC(display,window,GCFont+GCForeground, &gr_values);
XFlush(display);
XMapWindow(display,window);
XFlush(display);
int i,j,a,b,h,w,angle1,angle2;
while(1){
XNextEvent(display,&event);
switch(event.type){
case Expose:
//Straight line
XDrawLine(display,window,gr_context,800,800, 400, 450);
XFlush(display);
usleep(50000);
// Which function should I write here to make the line vanished??
//Rectangle
XDrawRectangle(display,window,gr_context,102,103, 200, 150);
XFlush(display);
usleep(50000);
//Arc and Circle
a = 600, b = 700;
h = 100, w = 100;
angle1 = 0, angle2 = 360*64;
XDrawArc(display, window, gr_context, a-(w/2), b-(h/2), w, h, angle1, angle2);
XFlush(display);
usleep(50000);
break;
case KeyPress:
XCloseDisplay(display);
exit(0);
}
}
}
更新的代码:关注 - 显示两次,然后删除第一个
#include <stdlib.h>
#include <stdio.h>
#include <X11/Xlib.h>
#include <math.h>
#include "time.h"
#include "sys/time.h"
// Global variables
Display *display;
Window window;
XSetWindowAttributes attributes;
XGCValues gr_values;
XFontStruct *fontinfo;
GC gr_context;
Visual *visual;
int depth;
int screen;
XEvent event;
XColor color, dummy;
main (argc, argv)
char *argv[];
int argc;
{
display = XOpenDisplay(NULL);
screen = DefaultScreen(display);
visual = DefaultVisual(display,screen);
depth = DefaultDepth(display,screen);
attributes.background_pixel = XWhitePixel(display,screen);
window = XCreateWindow( display,XRootWindow(display,screen),
200, 200, 1050, 1200, 5, depth, InputOutput,
visual ,CWBackPixel, &attributes);
XSetStandardProperties(display,window,"Welcome","Hi",None,NULL,0,NULL);
XSelectInput(display,window,ExposureMask | KeyPressMask) ;
fontinfo = XLoadQueryFont(display,"6x10");
XAllocNamedColor(display, DefaultColormap(display, screen),"cyan",
&color,&dummy);
gr_values.font = fontinfo->fid;
gr_values.foreground = color.pixel;
gr_values.function = GXxor;
gr_context=XCreateGC(display,window,GCFont+GCForeground+GCFunction, &gr_values);
XFlush(display);
XMapWindow(display,window);
XFlush(display);
int i,j,a,b,h,w,angle1,angle2;
while(1){
XNextEvent(display,&event);
switch(event.type){
case Expose:
XDrawLine(display,window,gr_context,800,800, 400, 450);
XFlush(display);
usleep(500000);
XDrawRectangle(display,window,gr_context,102,103, 200, 150);
XFlush(display);
usleep(500000);
break;
case KeyPress:
XCloseDisplay(display);
exit(0);
}
}
}
答案 0 :(得分:4)
没有直接的方法,因为X11
不存储可以从显示列表中删除的对象,就像更高级别的图形库可能支持的那样。
最简单的解决方法是使用XOR图形。
以下是代码中应该更改的内容,以便使用此方法实现您的目标:
***************
*** 38,40 ****
! XAllocNamedColor(display, DefaultColormap(display, screen),"cyan",
&color,&dummy);
--- 38,40 ----
! XAllocNamedColor(display, DefaultColormap(display, screen),"red",
&color,&dummy);
***************
*** 43,47 ****
gr_values.foreground = color.pixel;
- gr_values.function = GXxor;
! gr_context=XCreateGC(display,window,GCFont+GCForeground+GCFunction, &gr_values);
--- 43,46 ----
gr_values.foreground = color.pixel;
! gr_context=XCreateGC(display,window,GCFont+GCForeground, &gr_values);
***************
*** 66,68 ****
// Which function should I write here to make the line vanished??
- XDrawLine(display,window,gr_context,800,800, 400, 450);
--- 65,66 ----
***************
绘图颜色从红色变为青色,xor
渲染在白色背景下保持红色。
图形上下文现在包含GXxor
函数。
在延迟之后添加具有相同参数的行的第二个绘图。第二个调用“中和”前一个调用,因此屏幕恢复到原来的状态。
或者,您可以使用双缓冲,但这意味着您需要重绘除备用缓冲区上已删除对象之外的所有内容。