列名、行内容的添加、删除。
据列名、行内容长度设置列宽。
排序。
提升权限
BOOL CDemoListCtrlApp::EnableDebugPrivilege() { HANDLE token; if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&token)) { return FALSE; } TOKEN_PRIVILEGES tkp; tkp.PrivilegeCount = 1; ::LookupPrivilegeValue(NULL,SE_DEBUG_NAME,&tkp.Privileges[0].Luid); tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; if(!AdjustTokenPrivileges(token,FALSE,&tkp,sizeof(tkp),NULL,NULL)) { return FALSE; } CloseHandle(token); return TRUE; }
取进程全路径
BOOL MFCTool::DosPathToNtPath( LPTSTR pszDosPath, LPTSTR pszNtPath ) {TCHAR szDriveStr[500];TCHAR szDrive[3];TCHAR szDevName[100];INT cchDevName;INT i;//检查参数if(!pszDosPath || !pszNtPath )return FALSE;//获取本地磁盘字符串if(GetLogicalDriveStrings(sizeof(szDriveStr), szDriveStr)){for(i = 0; szDriveStr[i]; i += 4){if(!lstrcmpi(&(szDriveStr[i]), _T("A:\\")) || !lstrcmpi(&(szDriveStr[i]), _T("B:\\")))continue;szDrive[0] = szDriveStr[i];szDrive[1] = szDriveStr[i + 1];szDrive[2] = '\0';if(!QueryDosDevice(szDrive, szDevName, 100))//查询 Dos 设备名return FALSE;cchDevName = lstrlen(szDevName);if(_tcsnicmp(pszDosPath, szDevName, cchDevName) == 0)//命中 {lstrcpy(pszNtPath, szDrive);//复制驱动器lstrcat(pszNtPath, pszDosPath + cchDevName);//复制路径return TRUE;} }}lstrcpy(pszNtPath, pszDosPath);return FALSE; }BOOL MFCTool::GetProcessFullPath( DWORD dwPID, TCHAR pszFullPath[MAX_PATH] ) {TCHAR szImagePath[MAX_PATH];HANDLE hProcess;if(!pszFullPath)return FALSE;pszFullPath[0] = '\0';hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, dwPID);if(!hProcess)return FALSE;if(!GetProcessImageFileName(hProcess, szImagePath, MAX_PATH)){CloseHandle(hProcess);return FALSE;}if(!DosPathToNtPath(szImagePath, pszFullPath)){CloseHandle(hProcess);return FALSE;}CloseHandle(hProcess);return TRUE; }
显示进程
void CDemoListCtrlDlg::ShowProcess( CListCtrl& listctrl ) {//清除所有内容行 listctrl.DeleteAllItems();//清除所有列名while (listctrl.DeleteColumn(0)){}//设置显示模式,网格+选中全行listctrl.SetExtendedStyle(listctrl.GetExtendedStyle() | LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);//内容添加结束前停止生绘 listctrl.SetRedraw(FALSE);//添加列名listctrl.InsertColumn(0, "PID");listctrl.InsertColumn(1, "NAME");listctrl.InsertColumn(2, "FULLPATH");HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);if (INVALID_HANDLE_VALUE != hSnap){PROCESSENTRY32 item;item.dwSize = sizeof(item);if (Process32First(hSnap, &item)){CString str;int iLine = 0;char chPath[MAX_PATH];MFCTool mfctool;do {str.Format("%d", item.th32ProcessID);listctrl.InsertItem(iLine, str);listctrl.SetItemText(iLine, 1, item.szExeFile);memset(chPath, 0, MAX_PATH);mfctool.GetProcessFullPath(item.th32ProcessID, chPath);str.Format("%s", chPath);listctrl.SetItemText(iLine, 2, str);iLine++;} while (Process32Next(hSnap, &item));}CloseHandle(hSnap);hSnap = INVALID_HANDLE_VALUE;}//自动变列宽,据字段名&字段值长度int iCount = listctrl.GetHeaderCtrl()->GetItemCount();for (int i = 0; i < iCount; i++){listctrl.SetColumnWidth(i, LVSCW_AUTOSIZE_USEHEADER);}listctrl.SetRedraw(TRUE); }
右键显示菜单
void CDemoListCtrlDlg::OnNMRClickList1(NMHDR *pNMHDR, LRESULT *pResult) {LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);//弹出菜单 CPoint pos;if (GetCursorPos(&pos)){CMenu menu;//加载菜单资源 menu.LoadMenu(IDR_MENU1);//目标子菜单是菜单资源第0列menu.GetSubMenu(0)->TrackPopupMenu(TPM_RIGHTBUTTON, pos.x, pos.y, this);}*pResult = 0; }
右键菜单:刷新
添加菜单资源
xxxDlg.h
class CDemoListCtrlDlg : public CDialog { //...public:afx_msg void OnRefreshList1(); }
xxxDlg.cpp
BEGIN_MESSAGE_MAP(CDemoListCtrlDlg, CDialog) //... ON_COMMAND(ID_REFRESHLIST1, OnRefreshList1) END_MESSAGE_MAP()void CDemoListCtrlDlg::OnRefreshList1() {ShowProcess(m_listctrl); }
排序
静态排序函数
响应消息 ON_NOTIFY(NM_CLICK, IDC_LIST1, &CDemoListCtrlDlg::OnNMClickList1)
xxxDlg.h
static DWORD m_dwSortColumn;static BOOL m_bAscending;//Descending/Ascendingstatic int CALLBACK CompareList1(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);afx_msg void OnLvnColumnclickList1(NMHDR *pNMHDR, LRESULT *pResult);
xxxDlg.cpp
int CALLBACK CDemoListCtrlDlg::CompareList1( LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort ) {CListCtrl* pListCtrl = (CListCtrl*)lParamSort;int iCompare = 0; CString strCompare1 = pListCtrl->GetItemText(lParam1, m_dwSortColumn);CString strCompare2 = pListCtrl->GetItemText(lParam2, m_dwSortColumn);switch (m_dwSortColumn){case 0://by idiCompare = atof((char*)strCompare1.GetBuffer(0)) <= atof((char*)strCompare2.GetBuffer(0)) ? -1 : 1; break;default://by stringiCompare = strCompare1.CompareNoCase(strCompare2);break;}if(m_bAscending) {return iCompare;}return -iCompare; }void CDemoListCtrlDlg::OnLvnColumnclickList1(NMHDR *pNMHDR, LRESULT *pResult) {LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;m_dwSortColumn = pNMListView->iSubItem;m_bAscending = !m_bAscending;//Descending or Ascending int iCount = m_listctrl.GetItemCount();for (int i = 0; i < iCount; i++) { m_listctrl.SetItemData(i, i); }m_listctrl.SortItems(CompareList1, (LPARAM)&m_listctrl);*pResult = 0; }
选中某行,取各列内容
void CDemoListCtrlDlg::OnNMClickList1(NMHDR *pNMHDR, LRESULT *pResult) {LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);//selected lineint iSelectedLine = m_listctrl.GetSelectionMark();if (-1 != iSelectedLine){CString str = m_listctrl.GetItemText(iSelectedLine, 0) + "|" + m_listctrl.GetItemText(iSelectedLine, 1)+ "|" + m_listctrl.GetItemText(iSelectedLine, 2);//MessageBox(str); SetWindowText(str);}*pResult = 0; }