MFC CListCtrl
生活随笔
收集整理的這篇文章主要介紹了
MFC CListCtrl
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
列名、行內(nèi)容的添加、刪除。
據(jù)列名、行內(nèi)容長(zhǎng)度設(shè)置列寬。
排序。
?
提升權(quán)限
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; }?
取進(jìn)程全路徑
BOOL MFCTool::DosPathToNtPath( LPTSTR pszDosPath, LPTSTR pszNtPath ) {TCHAR szDriveStr[500];TCHAR szDrive[3];TCHAR szDevName[100];INT cchDevName;INT i;//檢查參數(shù)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 設(shè)備名return FALSE;cchDevName = lstrlen(szDevName);if(_tcsnicmp(pszDosPath, szDevName, cchDevName) == 0)//命中 {lstrcpy(pszNtPath, szDrive);//復(fù)制驅(qū)動(dòng)器lstrcat(pszNtPath, pszDosPath + cchDevName);//復(fù)制路徑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; }?
顯示進(jìn)程
void CDemoListCtrlDlg::ShowProcess( CListCtrl& listctrl ) {//清除所有內(nèi)容行 listctrl.DeleteAllItems();//清除所有列名while (listctrl.DeleteColumn(0)){}//設(shè)置顯示模式,網(wǎng)格+選中全行listctrl.SetExtendedStyle(listctrl.GetExtendedStyle() | LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);//內(nèi)容添加結(jié)束前停止生繪 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;}//自動(dòng)變列寬,據(jù)字段名&字段值長(zhǎng)度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);//目標(biāo)子菜單是菜單資源第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); }?
排序
靜態(tài)排序函數(shù)
響應(yīng)消息?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; }?
選中某行,取各列內(nèi)容
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; }?
轉(zhuǎn)載于:https://www.cnblogs.com/nonebutnow/p/6006009.html
總結(jié)
以上是生活随笔為你收集整理的MFC CListCtrl的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Electron】Electron开发
- 下一篇: UITextView: 响应键盘的 re