包含图标的窗口实际上是DesktopWindow的一个子窗口。
确切地讲,Desktop Window包含一个无标题的、类名为“SHELLDLL_DefView”的子窗口,
这个子窗口又包含一个无标题的、类名为“SysListView32”的子窗口——这才是那个真正包含桌面图标的窗口。
桌面图标是放在 SysListView32这个列表中,下面得到了它的句柄,
HWND hwndParent = ::FindWindow(L"Progman", L"Program Manager");
HWND hwndSHELLDLL_DefView = ::FindWindowEx(hwndParent,NULL, L"shelldll_defview", NULL);
HWND hwndSysListView32 = ::FindWindowEx(hwndSHELLDLL_DefView, NULL, L"SysListView32", L"FolderView");
说明:
'ProgMan'是Windows桌面的默认类名,我们平时看到的桌面的图标所在,默认情况下是一个Listview,属于ProgMan的一个子窗口,父子关系如下:
类名:Progman 窗口标题:Program Manager
类名:SHELLDLL_DefView
类名:SysListView32 窗口标题:FolderView
抓图工具截图如下:
如何遍历它得到各个桌面图标的句柄呢?
如果桌面上有文件夹,文件什么的怎么分辨?
以下三句得到列表的句柄,哪个能告诉我下面该怎么做,我的目标是通过 MoveWindow函数来任意改变桌面上所有图标的位置。。。
<pre name="code" class="cpp">void C***::OnSetDeskIcon() { HWND hwndParent = ::FindWindow( "Progman", "Program Manager" ); HWND hwndSHELLDLL_DefView = ::FindWindowEx( hwndParent, NULL, "SHELLDLL_DefView", NULL ); HWND hwndSysListView32 = ::FindWindowEx( hwndSHELLDLL_DefView, NULL, "SysListView32", "FolderView" ); int Nm = ListView_GetItemCount( hwndSysListView32 ); int sNm = 0; if( Nm >= 10 ) { sNm = 10; } else { sNm = Nm; } for( int i = 0; i < sNm; i++ ) { int x = 400 + 150*cos( i*36*3.1415926/180 ); int y = 400 + 150*sin( i*36*3.1415926/180 ); ::SendMessage( hwndSysListView32, LVM_SETITEMPOSITION, i, MAKELPARAM( x,y)); } ListView_RedrawItems(hwndSysListView32, 0, ListView_GetItemCount(hwndSysListView32) - 1); ::UpdateWindow(hwndSysListView32); } 上面的代码 实现把桌面前10图标排成一个圆 楼主去查ListView_RedrawItems这类函数的用法
void C***::OnSetDeskIcon() { HWND hDestTop; hDestTop = ::FindWindow("progman", NULL); hDestTop = ::FindWindowEx(hDestTop, 0, "shelldll_defview", NULL); hDestTop = ::FindWindowEx(hDestTop, 0, "syslistview32", NULL); int count=(int)::SendMessage( hDestTop, LVM_GETITEMCOUNT, 0, 0); LVITEM lvi, *_lvi; char item[512], subitem[512]; char *_item, *_subitem; unsigned long pid; HANDLE process; GetWindowThreadProcessId( hDestTop, &pid); process=OpenProcess(PROCESS_VM_OPERATION|PROCESS_VM_READ|PROCESS_VM_WRITE|PROCESS_QUERY_INFORMATION, FALSE, pid); _lvi=(LVITEM*)VirtualAllocEx(process, NULL, sizeof(LVITEM), MEM_COMMIT, PAGE_READWRITE); _item=(char*)VirtualAllocEx(process, NULL, 512, MEM_COMMIT, PAGE_READWRITE); _subitem=(char*)VirtualAllocEx(process, NULL, 512, MEM_COMMIT, PAGE_READWRITE); RECT rc; rc.left = LVIR_ICON; //这个一定要设定 可以去看MSDN关于LVM_GETITEMRECT的说明 RECT* _rc =(RECT*)VirtualAllocEx( process, NULL, sizeof(RECT), MEM_COMMIT, PAGE_READWRITE); lvi.cchTextMax=512; for( int i=0; i< 10; i++) { lvi.iSubItem=0; lvi.pszText=_item; WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL); ::SendMessage( hDestTop, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi); lvi.iSubItem=1; lvi.pszText=_subitem; WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL); ::SendMessage( hDestTop, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi); ::WriteProcessMemory( process, _rc, &rc, sizeof(rc), NULL); ::SendMessage( hDestTop, LVM_GETITEMRECT, (WPARAM)i, (LPARAM)_rc); ReadProcessMemory(process, _item, item, 512, NULL); ReadProcessMemory(process, _subitem, subitem, 512, NULL); ReadProcessMemory(process, _rc, &rc, sizeof(rc), NULL); CString str; str.Format("LF:%d TP:%d RT:%d BT:%d", rc.left,rc.top,rc.right,rc.bottom); AfxMessageBox(str); str.Format("%s - %s/n", item, subitem); AfxMessageBox( str ); } VirtualFreeEx(process, _lvi, 0, MEM_RELEASE); VirtualFreeEx(process, _item, 0, MEM_RELEASE); VirtualFreeEx(process, _subitem, 0, MEM_RELEASE); VirtualFreeEx(process, _rc, 0, MEM_RELEASE); CloseHandle( process ); }