QT程序嵌入到桌面(Win+D显示桌面不最小化)
- 在main.c加入头文件windows.h及接口
#include <windows.h>
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;
return FALSE;
}
HWND findDesktopIconWnd()
{
HWND resultHwnd = NULL;
EnumWindows((WNDENUMPROC)enumUserWindowsCB, (LPARAM)&resultHwnd);
return resultHwnd;
}
- 指定父窗口为桌面
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
HWND desktopHwnd = findDesktopIconWnd();
// 设置桌面为父窗口
if (desktopHwnd) {
SetParent((HWND)w.winId(), desktopHwnd);
}
w.show();
return a.exec();
}
注:win7 win10 测试ok