如何操作桌面上的图标
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
如何操作桌面上的图标,并取得图标的信息
2012 年 2 月 19 日 | Filed under: Windows程序设计 and tagged with: Windows API桌面图标是放在 SysListView32这个列表中,所以要想操作图标,要先得到了它的句柄,然后可以遍历它得到各桌面图标的句柄,下面的代码实现把桌面上的图标排成一个圆:
1
2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2void C***::OnSetDeskIcon()
{
HWNDhwndParent = ::FindWindow("Progman","Program Manager");
HWNDhwndSHELLDLL_DefView = ::FindWindowEx( hwndParent,
NULL,"SHELLDLL_DefView", NULL );
HWNDhwndSysListView32 = ::FindWindowEx( hwndSHELLDLL_DefView, NULL,"SysListView32","FolderView");
intNm = ListView_GetItemCount( hwndSysListView32 );
//取得图标的数目
for(inti = 0; i < Nm; i++ )
{
intx = 400 + 150*cos( i * 2 * 3.1415926/Nm );
inty = 400 + 150*sin( i * 2 * 3.1415926/Nm );
::SendMessage( hwndSysListView32, LVM_SETITEMPOSITION, i,
MAKELPARAM( x,y));
}
ListView_RedrawItems(hwndSysListView32, 0,
ListView_GetItemCount(hwndSysListView32) - 1);
::UpdateWindow(hwndSysListView32);
}
3
2
4
2
5
2
6
2
7
2
8
2
9
3
3
1
3
2
3
3
3
4
操作了图标,那么怎样得到图标的信息呢,如图标的名称,大小,位置等等,在上一篇文章Stealing Program's Memory 中提到了怎样取得别的进程中的信息,下面的代码实现了取得图标名称、位置、大小的功能:
1 2 3 4 5 6 7 8 9 10 11 12 13 void C***::OnSetDeskIcon()
{
HWNDhDestTop;
hDestTop = ::FindWindow("progman", NULL);
hDestTop = ::FindWindowEx(hDestTop, 0,"shelldll_defview",
NULL);
hDestTop = ::FindWindowEx(hDestTop, 0,"syslistview32",
NULL);
intcount=(int)::SendMessage( hDestTop, LVM_GETITEMCOUNT,
0, 0);
LVITEM lvi, *_lvi;
charitem[512], subitem[512];
char*_item, *_subitem;
unsignedlongpid;
HANDLEprocess;
GetWindowThreadProcessId( hDestTop, &pid);
process=OpenProcess(PROCESS_VM_OPERATION|PROCESS_VM_READ
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |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);
hTextMax=512;
for (inti=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);