简体   繁体   English

在运行X窗口程序时未获得任何输出

[英]Not getting any output on Running a X window program

#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;
}

This is my first program to get started with X programming. 这是我第一个开始使用X编程的程序。 I got this sample code from Internet, I compiled and ran the code but there is no output window.Can anyone please guide me what modification is required here to display a window. 我从互联网上获得了此示例代码,我编译并运行了该代码,但是没有输出窗口。任何人都可以指导我这里显示窗口需要进行哪些修改。

You don't have an event loop, so the program initializes the window and then exits immediately. 您没有事件循环,因此程序会初始化窗口,然后立即退出。

Try using 尝试使用

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

and also clean up on exit using 并在退出时使用清理

XCloseDisplay(dis);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM