#include <windows.h> #include <commctrl.h> #include <tlhelp32.h> #include "resource.h" // 链接到comctl32.lib库 #pragma comment(lib,"comctl32.lib") BOOL __stdcall DlgProc(HWND, UINT, WPARAM, LPARAM); void UpdateProcess(HWND hWndList); int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int) { // 初始化Comctl32.dll库 ::InitCommonControls(); ::DialogBoxParam(hInstance, (LPCTSTR)IDD_DIALOG1, NULL, DlgProc, NULL); return 0; } BOOL __stdcall DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_INITDIALOG: { // 初始化列表视图控件 HWND hWndList = ::GetDlgItem(hDlg, IDC_LIST); // 设置它的扩展风格 ::SendMessage(hWndList, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES); LVCOLUMN column; // 指定LVCOLUMN结构中的pszText、fmt、cx域有效 column.mask = LVCF_TEXT|LVCF_FMT|LVCF_WIDTH; // 设置有效的域的属性 column.fmt = LVCFMT_CENTER; // 指定文本居中显示 column.cx = 100; // 指定此栏的宽度 column.pszText = "进程名称"; // 指定此栏显示的文本 // 添加一个新的专栏 ::SendMessage(hWndList, LVM_INSERTCOLUMN, 0, (LPARAM)&column); // 再添加一个专栏 column.pszText = "PID"; column.cx = 50; ::SendMessage(hWndList, LVM_INSERTCOLUMN, 1, (LPARAM)&column); // 刷新进程列表 UpdateProcess(hWndList); } break; case WM_COMMAND: switch(LOWORD(wParam)) { case IDCANCEL: ::EndDialog(hDlg, IDCANCEL); break; } break; } return 0; } void UpdateProcess(HWND hWndList) { // 删除所有的项 ::SendMessage(hWndList, LVM_DELETEALLITEMS, 0, 0); int nItem = 0; // 项计数 PROCESSENTRY32 pe32 = { sizeof(PROCESSENTRY32) }; HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if(hProcessSnap == INVALID_HANDLE_VALUE) return; if(Process32First(hProcessSnap, &pe32)) { do { // 取得进程ID号 char szID[56]; wsprintf(szID, "%u", pe32.th32ProcessID); // 插入一个项 LVITEM item = { 0 }; item.iItem = nItem; item.mask = LVIF_TEXT; // 指定pszText域有效 item.pszText = (LPTSTR)pe32.szExeFile; // 设置文本 ::SendMessage(hWndList, LVM_INSERTITEM, 0, (long)&item); // 设置新项的文本 LVITEM lvi; lvi.iSubItem = 1; // 指定要设置第1个专栏的文本 lvi.pszText = (LPTSTR)szID; // 要设置的文本 ::SendMessage(hWndList, LVM_SETITEMTEXT, nItem, (LPARAM)&lvi); nItem++; } while(Process32Next(hProcessSnap, &pe32)); } ::CloseHandle(hProcessSnap); }
在对话框上画一个列表控件后,要把属性中的View设置为:Report
效果图: