获取桌面上所有程序图标的标题--上位机VC
获取桌面上所有程序图标的标题 功能展示 桌面上有各种各样的图标,图标下配有对应文字说明,如我的电脑图标,回收站图标网上邻居图标,我们当前例程实现桌面图标对应的文字获取,效果如图,点击按钮<获取桌面图标标题>例程会将桌面上所有图标对应标题全部罗列到列表中 要点提示 例程首先是桌面窗口句柄的获取,通过代码hWnd = FindWindowEx(NULL, NULL, “Progman”, NULL),hWnd = FindWindowEx(hWnd, NULL, “SHELLDLL_DefView”, NULL)hWnd = FindWindowEx(hWnd, NULL, “SysListView32”, NULL)可获得桌面窗口句柄;
通过函数SendMessage()向窗口句柄发送消息LVM_GETITEMCOUNT,可获得桌面图标个数 Int nItemCount =::SendMessage(hWnd, LVM_GETITEMCOUNT, (WPARAM)0, (LPARAM)0); 然后再通过函数::SendMessage(hWnd,LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)pData); 获得图标相关数据, 通过ReadProcessMemory(hProcess,pString, szText, ALLOC_SIZE, &BytesRead); 就可获得图标对应的标题,保存在szText变量里; 最后就是通过CListCtrl控件的成员函数SetItemText()显示获得的标题
实现功能 1.新建基于对话框的应用程序
2.拖拽一列表控件ID保持不变IDC_LIST1,添加一按钮《获取桌面图标标题》关联按钮的点击函数,获取标题 - void CGkbc8Dlg::OnButton1()
- {
- CListCtrl* pList = (CListCtrl*)GetDlgItem(IDC_LIST1);
- pList->DeleteAllItems();
- #define ALLOC_SIZE 200// bytes to allocate for icon text and LVITEM struct
- HWND hWnd; // handle to desktop window
- DWORD dwPID; // explorer process ID (associated with desktop)
- HANDLE hProcess; // handle to explorer process (associated with desktop)
- LPVOID pData; // pointer to LVITEM struct in explorer address space
- LPVOID pString; // pointer to icon text in explorer address space
- char szText[ALLOC_SIZE]; // char array of icon text in application address space
- SIZE_T BytesRead; // for ReadProcessMemory
- SIZE_T BytesWritten; // for WriteProcessMemory
- BOOL fResult; // for ReadProcessMemory/WriteProcessMemory
- LVITEM lvi; // LVITEM struct
- int i; // counter for enumeration
- int nItemCount; // icon item count
- // get desktop window handle "Progman=EXPLORER.EXE桌面图标下方窗口"
- if(((hWnd = FindWindowEx(NULL, NULL, "Progman", NULL)) == NULL) ||
- ((hWnd = FindWindowEx(hWnd, NULL, "SHELLDLL_DefView", NULL)) == NULL) ||
- ((hWnd = FindWindowEx(hWnd, NULL, "SysListView32", NULL)) == NULL))
- {
- MessageBox("Could not get desktop window.");
- goto Exit;
- }
- // get desktop window process ID (explorer.exe)
- GetWindowThreadProcessId(hWnd, &dwPID);
- // open process to get explorer process handle
- hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPID);
- // allocate memory in explorer's address space
- // allocate space for LVITEM struct
-
复制代码- pData = VirtualAllocEx(hProcess, NULL, ALLOC_SIZE, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
- // allocate space for string (i.e. "Network Neighborhood")
- pString = VirtualAllocEx(hProcess, NULL, ALLOC_SIZE, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
- // init LV_ITEM struct
- ZeroMemory(&lvi, sizeof(LVITEM));
- lvi.iItem = 0;
- lvi.iSubItem = 0;
- lvi.cchTextMax = 500;
- lvi.pszText = (char*)pString; // use alloc'd string space
- // write the contents of lvi into explorer's address space
- fResult = WriteProcessMemory(hProcess, pData, &lvi, sizeof(LVITEM), &BytesWritten);
- // get item count on desktop
- nItemCount = ::SendMessage(hWnd, LVM_GETITEMCOUNT, (WPARAM)0, (LPARAM)0);
- for(i = 0; i < nItemCount; i++)// enum all icons
- {
- // get item's name
- ::SendMessage(hWnd, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)pData);
- // read back lvi struct (for debugging purposes only)
- fResult = ReadProcessMemory(hProcess, pData, szText, ALLOC_SIZE, &BytesRead);
- // read text of queried item
- fResult = ReadProcessMemory(hProcess, pString, szText, ALLOC_SIZE, &BytesRead);
- CString sIndex;
- sIndex.Format("%d",i);
- pList->InsertItem(i,"");
- pList->SetItemText(i,0,sIndex);
- pList->SetItemText(i,1,szText);
- }
- // free alloc'd memory for string and LVITEM structs
- if(!VirtualFreeEx(hProcess, pString, ALLOC_SIZE, MEM_DECOMMIT))
- {
- MessageBox("VirtualFreeEx failed.");
- goto Exit;
- }
-
复制代码- if(!VirtualFreeEx(hProcess, pData, ALLOC_SIZE, MEM_DECOMMIT))
- {
- MessageBox("VirtualFreeEx failed.");
- goto Exit;
- }
- CloseHandle(hProcess); // close process handle
- Exit:
- return;
- }
复制代码3.在程序初始化时记得初始化列表控件,给列表添加表头 CListCtrl* pList= (CListCtrl*)GetDlgItem(IDC_LIST1); pList->ModifyStyle(0,LVS_REPORT,0); pList->InsertColumn(0,"索引",LVCFMT_LEFT,50); pList->InsertColumn(1,"标题",LVCFMT_LEFT,500);
我们来演示下功能实现的整个过程
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!
|