为什么我设置的xlib窗口背景透明的失败? [英] Why I set xlib window background transparent failed?

查看:1241
本文介绍了为什么我设置的xlib窗口背景透明的失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code得到一个透明的窗口,但它返回black.What是我错了?而且,有谁能够给我一个简单的例子来创建具有透明背景的窗口?谢谢!

 的#include< X11 / Xlib.h>
#包括LT&; X11 / Xutil.h>INT主(INT ARGC,CHAR *的argv [])
{
    显示*显示= XOpenDisplay(NULL);    XVisualInfo vinfo;
    XMatchVisualInfo(显示,DefaultScreen(显示器),32,真彩色,&安培; vinfo);    XSetWindowAttributes ATTR;
    attr.colormap = XCreateColormap(显示器,DefaultRootWindow(显示器),vinfo.visual,AllocNone);
    attr.border_pixel = 0;
    attr.background_pixel = 0;    窗口赢= XCreateWindow(显示,DefaultRootWindow(显示器),0,0,300,200,0,vinfo.depth,的inputOutput,vinfo.visual,CWColormap | CWBorderPixel | CWBackPixel,&放大器; attr指示);
    XSelectInput(显示器,赢,StructureNotifyMask);
    GC GC = XCreateGC(显示器,赢,0,0);    原子WM_DELETE_WINDOW = XInternAtom(显示,WM_DELETE_WINDOW,0);
    XSetWMProtocols(显示器,赢了,&安培; WM_DELETE_WINDOW,1);    XMapWindow(显示器,赢);    INT keep_running = 1;
    XEvent事件;    而(keep_running){
        XNextEvent例行(显示器,功放及;事件);        开关(event.type){
            案例ClientMessage:
                如果(event.xclient.message_type == XInternAtom(显示,WM_PROTOCOLS,1)及及(原子)event.xclient.data.l [0] == XInternAtom(显示,WM_DELETE_WINDOW,1))
                    keep_running = 0;                打破;            默认:
                打破;
        }
    }    XDestroyWindow(显示器,赢);
    XCloseDisplay(显示);
    返回0;
}


解决方案

您code正常工作对我来说:

KDE

OPENBOX +使用xcompmgr:

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

另外,请检查 _NET_WM_CM_S0 选择所有者 - 它应该的点由复合经理创建的窗口。

 的#include< X11 / Xlib.h>
#包括LT&; X11 / Xutil.h>
#包括LT&;&stdio.h中GT;INT主(INT ARGC,CHAR *的argv [])
{
    显示*显示= XOpenDisplay(NULL);    原子cmAtom = XInternAtom(显示,_NET_WM_CM_S0,0);
    窗口cmOwner = XGetSelectionOwner(显示器,cmAtom);    的printf(综合管理器窗口:%I \\ N,cmOwner);    XCloseDisplay(显示);
    返回0;
}

更新:

尝试设置从遮蔽你的窗口否决重定向到prevent WM装饰品。

  attr.border_pixel = 0;
attr.background_pixel = 0;
attr.override_redirect = 1; / *加入这一行* /窗口赢= XCreateWindow(显示,DefaultRootWindow(显示器),0,0,300,200,0,
vinfo.depth,输入 - 输出,vinfo.visual,
CWColormap | CWBorderPixel | CWBackPixel | CWOverrideRedirect / *,这一次* /&安培; attr指示);

I use the following code to get a transparent window, but it returns black.What's wrong with me? And, can anybody give me a simple example to create a window with transparent background?THANKS!

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

解决方案

Your code works fine for me:

kde:

openbox + xcompmgr:

Most likely you are not running composite manager. Try to start xcompmgr command

Also check _NET_WM_CM_S0 selection owner - it should 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;
}

Update:

Try to set override-redirect to prevent WM decorations from obscuring your window.

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

这篇关于为什么我设置的xlib窗口背景透明的失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆