2

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);
user.dz
  • 962
  • 2
  • 19
  • 39
lidedongsn
  • 125
  • 2
  • 12

1 Answers1

2

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)

Community
  • 1
  • 1
Andrey Sidorov
  • 24,905
  • 4
  • 62
  • 75
  • I saw your page before.XMatchVisualInfo func make the depth 32.The next two steps I don't know how to do.can you give me a simple example that create a window with transparent background.I use XClearArea to make an area show the background of the window.I want to set the background of the window transparent.So, I can get a subregion that is transparent. – lidedongsn Apr 14 '14 at 05:47
  • I got a page,the example is below: [a link](http://stackoverflow.com/questions/13395179/empty-or-transparent-window-with-xlib-showing-border-lines-only/23053020#23053020) But the background is black still.Anyone has a good solution? – lidedongsn Apr 14 '14 at 06:19
  • Just transfer transparent pixels (not black). Pre-fill RGBA32 pixmap on the client, set A to some value and then do XPutImage – Andrey Sidorov Apr 14 '14 at 07:11
  • exemple using node-x11: https://github.com/sidorares/node-x11/blob/master/examples/smoketest/transpwindow.js – Andrey Sidorov Apr 14 '14 at 07:11
  • I'm so sorry that I can't understand your .js code example clearly.Can you give me a simple example that just create a window with transparent background using C or C++.With the best thanks!!!For I am a newbie.I use [create 32 depth window example](http://stackoverflow.com/questions/13395179/empty-or-transparent-window-with-xlib-showing-border-lines-only/23053020#23053020)for example – lidedongsn Apr 14 '14 at 09:15
  • 1
    Whether should it done by compositing clients like many WMs,like Metacity, Compiz etc. or standalone compositors like compton or xcompmgr? Can I use pure xlib and it's extention--xrender,to do this work?It looks difficult ,even no effect. – lidedongsn Apr 15 '14 at 08:01
  • not quite sure - do you want to write your own composite manager? In that case you need Composite & Damage extension to get up to date set of pixmaps representing window surfaces and Render or GLX to efficiently composite it into single image – Andrey Sidorov Apr 15 '14 at 12:37
  • I use xcompmgr as compositor.When I run it, the 32 bit window is abnormal.It hides and apears sometime.the root window becomes gray. – lidedongsn Apr 17 '14 at 06:16
  • My system supports transparent windows (using `transset` from a terminal applies transparency to any window), yet the root depth on my system appears to be only 24. Is setting a depth of 32 really required? – matanster Jun 02 '20 at 12:31