gpt4 book ai didi

c++ - 在运行 X 窗口程序时没有得到任何输出

转载 作者:太空宇宙 更新时间:2023-11-04 11:49:46 24 4
gpt4 key购买 nike

#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编程的第一个程序。我从网上得到了这个示例代码,我编译并运行了代码但是没有输出窗口。任何人都可以指导我在这里需要进行哪些修改才能显示一个窗口。

最佳答案

你没有事件循环,所以程序初始化窗口然后立即退出。

尝试使用

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

并在退出时清理

XCloseDisplay(dis);

关于c++ - 在运行 X 窗口程序时没有得到任何输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18819081/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com