Qt 不规则窗体 – 鼠标点击穿透(Linux也可以,有对x11的配置的方法)

之前写过如何用 Qt 现成的方法写出无边框半透明的不规则窗体:《Qt 不规则窗体 – 无边框半透明》

其实有一个很特殊的窗体属性一直以来都伴随着不规则窗体出现,这就是本文要介绍的鼠标点击穿透。鼠标点击穿透被广泛应用在桌面挂件程序上,目的是为了避免鼠标对窗体进行误操作。

Qt 并没有自带的方法可以实现该功能,所以需要调用系统本身的 API 来实现。这里介绍下 Windows 下和 Linux 下如何实现鼠标点击穿透。

Windows API : SetWindowLong

SetWindowLong是一个Windows API函数。该函数用来改变指定窗口的属性.函数也将指定的一个32位值设置在窗口的额外存储空间的指定偏移位置。

函数定义:

LONG WINAPI SetWindowLong(
  _In_  HWND hWnd,
  _In_  int nIndex,
  _In_  LONG dwNewLong
);

头文件:

#include<Winuser.h>

代码示例:

SetWindowLong((HWND)winId(), GWL_EXSTYLE, GetWindowLong((HWND)winId(), GWL_EXSTYLE) |
               WS_EX_TRANSPARENT | WS_EX_LAYERED);

Linux X11 API : XShapeCombineRectangles

在 Linux/Unix 中,需要用到 X11 库函数:XShapeCombineRectangles。

函数定义:

void XShapeCombineRectangles (
        Display *dpy, 
        XID dest, 
        int destKind, 
        int xOff, 
        int yOff, 
        XRectangle *rects, 
        int n_rects, 
        int op, 
        int ordering);

头文件:

#include <X11/extensions/shape.h>

代码示例:以 Linux 下 Qt 中使用为例

//头文件
#include <X11/extensions/shape.h>
#include <QtX11Extras/QX11Info>

//函数调用
XShapeCombineRectangles(QX11Info::display(), winId(), ShapeInput,0,0, NULL, 0, ShapeSet, YXBanded);

//.pro文件中添加
QT += x11extras
LIBS += -lX11 -lXext

http://ju.outofmemory.cn/entry/162219

原文地址:https://www.cnblogs.com/findumars/p/6350233.html