HWND FindDesktopIconWnd()
{
HWND hWndDesktop = NULL;
HWND hProgMan = ::FindWindow(L"Progman", NULL);
if (hProgMan)
{
HWND hShellDefView = ::FindWindowEx(hProgMan, NULL, L"SHELLDLL_DefView", NULL);
if (hShellDefView) hWndDesktop = ::FindWindowEx(hShellDefView, NULL, L"SysListView32", NULL);
}
if (hWndDesktop) return hWndDesktop;
HWND hwndWorkerW = NULL;
HWND hShellDefView = NULL;
HWND hwndDesktop = NULL;
while (hwndDesktop == NULL)//必须存在桌面窗口层次
{
hwndWorkerW = ::FindWindowEx(0, hwndWorkerW, L"WorkerW", NULL);//获得WorkerW类的窗口
if (hwndWorkerW == NULL) break;//未知错误
hShellDefView = ::FindWindowEx(hwndWorkerW, NULL, L"SHELLDLL_DefView", NULL);
if (hShellDefView == NULL) continue;
hwndDesktop = hShellDefView;
//hwndDesktop = ::FindWindowEx(hShellDefView, NULL, L"SysListView32", NULL);
//hwndDesktop = ::FindWindowEx(hwndDesktop, NULL, L"SysHeader32", NULL);
}
return hwndDesktop;
}