为什么我设置xlib窗口背景透明失败?

时间:2014-04-15 03:16:48

标签: c linux window transparent xlib

我使用以下代码来获取透明窗口,但它返回黑色。我怎么了?并且,任何人都可以给我一个简单的例子来创建一个透明背景的窗口吗?谢谢!

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

1 个答案:

答案 0 :(得分:1)

您的代码适用于我:

KDE:

enter image description here

openbox + xcompmgr:

enter image description here

很可能你没有运行复合管理器。尝试启动xcompmgr命令

同时选中_NET_WM_CM_S0选择所有者 - 它应point to a window created by composite manager

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

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

    Atom cmAtom = XInternAtom(display, "_NET_WM_CM_S0", 0);
    Window cmOwner = XGetSelectionOwner(display, cmAtom);

    printf("Composite manager window: %i\n", cmOwner);

    XCloseDisplay(display);
    return 0;
}

更新

尝试设置覆盖重定向以防止WM装饰遮挡窗口。

attr.border_pixel = 0;
attr.background_pixel = 0;
attr.override_redirect = 1; /* this line added */

Window win = XCreateWindow(display, DefaultRootWindow(display), 0, 0, 300, 200, 0, 
vinfo.depth, InputOutput, vinfo.visual, 
CWColormap | CWBorderPixel | CWBackPixel | CWOverrideRedirect /* and this one*/, &attr);