admin管理员组文章数量:1566361
首先嵌入桌面
添加依赖:
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。
版权声明:本文标题:QT 5.10 嵌入桌面以及 真半透明实现,win7&win10 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1726730304a1082252.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论