代码实现如下:
#include "widget.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;}void setParentDesktop(QWidget *pWidget){HWND resultHwnd = NULL;EnumWindows((WNDENUMPROC)enumUserWindowsCB, (LPARAM)&resultHwnd);// 设置桌面为父窗口if (resultHwnd) {SetParent((HWND)pWidget->winId(), resultHwnd);}}Widget::Widget(QWidget *parent): QWidget(parent){setParentDesktop(this);}Widget::~Widget(){}