c++ - 如何通过 Xlib 在启动屏幕中绘制字符串

标签 c++ c linux x11 xlib

这是我的代码,我无法跳出 while(!done) 函数

使用XFlush(d)可以显示显示表单缓冲区并且在XCloseDisplay(d)之前不会消失

我想制作这样的绘制字符串

g++ -o youname youcppname -lX11

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(void)
{
    Display *d;
    Window w;
    XEvent e;
    const char *msg = "Hello, World!";
    int s;
    bool done = false;

    /* open connection with the server */
    d = XOpenDisplay(NULL);
    if (d == NULL)
    {
        fprintf(stderr, "Cannot open display\n");
        exit(1);
    }
    s = DefaultScreen(d);

    /* create window */
    w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 480, 320, 0,BlackPixel(d, s), WhitePixel(d, s));
    Atom type = XInternAtom(d,"_NET_WM_WINDOW_TYPE", False);
    Atom value = XInternAtom(d,"_NET_WM_WINDOW_TYPE_SPLASH", False);
    XChangeProperty(d, w, type, XA_ATOM, 32, PropModeReplace, reinterpret_cast<unsigned char*>(&value), 1);
    /* register interest in the delete window message */
    Atom wmDeleteMessage = XInternAtom(d, "WM_DELETE_WINDOW", False);
    XSetWMProtocols(d, w, &wmDeleteMessage, 1);

    /* select kind of events we are interested in */
    XSelectInput(d, w, ExposureMask | KeyPressMask | StructureNotifyMask);

    /* map (show) the window */
    XMapWindow(d, w);
    /* event loop */
    while (!done)
    {
        XNextEvent(d, &e);
        /* draw or redraw the window */
        if (e.type == Expose)
        {
            XDrawString(d, w, DefaultGC(d, s), 50, 50, msg, strlen(msg));
        }

        /* exit on key press */
        switch(e.type)
        {
        case KeyPress:
            XDestroyWindow(d, w);

        break;

        case DestroyNotify:
            done = true;
        break;

        case ClientMessage:
            if (e.xclient.data.l[0] == wmDeleteMessage)
            {
                done = true;
            }
        break;
        }
    }
    /* close connection to server */
    XCloseDisplay(d);

    return 0;
}

最佳答案

我不太确定为什么 XFlush 不起作用,但这是事件循环和刷新的混合体。它等待暴露事件,绘制字符串并退出循环。

5 秒后,它取消映射窗口,然后 5 秒后退出。

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(void)
{
    Display *d;
    Window w;
    XEvent e;
    const char *msg = "Hello, World!";
    int s;
    bool done = false;

    /* open connection with the server */
    d = XOpenDisplay(NULL);
    if (d == NULL)
    {
        fprintf(stderr, "Cannot open display\n");
        exit(1);
    }
    s = DefaultScreen(d);

    /* create window */
    w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 480, 320, 0,BlackPixel(d, s), WhitePixel(d, s));
    Atom type = XInternAtom(d,"_NET_WM_WINDOW_TYPE", False);
    Atom value = XInternAtom(d,"_NET_WM_WINDOW_TYPE_SPLASH", False);
    XChangeProperty(d, w, type, XA_ATOM, 32, PropModeReplace, reinterpret_cast<unsigned char*>(&value), 1);
    /* register interest in the delete window message */
    Atom wmDeleteMessage = XInternAtom(d, "WM_DELETE_WINDOW", False);
    XSetWMProtocols(d, w, &wmDeleteMessage, 1);

    /* select kind of events we are interested in */
    XSelectInput(d, w, ExposureMask);

    /* map (show) the window */
    XMapWindow(d, w);

    /* event loop */
    while (!done)
    {
        XNextEvent(d, &e);
        /* draw or redraw the window */
        if (e.type == Expose)
        {
            XDrawString(d, w, DefaultGC(d, s), 50, 50, msg, strlen(msg));
            done = true;
        }
    }
    XFlush(d);
    sleep(5);
    XUnmapWindow(d,w);
    XFlush(d);
    printf("unmapped\n");
    sleep(5);
    /* close connection to server */
    XCloseDisplay(d);

    return 0;
}

关于c++ - 如何通过 Xlib 在启动屏幕中绘制字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16688368/

相关文章:

python - 为 Linux 命令编写 Python 脚本

c - 我应该如何将变量传递给 system() C 调用?

c++ - 为 QRectF 设置 topLeft 和 bottomRight

#if 0 和#endif 之间的代码块必须有成对的双引号吗?

c++ - 如何在 linux 中的多个应用程序中共享库中的变量?

c - 无法从 Linux 计算机将 UDP 发送到多播组

c++ - 如何让 gcc 为所有符号名称添加前缀

linux - 有什么工具可以在 Linux 上轻松解析配置文件?

c++ - 在 Qt 中做 Gui 测试

c++ - If 语句:值列表