【问题标题】:The line wasn't shown up even though the buffer was flushed in Xlib即使缓冲区已在 Xlib 中刷新,该行也未显示
【发布时间】:2014-05-31 10:11:25
【问题描述】:
/* 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"

GCC 不会报告任何警告和错误。无论背景如何,红线都没有出现,甚至消息字符串“退出进程”也没有打印到标准输出(运行脚本的终端)。为什么?

【问题讨论】:

    标签: c xlib


    【解决方案1】:

    您需要在 Expose 事件处理程序中包含您的绘图代码。可能的内容在调用 XDrawLine 后以某种方式失效,因此不可见。如果您将 XFlush 替换为以下代码,则一切正常:

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

    【讨论】:

      猜你喜欢
      • 2017-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-19
      • 1970-01-01
      • 2018-07-11
      • 1970-01-01
      相关资源
      最近更新 更多