1
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>


Display *dis;
int screen;
Window win;
GC gc;
void init_x() {
    /* get the colors black and white (see section for details) */
    unsigned long black,white;

    /* use the information from the environment variable DISPLAY 
       to create the X connection:
    */  
    dis=XOpenDisplay((char *)0);
    screen=DefaultScreen(dis);
    black=BlackPixel(dis,screen),   /* get color black */
    white=WhitePixel(dis, screen);  /* get color white */

    /* once the display is initialized, create the window.
       This window will be have be 200 pixels across and 300 down.
       It will have the foreground white and background black
    */
    win=XCreateSimpleWindow(dis,DefaultRootWindow(dis),0,0, 
        200, 300, 5, white, black);

    /* here is where some properties of the window can be set.
       The third and fourth items indicate the name which appears
       at the top of the window and the name of the minimized window
       respectively.
    */
    XSetStandardProperties(dis,win,"My Window","HI!",None,NULL,0,NULL);

    /* this routine determines which types of input are allowed in
       the input.  see the appropriate section for details...
    */
    XSelectInput(dis, win, ExposureMask|ButtonPressMask|KeyPressMask);

    /* create the Graphics Context */
        gc=XCreateGC(dis, win, 0,0);        

    /* here is another routine to set the foreground and background
       colors _currently_ in use in the window.
    */
    XSetBackground(dis,gc,white);
    XSetForeground(dis,gc,black);

    /* clear the window and bring it on top of the other windows */
    XClearWindow(dis, win);
    XMapRaised(dis, win);
}

int main()
{

init_x();

return 0;
}

这是我开始使用 X 编程的第一个程序。我从网上得到了这个示例代码,我编译并运行了代码,但是没有输出窗口。谁能指导我这里需要什么修改才能显示一个窗口。

4

1 回答 1

1

您没有事件循环,因此程序初始化窗口然后立即退出。

尝试使用

XEvent e;
while (1) {
  XNextEvent(dis, &e);
  if (e.type == KeyPress)
     break;
}

并在退出时使用

XCloseDisplay(dis);
于 2013-09-16T01:09:00.607 回答