QT 5.10 嵌入桌面以及 真半透明实现,win7&win10

首先嵌入桌面

添加依赖:

LIBS += -luser32

其次main.cpp中添加定义及引用

 

#include <Windows.h>

static BOOL enumUserWindowsCB(HWND hwnd, LPARAM lParam)
{
	long wflags = GetWindowLong(hwnd, GWL_STYLE);
	if (!(wflags & WS_VISIBLE)) return TRUE;

	HWND sndWnd;
	if (!(sndWnd = FindWindowEx(hwnd, NULL, L"SHELLDLL_DefView", NULL))) 
		return TRUE;

	HWND targetWnd;
	if (!(targetWnd = FindWindowEx(sndWnd, NULL, L"SysListView32", L"FolderView"))) 
		return TRUE;

	HWND* resultHwnd = (HWND*)lParam;
    *resultHwnd = targetWnd;
    //*resultHwnd = hwnd;//set to workerW
	return FALSE;
}

HWND findDesktopIconWnd()
{
	HWND resultHwnd = NULL;
	EnumWindows((WNDENUMPROC)enumUserWindowsCB, (LPARAM)&resultHwnd);
	return resultHwnd;
}

并在main函数中添加调用,

HWND hdesktop = findDesktopIconWnd();

WId wid = w.winId();

SetParent((HWND)wid, hdesktop);

至此可通过VS自带spy++查看窗口已经嵌入了桌面窗口中。

 

背景半透明

在QT中据说这是个BUG,反正爬了很久才出来。

在同时使用了

setAttribute(Qt::WA_TranslucentBackground, true);
setWindowFlags(Qt::FramelessWindowHint|Qt::Tool);

这两个设置之后,如果没有嵌入桌面层还好说,一旦嵌入了桌面层,在win10下会莫名其妙的坐标飞掉。。。拉都拉不回来,没找到原因,忘有大拿指点。

而通过调色板来做半透明的话,win10下效果良好,win7下背景色永恒的黑色。。所以要根据系统版本来使用不同的代码实现win7&win10的半透明效果。

MainWindow:MainWindow(){
  if (QSysInfo::windowsVersion() == QSysInfo::WV_WINDOWS10) {
		QPalette pal;
		pal.setColor(QPalette::Background, QColor(0, 0, 0, 60));
		setPalette(pal);
  }

  if (QSysInfo::windowsVersion() == QSysInfo::WV_WINDOWS7) {
	  setAttribute(Qt::WA_TranslucentBackground, true);
  }

  setWindowFlags(Qt::FramelessWindowHint|Qt::Tool);

  .....
}

//must call setAttribute(Qt::WA_TranslucentBackground, true);
void MainWindow::paintEvent(QPaintEvent*)
{
	if (QSysInfo::windowsVersion() == QSysInfo::WV_WINDOWS7) {
		QPainter p(this);
		p.fillRect(rect(), QColor(0, 0, 0, 60));
	}
}

win7效果:

win10效果:

注:

最近发现了另外一这种方法,看起来是正统的方法,待实现后贴出来。

通过设置WS_EX_LAYERED属性以及调用SetLayeredWindowAttributes或UpdateLayeredWindow。

 

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值