110,534
社区成员
发帖
与我相关
我的任务
分享
private IntPtr GetDesktopPtr() {
//http://blog.csdn.net/mkdym/article/details/7018318
// 情况一
IntPtr hwndWorkerW = IntPtr.Zero;
IntPtr hShellDefView = IntPtr.Zero;
IntPtr hwndDesktop = IntPtr.Zero;
IntPtr hProgMan = Win32.FindWindow("ProgMan", null);
if (hProgMan != IntPtr.Zero) {
hShellDefView = Win32.FindWindowEx(hProgMan, IntPtr.Zero, "SHELLDLL_DefView", null);
if (hShellDefView != IntPtr.Zero) {
hwndDesktop = Win32.FindWindowEx(hShellDefView, IntPtr.Zero, "SysListView32", null);
}
}
if (hwndDesktop != IntPtr.Zero) return hwndDesktop;
// 情况二
while (hwndDesktop == IntPtr.Zero) {//必须存在桌面窗口层次
hwndWorkerW = Win32.FindWindowEx(IntPtr.Zero, hwndWorkerW, "WorkerW", null);//获得WorkerW类的窗口
if (hwndWorkerW == IntPtr.Zero) break;//未知错误
hShellDefView = Win32.FindWindowEx(hwndWorkerW, IntPtr.Zero, "SysListView32", null);
if (hShellDefView == IntPtr.Zero) continue;
hwndDesktop = Win32.FindWindowEx(hShellDefView, IntPtr.Zero, "SysListView32", null);
}
return hwndDesktop;
}
IntPtr desktopHwnd = GetDesktopPtr();
IntPtr ownHwnd = new WindowInteropHelper(this).Handle;
IntPtr result = Win32.SetParent(ownHwnd, desktopHwnd);