gpt4 book ai didi

c - 在多对象/结构 X11 显示的情况下如何从窗口中删除对象?

转载 作者:太空宇宙 更新时间:2023-11-04 01:25:40 26 4
gpt4 key购买 nike

在 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);

}
}
}

最佳答案

没有直接的方法,因为 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 函数。

  • 在延迟之后添加具有完全相同参数的第二幅线图。第二个调用“中和”前一个调用,因此屏幕恢复到原来的样子。

或者,您可以使用双缓冲,但这意味着您需要在备用缓冲区上重绘除已删除对象之外的所有内容。

关于c - 在多对象/结构 X11 显示的情况下如何从窗口中删除对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31362141/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com