窗口最底层:

浮在图标上面,但是Win+D会最小化窗口

1
2
3
4
5
6
setAttribute(Qt::WA_TranslucentBackground, true); // 按需加上
setWindowFlags(Qt::FramelessWindowHint //去边框
| Qt::X11BypassWindowManagerHint //linux下脱离任务管理器
| Qt::WindowStaysOnBottomHint //最低层显示
| Qt::Tool //不在任务栏显示
);

嵌入桌面:

浮在图标上面,Win+D不会隐藏窗口

.h

1
2
static BOOL enumUserWindowsCB(HWNDhwnd,LPARAMlParam);   //静态全局函数
HWND findDesktopIconWnd();

.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
BOOL Test::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;
return FALSE;
}

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

main

1
2
3
HWND desktopHwnd = findDesktopIconWnd();
if(desktopHwnd)
SetParent((HWND)widget->winId(), desktopHwnd);

桌面壁纸

finddesktop.h

1
2
3
4
5
6
7
8
9
#include <qt_windows.h>
/*因为msvc编译器的缘故,要链接以下库文件*/
#pragma comment(lib,"winmm.lib")
#pragma comment(lib,"User32.lib")
#pragma comment(lib,"gdi32.lib")
/*分割*/
HWND findDesktopIconWnd();//找到桌面句柄
void SendMessageToDesktop();//召唤WorkerW
HWND GetWorkerW();//得到WorkerW

finddesktop.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include"finddesktop.h"
HWND WorkerW=NULL;

HWND findDesktopIconWnd()
{
HWND hWorkerW = NULL;//WorkerW的句柄
HWND hDefView = NULL;//SHELLDLL_DefView的句柄

//找到WorkerW类的窗口
hWorkerW = FindWindowEx(NULL, NULL, L"WorkerW", NULL);
//通过遍历找到包含SHELLDLL_DefView的WorkerW
while ((!hDefView) && hWorkerW)
{
hDefView = FindWindowEx(hWorkerW, NULL, L"SHELLDLL_DefView", NULL);
WorkerW=hWorkerW;//得到WorkerW
hWorkerW = FindWindowEx(NULL, hWorkerW, L"WorkerW", NULL);
}
//隐藏窗口,不让mainwindow被挡住
ShowWindow(hWorkerW,0);
return FindWindow(L"Progman",NULL);
}
void SendMessageToDesktop()
{
PDWORD_PTR result = NULL;
//发送消息,召唤WorkerW
//参考:https://www.codeproject.com/articles/856020/draw-behind-desktop-icons-in-windows
SendMessageTimeout(FindWindow(L"Progman",NULL), 0x52c, 0, 0, SMTO_NORMAL, 1000, result);
}

HWND GetWorkerW()
{
findDesktopIconWnd();
return WorkerW;//得到WorkerW
}

main.cpp

1
2
3
4
5
MainWindow w;
SendMessageToDesktop();//发送消息
HWND desktopWnd = findDesktopIconWnd();//获取桌面句柄
if (desktopWnd)
SetParent((HWND)w.winId(), desktopWnd); // 设置为桌面壁纸级