// ...
int main(int argc, char *argv[]) {
int rev;
Window win;
Display *dis = XOpenDisplay(NULL);
XGetInputFocus(dis, &win, &rev);
XUnmapWindow(dis, win);
XCloseDisplay(dis);
// ...
dis = XOpenDisplay(NULL);
XMapWindow(dis, win);
XCloseDisplay(dis);
return 0;
}
请注意,在取消映射窗口之前,我必须关闭显示,因此,我必须在映射之前重新初始化它以使程序正常工作。
这里发生了什么?
解决方案:
// ...
int main(int argc, char *argv[]) {
int rev;
Window win;
Display *dis = XOpenDisplay(NULL);
XGetInputFocus(dis, &win, &rev);
XUnmapWindow(dis, win);
XFlush(dis); // The fix
//...
XMapWindow(dis, win);
XCloseDisplay(dis);
return 0;
}
答案1
您不需要在 XUnmapWindow 之后调用 XCloseDisplay。如果这不起作用,您的程序中一定有其他错误可能会扰乱某些变量?
这是我快速尝试过的一个例子。它取消映射和映射窗口,并且中间不需要 XCloseDisplay/XOpenDisplay。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <X11/Xlib.h>
int main (int argc, char *argv[])
{
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;
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, 350, 200, 5, depth, InputOutput, visual, CWBackPixel, &attributes);
XSelectInput (display, window, ExposureMask | KeyPressMask) ;
fontinfo = XLoadQueryFont (display, "6x10");
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);
XFlush (display);
XMapWindow (display, window);
XFlush (display);
int keypressed = 0;
while (!keypressed) {
XNextEvent (display, &event);
switch (event.type) {
case Expose:
XDrawLine (display, window, gr_context, 0, 0, 100, 100);
XDrawString (display, window, gr_context, 100, 100, "hello1", 6);
break;
case KeyPress:
keypressed = 1;
XUnmapWindow (display, window);
XFlush (display);
break;
}
}
sleep (1);
XMapWindow (display, window);
XFlush (display);
while (1) {
XNextEvent (display, &event);
switch (event.type) {
case Expose:
XDrawLine (display, window, gr_context, 50, 50, 100, 100);
XDrawString (display, window, gr_context, 50, 50, "hello2", 6);
break;
case KeyPress:
XCloseDisplay (display);
exit (0);
}
}
}
该程序会等待,直到您按下某个键。然后它会取消映射并再次映射。下一次按键时它将退出。编译它
gcc -o programname programname.c -lX11
样本取自这里:http://www-h.eng.cam.ac.uk/help/tpl/graphics/X/X11R5/node21.html