如何在XLib中创建半透明白色窗口

jsr*_*jsr 4 c x11 alpha transparent xlib

我想在XLib中创建一个半透明的白色窗口,但窗口不是半透明的,它仍然是完全不透明的.我使用compton合成器,系统中有透明窗口,所以问题在于代码:

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.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 = 0x80ffffff;

    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;
}
Run Code Online (Sandbox Code Playgroud)

n. *_* m. 6

X11需要预乘的颜色,即真正的不透明颜色需要乘以alpha值(并相应地缩放,即当通道宽度为8位时除以256).当您需要组合多个级别时,此格式更易于使用.见这里的公式.当所有内容都预乘时,计算量就会减少.

因此,您需要将每个R,G和B通道乘以alpha值(0x80)并除以256.

将背景设置为0x80808080会得到所需的结果:

注意结果与@patthoyts建议的结果不同:这里只有适当的窗口是半透明的,WM装饰保持不透明; 在那里,窗户本身和装饰都由WM透明(并且WM进行必要的颜色混合).