通过改变窗口形状实现穿透效果
#include <stdio.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/shape.h>
int main(int argc, char *argv[])
{
Display *display;
Window win;
int screen_num;
int dummy, h = 300, w = 400, bwidth = 2;
XEvent event;
GC gc;
XGCValues gcvalues;
Pixmap mask;
XFontStruct *font;
const char *msg = "Wah, Transparent Window!";
if ( (display = XOpenDisplay (NULL)) == NULL ) {
fprintf (stderr, "Cannot connect to X server :0\n");
//exit(0);
}
printf ("connect to X server :0\n");
if ( !XShapeQueryExtension(display, &dummy, &dummy) ) {
fprintf (stderr, "SHAPE extension not supported\n");
//exit(0);
}
printf ("SHAPE extension supported\n");
/* Create a window */
screen_num = DefaultScreen (display);
printf ("got screen\n");
win = XCreateSimpleWindow (display, DefaultRootWindow(display),
10, 10, w, h, bwidth,
BlackPixel (display, screen_num),
WhitePixel (display, screen_num));
printf ("creat window\n");
/* Create a pixmap of depth 1 as mask */
mask = XCreatePixmap(display, win, w, h, 1);
printf ("creat pixmap\n");
/* Create GC */
gc = XCreateGC(display, mask, 0, &gcvalues);
printf ("got gc\n");
font = XLoadQueryFont (display, "8x16");
if(!font){
printf ("not suport font\n");
}
else{
printf ("got font\n");
}
//XSetFont (display, gc, font->fid);
printf ("set font\n");
XSetLineAttributes(display, gc, 4, LineSolid, CapRound, JoinMiter);
printf ("creat gc\n");
/* Clear the mask */
XSetForeground(display, gc, 0);
XFillRectangle (display, mask, gc, 0, 0, w, h);
printf ("clear mask\n");
/* Draw two lines and few texts */
XSetForeground(display, gc, 1);
XDrawLine (display, mask, gc, 10, h/4, w-10, h/4);
//XDrawString (display, mask, gc, w/4, h/2, msg, strlen(msg));
XDrawLine (display, mask, gc, 10, 3*h/4, w-10, 3*h/4);
printf ("draw line and string\n");
/* Map the window */
XSelectInput (display, win, ButtonPressMask | ExposureMask);
XMapWindow (display, win);
printf ("map ok \n");
while (1) {
XNextEvent (display, &event);
switch (event.type) {
case Expose:
XShapeCombineMask (display, win, ShapeBounding,
-bwidth, -bwidth, mask, ShapeSet);
break;
case ButtonPress:
XCloseDisplay (display);
//exit (0);
}
}
return 0;
}
编译cc -I/usr/X11R6/include testTrans.c -L/usr/X11R6/lib -lX11 -lXext
在ubuntu上跑的很好,有透明效果,在盒子上包段错误,加打印显示在setfont时出错,说明是字体问题。注释这一行,并取消掉显示字符的哪一行。编译通过,显示窗口,不过没有透明效果。说明是不支持透明
逐行理解全透明运行的例子testTrans.c,发现是在主循环里
XShapeCombineMask (display, win, ShapeBounding,
-bwidth, -bwidth, mask, ShapeSet);
来改变窗口形状,即无内容的部分不再是窗体的一部分,而是被挖空了。因此,透明部分是后面的窗口在响应(测试证明)。
回到盒子上来在有窗口管理器时没有透明效果,而是一片白底色;关窗口管理器,可见是两条白线画在qmlver前面,在透明处,是qmlver响应,在有内容处是透明窗体响应,可见这种全透明是有效的。只是窗口管理器进行了处理。
http://linux.die.net/man/3/xshapecombinemask
各种改变形状的方法。回头要好好研究,为视频挖一个矩形的洞出来