4

I am going through (Change Xlib window background color with C++) & know to set the background color of XWindow.

But how can I set the Alpha value of that Xwindow. I tried several things, but when I do read from XWindow, each pixel has Alpha as 0xff.

Is there any way I can set this alpha as 0x00. Please provide some sample code.

Community
  • 1
  • 1
user1409528
  • 123
  • 2
  • 7
  • 1
    Xlib has no notion of transparency. What are you trying to achieve? There may be other ways to do what you want, perhaps with a shaped window if you only need "on" or "off" transparency. – n. m. could be an AI Oct 17 '13 at 16:27
  • 1
    As @n.m. has pointed out X11 (aka or via Xlib) has no concept of transparencies. But there may be two X extensions that could be relevant depending on your goals. The first is [Shape](https://en.wikipedia.org/wiki/Shape_extension), which allows the creation of non-rectangular windows (classic example is the round clock face in `oclock` which I believe is still part of the X.org distribution (some distributions may not install it by default). The second is [Render](https://en.wikipedia.org/wiki/X_Rendering_Extension) or XRender which implements image compositing, ideally via graphics hardware. – mctylr Nov 28 '14 at 22:41

2 Answers2

3

Setting the alpha channel will only work for visuals with a depth of 32 bits. Use XMatchVisualInfo to create 32 bit visuals. That will only work though if your graphics hardware supports it.

Check out this answer where I have posted sample code that generates a completely transparent window.

Community
  • 1
  • 1
kassiopeia
  • 615
  • 1
  • 9
  • 23
1

For creating a Transparent window (ARGB : 0x00000000) with no borders, can I use like this. pls confirm.

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);

    XDestroyWindow(display, win);
    XCloseDisplay(display);
    return 0;
}
user1409528
  • 123
  • 2
  • 7
  • Yes, that just creates the window with a transparent background. Setting the border width to zero means there will be no additional border in the client area, but the window will still have the default decorations of the desktop environment around it. No mapping or event handling in that code though. – kassiopeia Oct 17 '13 at 06:43
  • It *doesn't* map the window, so yeah it is pretty transparent, as in it isn't even drawn. Try adding: `XMapWindow(display, win); XFlush(display); sleep(5);` _before_ `XDestroyWindow` to see its actual impact. – mctylr Nov 28 '14 at 22:24
  • See [this answer](http://stackoverflow.com/a/13397150/83348) for a working example. – mctylr Nov 28 '14 at 22:35