我已经在这几天了,但我似乎无法弄清楚如何创建一个 32 位根窗口,以便我可以在子窗口上使用 RGBA 颜色,而子窗口不能使用 32 - 位颜色深度(RGB 为 24 位,Alpha channel 为 8 位)如果父窗口,或在本例中为根窗口,没有 32 位颜色深度。我正在使用以下代码将根窗口的背景设置为颜色深度为 24 位的 RGB 图像,因此当我将 XCreatePixmap 设置为 24 位颜色深度时它就可以工作,但我需要这个根窗口具有用于 alpha 合成的 32 位颜色深度:
/* displays an image or sets root background
* PUBLIC DOMAIN - CC0 http://creativecommons.org/publicdomain/zero/1.0/
* J.Mayo 2013
*
* gcc -Wall -W -g3 -o background background.c -lX11 -lImlib2
*
*/
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xcomposite.h>
#include <Imlib2.h>
struct screenAttributes {
int height;
int width;
};
struct screenAttributes screenAttr;
void initializeScreenAttributes(Screen *screen) {
screenAttr.height=screen->height;
screenAttr.width=screen->width;
}
int main(int argc, char **argv)
{
Imlib_Image img;
Display *dpy;
Pixmap pix;
Window root;
Screen *scn;
// Window topPanel;
int width, height;
const char *filename = "/sampleImage.png";
img = imlib_load_image(filename);
if (!img) {
fprintf(stderr, "%s:Unable to load image\n", filename);
goto usage;
}
imlib_context_set_image(img);
width = imlib_image_get_width();
height = imlib_image_get_height();
dpy = XOpenDisplay(NULL);
if (!dpy) return 0;
scn = DefaultScreenOfDisplay(dpy);
root = DefaultRootWindow(dpy);
pix = XCreatePixmap(dpy, root, width, height,32); //when depth is set to 24 it just works, but when it is set to 32 it fails.
//scale the image
initializeScreenAttributes(scn);
imlib_blend_image_onto_image(img,0,0,0,width,height,0,0,
screenAttr.width, screenAttr.height);
imlib_context_set_display(dpy);
imlib_context_set_visual(DefaultVisualOfScreen(scn));
imlib_context_set_colormap(DefaultColormapOfScreen(scn));
imlib_context_set_drawable(pix);
imlib_render_image_on_drawable(0, 0);
XSetWindowBackgroundPixmap(dpy, root, pix);
XClearWindow(dpy, root);
while (XPending(dpy)) {
XEvent ev;
XNextEvent(dpy, &ev);
}
XFreePixmap(dpy, pix);
imlib_free_image();
sleep(10);
//XFreePixmap(dpy, pix);
//imlib_free_image();
XCloseDisplay(dpy);
return 0;
usage:
fprintf(stderr, "usage: %s <image_file>\n", argv[0]);
return 1;
}
当我将 XCreapePixmap 的颜色深度设置为 32 位时,我得到:
X Error of failed request: BadMatch (invalid parameter attributes)
Major opcode of failed request: 130 (MIT-SHM)
Minor Opcode of failed request: 3 (X_ShmPutImage)
Serial number of failed request : 28
Current serial number in output stream: 29 xinit: connection to X server lost
所以,换句话说,我不太确定如何将根窗口的颜色深度设置为 32 位,并将 24 位 RGB 图像设置为根窗口的背景。
谢谢!
附言我没有安装任何窗口管理器或任何桌面环境,因此无法使用其中的任何可用工具。
最佳答案
没有。您不必让根窗口的深度为 32 就可以让子窗口的深度为 32 也。否则你怎么认为 Windows 可以有 alpha channel Composer ?你怎么看:
http://www.enlightenment.org/ss/e-5872c6ec3ddce1.54730231.png
没有 2 个具有 32 位深度的窗口是可能的吗? (左边的 2 - 时钟和半透明终端)。 :)
半透明的工作方式是合成器的干预(现在通常是你的 窗口管理器)并在顶部合成 32 位窗口(也可能处理 在底部也重新绘制根窗口 - 这可能取决于)。所以说“我没有合成器/窗口管理器,所以这是不可能的”基本上是在说“我不想做我必须做的唯一一件事,以便获得半透明性”,因此我建议您重新评估该位置。
所以你真正想要的是一个合成器和 32 位窗口。要么使用 合成窗口管理器,然后创建 32 位窗口,或者运行一个单独的 合成器和你现有的 WM,或者编写你自己的合成器......(不打算 正确快速地完成这项工作非常有趣)...
现在要创建一个 ARGB 窗口,您需要 XRender 的视觉帮助。如下所示,disp 是您的 Xlib Display,parent 是父级 Window(例如 root):
Window win;
XSetWindowAttributes attr;
XWindowAttributes att;
XVisualInfo *xvi;
XVisualInfo vi_in;
int nvi, i, scr = 0;
XRenderPictFormat *fmt;
Visual *vis;
vi_in.screen = scr;
vi_in.depth = 32;
vi_in.class = TrueColor;
xvi = XGetVisualInfo(disp,
VisualScreenMask |
VisualDepthMask |
VisualClassMask,
&vi_in,
&nvi);
if (!xvi) return 0;
vis = NULL;
for (i = 0; i < nvi; i++)
{
fmt = XRenderFindVisualFormat(disp, xvi[i].visual);
if ((fmt->type == PictTypeDirect) && (fmt->direct.alphaMask))
{
vis = xvi[i].visual;
break;
}
}
XFree (xvi);
attr.backing_store = NotUseful;
attr.override_redirect = 0;
attr.colormap = XCreateColormap(disp, parent,
vis, AllocNone);
attr.border_pixel = 0;
attr.background_pixmap = None;
attr.bit_gravity = NorthWestGravity;
attr.win_gravity = NorthWestGravity;
attr.save_under = 0;
attr.do_not_propagate_mask = NoEventMask;
attr.event_mask = KeyPressMask |
KeyReleaseMask |
ButtonPressMask |
ButtonReleaseMask |
EnterWindowMask |
LeaveWindowMask |
PointerMotionMask |
ExposureMask |
VisibilityChangeMask |
StructureNotifyMask |
FocusChangeMask |
PropertyChangeMask |
ColormapChangeMask;
win = XCreateWindow(disp, parent,
x, y, w, h, 0,
32,
InputOutput,
vis,
CWBackingStore |
CWOverrideRedirect |
CWColormap |
CWBorderPixel |
CWBackPixmap |
CWSaveUnder |
CWDontPropagate |
CWEventMask |
CWBitGravity |
CWWinGravity,
&attr);
代码来自这里:https://git.enlightenment.org/core/efl.git/tree/src/lib/ecore_x/ecore_x_window.c#n1644
我们曾经有一个 XCB 后端和 XCB 中的所有这些代码,但我们在大约十年后放弃了 XCB。如果你克隆上面的内容并挖掘历史,如果你真的想挖掘它,你会发现曾经有 xlib 和 xcb 子目录的 ecore_x 目录。
这就是我编写 xlib 抽象器/细节填充器的原因,因为如果您将常见的冗长 Xlib 用法隐藏在更简单的 API 后面,那么编写的代码就会少得多。
关于c++ - 在 xlib 中创建 32 位根窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41524843/