即使缓冲区已在Xlib中刷新,该行也未显示

时间:2014-05-31 10:11:25

标签: c xlib

/* Draw_Red_Line.c */

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <stdlib.h>

Window     Win;
Display    *Dsp;
GC         Gc;
XGCValues  Gc_Values;
XEvent     Event;
Colormap   Color_Map;
XColor     Screen_RGB, Exact_RGB;

unsigned int Win_Size;

int main (void)
{
  /* Set window size */
  Win_Size = 625;

  /* Open a connection to the X server that controls a display */
  Dsp = XOpenDisplay(NULL);

  /* Create an unmapped subwindow of root window */
  Win = XCreateWindow(Dsp, XDefaultRootWindow(Dsp), 0, 0, Win_Size, Win_Size, 0,
                      XDefaultDepth(Dsp, 0), InputOutput, CopyFromParent, 0, 0);

  /* Map the window on the screen */
  XMapWindow(Dsp, Win);

  /* Wait until the window appears */
  do
    XNextEvent(Dsp, &Event);
  while
    (Event.type != MapNotify);

  /* Draw a red line */
  Color_Map = DefaultColormap(Dsp, DefaultScreen(Dsp));
  XAllocNamedColor(Dsp, Color_Map, "Red", &Screen_RGB, &Exact_RGB);
  Gc_Values.foreground = Screen_RGB.pixel;
  Gc_Values.line_width = 1;
  Gc = XCreateGC(Dsp, Win, GCForeground | GCLineWidth, &Gc_Values);
  XDrawLine (Dsp, Win, Gc, 0, 0, 624, 624);
  XFlush(Dsp);

  /* Exit */
  fprintf(stdout, "Exit the process");
  return EXIT_SUCCESS;
}

我尝试在(Gnome-)终端编译并运行它:

EXECUTABLENAME="Draw_Red_Line"
gcc "$EXECUTABLENAME.c" -o "$EXECUTABLENAME" -std=c99 -Wall -lX11
./"$EXECUTABLENAME"

海湾合作委员会没有报告任何警告和错误。无论背景如何,红线都没有显示,甚至是消息字符串,&#34;退出进程&#34;,没有打印到stdout(运行脚本的终端) 。为什么呢?

1 个答案:

答案 0 :(得分:1)

您需要在Expose事件处理程序中包含绘图代码。在调用XDrawLine后,可能的内容无论如何都会失效,因为这是不可见的。如果用以下代码替换XFlush,一切都按预期工作:

  //XFlush(Dsp);
  XSelectInput(Dsp, Win, ExposureMask);
  while(1) {
    XDrawLine (Dsp, Win, Gc, 0, 0, 624, 624);
    XNextEvent(Dsp, &Event);
  }