transparency - 如何创建位深度为 32 的窗口

标签 transparency x11 argb

我正在尝试创建一个位深度为 32 的 X11 窗口,以便我可以使用 ARGB 颜色。这是我所做的:

XVisualInfo vinfo;
整数深度 = 32;
XMatchVisualInfo(dpy, XDefaultScreen(dpy), depth, TrueColor, &vinfo);
XCreateWindow(dpy, XDefaultRootWindow(dpy), 0, 0, 150, 100, 0, depth, InputOutput,
vinfo.visual, 0, NULL);

这是发生的事情:

X 请求失败错误:BadMatch(参数属性无效)
失败请求的主要操作码:1 (X_CreateWindow)
请求失败的序列号:7
输出流中的当前序列号:7

关于为什么会出现 BadMatch 错误的任何指示?

最佳答案

问题是X服务器中的这段代码http://cgit.freedesktop.org/xorg/xserver/tree/dix/window.c#n615

  if (((vmask & (CWBorderPixmap | CWBorderPixel)) == 0) &&
    (class != InputOnly) &&
    (depth != pParent->drawable.depth))
    {
    *error = BadMatch;
    return NullWindow;
    }

即“如果深度与父深度不同,则必须设置边框像素或像素图”

这是一个完整的例子

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xcomposite.h>

#include <stdio.h>

int main(int argc, char **argv)
{
  Display *dpy;
  XVisualInfo vinfo;
  int depth;
  XVisualInfo *visual_list;
  XVisualInfo visual_template;
  int nxvisuals;
  int i;
  XSetWindowAttributes attrs;
  Window parent;
  Visual *visual;

  dpy = XOpenDisplay(NULL);

  nxvisuals = 0;
  visual_template.screen = DefaultScreen(dpy);
  visual_list = XGetVisualInfo (dpy, VisualScreenMask, &visual_template, &nxvisuals);

  for (i = 0; i < nxvisuals; ++i)
    {
      printf("  %3d: visual 0x%lx class %d (%s) depth %d\n",
             i,
             visual_list[i].visualid,
             visual_list[i].class,
             visual_list[i].class == TrueColor ? "TrueColor" : "unknown",
             visual_list[i].depth);
    }

  if (!XMatchVisualInfo(dpy, XDefaultScreen(dpy), 32, TrueColor, &vinfo))
    {
      fprintf(stderr, "no such visual\n");
      return 1;
    }

  printf("Matched visual 0x%lx class %d (%s) depth %d\n",
         vinfo.visualid,
         vinfo.class,
         vinfo.class == TrueColor ? "TrueColor" : "unknown",
         vinfo.depth);

  parent = XDefaultRootWindow(dpy);

  XSync(dpy, True);

  printf("creating RGBA child\n");

  visual = vinfo.visual;
  depth = vinfo.depth;

  attrs.colormap = XCreateColormap(dpy, XDefaultRootWindow(dpy), visual, AllocNone);
  attrs.background_pixel = 0;
  attrs.border_pixel = 0;

  XCreateWindow(dpy, parent, 10, 10, 150, 100, 0, depth, InputOutput,
                visual, CWBackPixel | CWColormap | CWBorderPixel, &attrs);

  XSync(dpy, True);

  printf("No error\n");

  return 0;
}

关于transparency - 如何创建位深度为 32 的窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3645632/

相关文章:

java - 我通过一些代码使java图像的某些部分透明,它在我制作的笔记本电脑上运行良好,但在其他笔记本电脑上运行不佳,为什么?

java - 手动将图像像素从彩色 ARGB 转换为灰度 ARGB

android - 在 Android 上处理使用 ImageReader 获取的图像

QT透明布局

java - 用于加载 PNG 图像的替代库

JavaFx 透明窗口 - 是的,请。鼠标透明 - 不,谢谢

c - 在多对象/结构 X11 显示的情况下如何从窗口中删除对象?

linux - 使用额外的 tty session 耗尽电池生命周期?

c - X11 - XCreateImage、Visual * vis 参数