首页 > 代码库 > SDK 操作 list-view control 实例 -- 遍历进程

SDK 操作 list-view control 实例 -- 遍历进程

遍历窗口,获得控件句柄

1 EnumChildWindows(hwndDlg, (WNDENUMPROC)EnumChildProc, NULL);

回调函数

 1 BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam ) 2 { 3     char strCLSName[MAXBYTE] = {0}; 4     GetClassName(hwnd, strCLSName, MAXBYTE); 5     if (strcmp(strCLSName, "SysListView32") == 0)//找到列表控件 6     { 7         InsertColumn(hwnd);//添加标题 8         EnumProgress(hwnd);//添加内容 9         return true;//结束遍历10     }11 12     return false;//继续遍历子窗口13 }

添加标题

 1 void InsertColumn(HWND hList) 2 { 3     LV_COLUMN lvc = {0}; 4      5     lvc.mask = LVCF_TEXT | LVCF_WIDTH; 6     lvc.pszText = "PID"; 7     lvc.cx = 80; 8     SendMessage(hList, LVM_INSERTCOLUMN, 0, (long)&lvc); 9     lvc.pszText = "父进程PID";10     lvc.cx = 100;11     SendMessage(hList, LVM_INSERTCOLUMN, 1, (long)&lvc);12     lvc.pszText = "映像名称";13     lvc.cx = 200;14     SendMessage(hList, LVM_INSERTCOLUMN, 2, (long)&lvc);15 }

添加内容

 1 BOOL EnumProgress(HWND hList) 2 { 3     BOOL bRet = FALSE;  4     HANDLE hProcessSnap = NULL; 5      6     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);  7     if (hProcessSnap == INVALID_HANDLE_VALUE)  8     { 9         return FALSE; 10     }11     12     PROCESSENTRY32 pe32 = {0};13     pe32.dwSize = sizeof(PROCESSENTRY32);14     if (Process32First(hProcessSnap, &pe32)) 15     { 16         LVITEM lvi = {0};17         lvi.mask = LVIF_TEXT;18         char strTmp[MAXBYTE] = {0};19         do 20         { 21             ZeroMemory(strTmp, MAXBYTE);22             sprintf(strTmp, "%d", pe32.th32ProcessID);23             lvi.iItem = 0;24             lvi.iSubItem = 0;//发送LVM_INSERTITEM消息时,subitem必须为025             lvi.pszText = strTmp;26             int nIndx = SendMessage(hList, LVM_INSERTITEM, 0, (LPARAM)&lvi);//返回值为item的索引27             28             ZeroMemory(strTmp, MAXBYTE);29             sprintf(strTmp, "%d", pe32.th32ParentProcessID);30             lvi.iItem = nIndx;31             lvi.iSubItem = 1;32             lvi.pszText = strTmp;33             SendMessage(hList, LVM_SETITEM, 0, (LPARAM)&lvi);34             35             lvi.iSubItem = 2;36             lvi.iItem = nIndx;37             lvi.pszText = pe32.szExeFile;38             SendMessage(hList, LVM_SETITEM, 0, (LPARAM)&lvi);39         } 40         while (Process32Next(hProcessSnap, &pe32)); 41         bRet = TRUE; 42     } 43     else 44     {45         bRet = FALSE;46     }47     48     CloseHandle (hProcessSnap); 49     return bRet; 50 }