TabControl+ListView
#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); TCITEM tci={0}; tci.mask=TCIF_TEXT; tci.pszText="进程"; TabCtrl_InsertItem(GetDlgItem(hDlg,IDC_TAB1),0,&tci); tci.pszText="标签2"; TabCtrl_InsertItem(GetDlgItem(hDlg,IDC_TAB1),1,&tci); tci.pszText="标签3"; TabCtrl_InsertItem(GetDlgItem(hDlg,IDC_TAB1),2,&tci); //进程标签分栏 LVCOLUMN lvi={0}; lvi.mask=LVCF_WIDTH|LVCF_TEXT|LVCF_FMT; lvi.pszText="进程名称"; lvi.cx=90; lvi.fmt=LVCFMT_CENTER; ListView_InsertColumn(GetDlgItem(hDlg,IDC_LIST),0,&lvi); lvi.pszText="PID"; ListView_InsertColumn(GetDlgItem(hDlg,IDC_LIST),1,&lvi); //标签2分栏 lvi.mask=LVCF_WIDTH|LVCF_TEXT|LVCF_FMT; lvi.pszText="进程名称2"; lvi.cx=90; lvi.fmt=LVCFMT_CENTER; ListView_InsertColumn(GetDlgItem(hDlg,IDC_LIST1),0,&lvi); lvi.pszText="PID2"; ListView_InsertColumn(GetDlgItem(hDlg,IDC_LIST1),1,&lvi); // 刷新进程列表 UpdateProcess(hWndList); } break; case WM_NOTIFY: { if (*(LPDWORD)((LPBYTE)lParam+8)==TCN_SELCHANGE ) { int i = SendMessage(GetDlgItem(hDlg,IDC_TAB1),TCM_GETCURSEL,0,0); //第一个标签被点击时 if (i == 0) { UpdateProcess(::GetDlgItem(hDlg, IDC_LIST)); //显示ID为IDC_LIST的列表控件 ShowWindow(GetDlgItem(hDlg,IDC_LIST),true); //隐藏ID为IDC_LIST1的列表控件 ShowWindow(GetDlgItem(hDlg,IDC_LIST1),false); } //第二个标签被点击时 else if (i==1) { //隐藏ID为IDC_LIST的列表控件 ShowWindow(GetDlgItem(hDlg,IDC_LIST),false); //显示ID为IDC_LIST1的列表控件 ShowWindow(GetDlgItem(hDlg,IDC_LIST1),true); } //第三个标签被点击时 else if (i==2) { //隐藏ID为IDC_LIST的列表控件 ShowWindow(GetDlgItem(hDlg,IDC_LIST),false); //隐藏ID为IDC_LIST1的列表控件 ShowWindow(GetDlgItem(hDlg,IDC_LIST1),false); MessageBox(NULL,"标签3","您点击了标签3!",MB_OK); } } } 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); }
初始化消息最好添加:
//隐藏ID为IDC_LIST1的列表控件
ShowWindow(GetDlgItem(hDlg,IDC_LIST1),false);