带有 Xlib 的空或透明窗口仅显示边框线 [英] Empty or transparent window with Xlib showing border lines only

查看:25
本文介绍了带有 Xlib 的空或透明窗口仅显示边框线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法用 Xlib 创建一个窗口,它只显示边框线、标题、关闭按钮并且可以用鼠标移动?窗口的内容必须为空(或完全透明",尽管透明"听起来更像是我不需要的效果).基本上窗口应该显示背景区域.

Is there a way to create a window with Xlib which only display the border lines, title, close button and that you can move with the mouse? The content of the window must be empty (or "totally transparent", although "transparency" sounds more like an effect I don't need). Basically the window should show the background area.

推荐答案

我不确定这是否是您想要的,但以下代码创建了一个具有透明背景的 X 窗口,但仍使用窗口管理器的窗口装饰.

I'm not sure if it is what you want, but following code creates an X window with transparent background but still using the window decoration of your window manager.

只有当您的 X11 和图形硬件配置支持 32 位深度的视觉效果时,它才能工作.

It will only work though if your X11 and graphics hardware configuration supports visuals with a depth of 32 bit.

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

int main(int argc, char* argv[])
{
    Display* display = XOpenDisplay(NULL);

    XVisualInfo vinfo;
    XMatchVisualInfo(display, DefaultScreen(display), 32, TrueColor, &vinfo);

    XSetWindowAttributes attr;
    attr.colormap = XCreateColormap(display, DefaultRootWindow(display), vinfo.visual, AllocNone);
    attr.border_pixel = 0;
    attr.background_pixel = 0;

    Window win = XCreateWindow(display, DefaultRootWindow(display), 0, 0, 300, 200, 0, vinfo.depth, InputOutput, vinfo.visual, CWColormap | CWBorderPixel | CWBackPixel, &attr);
    XSelectInput(display, win, StructureNotifyMask);
    GC gc = XCreateGC(display, win, 0, 0);

    Atom wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", 0);
    XSetWMProtocols(display, win, &wm_delete_window, 1);

    XMapWindow(display, win);

    int keep_running = 1;
    XEvent event;

    while (keep_running) {
        XNextEvent(display, &event);

        switch(event.type) {
            case ClientMessage:
                if (event.xclient.message_type == XInternAtom(display, "WM_PROTOCOLS", 1) && (Atom)event.xclient.data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", 1))
                    keep_running = 0;

                break;

            default:
                break;
        }
    }

    XDestroyWindow(display, win);
    XCloseDisplay(display);
    return 0;
}

这篇关于带有 Xlib 的空或透明窗口仅显示边框线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆