How to set Xlib window background transparent?

蹲街弑〆低调 提交于 2019-12-05 06:34:58

问题


I want to create a window that has transparent background. How to do it?

I use XSetBackground(display, gc, 0), the background is black. I change the depth of the screen to 32. The result is still black. Here is my code:

    display = XOpenDisplay(getenv("DISPALY"));
    screen = DefaultScreen(display);
    depth = DefaultDepth(display,screen);
    printf("depth: %d\n", depth);
    rootwindow = RootWindow(display,screen);

    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 = XCreateWindow(display, DefaultRootWindow(display), 0, 0, 1440, 900, 0, vinfo.depth, InputOutput,
                    vinfo.visual, CWColormap | CWBorderPixel | CWBackPixel, &attr);

    gc = XCreateGC (display, window, 0, NULL);
   XSetBackground(display, gc, 0L);

回答1:


You need to

  • make sure window depth is 32
  • set value of alpha bits for transparent areas
  • make sure you are running composite manager that can handle transparency correctly

See my question "How to upload 32bit pixmap to server" as an example of how to set alpha channel value

upd: also make sure your window is created with associated colormap (I don't know the reason behind this, but I was not able to get 32 bit window displayed correctly without colormap)



来源:https://stackoverflow.com/questions/23051594/how-to-set-xlib-window-background-transparent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!