In the learning interface design, because of some controls are not familiar with, so also refer to others. The core code is as follows.
voidCtasklistdlg::expand () {m_list. SetExtendedStyle (Lvs_ex_gridlines|lvs_ex_fullrowselect); M_list. InsertColumn (0, L"Process Name",0, -); M_list. InsertColumn (1, L"Pid",0, -); M_list. InsertColumn (2, L"Path",0,380);}voidCtasklistdlg::onnmrclicklist (NMHDR *pnmhdr, LRESULT *PResult) {lpnmitemactivate pnmitemactivate= reinterpret_cast<lpnmitemactivate>(PNMHDR); //Todo:add your control notification handler code hereCMenu popup; Popup. LoadMenu (IDR_MENU1); CMenu* PM = Popup. GetSubMenu (0); CPoint p; GetCursorPos (&p); intCount = pm->Getmenuitemcount (); if(M_list. Getselectedcount () = =0)//If you do not select { for(inti =0; I < count; i++) {PM->enablemenuitem (i, Mf_byposition | mf_disabled | mf_grayed);//Menu All dimmed}} PM->trackpopupmenu (Tpm_leftalign, p.x, P.Y, This); *presult =0;} #include<Tlhelp32.h>#include<Psapi.h>#pragmaComment (lib, "Psapi.lib")bool Ctasklistdlg::getprocesslist () {bool result=FALSE; HANDLE Hsnap= CreateToolhelp32Snapshot (th32cs_snapprocess,0); if(Hsnap = =Invalid_handle_value)returnFALSE; intIDX =0; CString Strid; HANDLE hprocess=NULL; CString strpath, Strsys (_t ("SystemRoot")); PROCESSENTRY32 Info= {0 }; Info.dwsize=sizeof(PROCESSENTRY32); BOOL BRet= Process32First (hsnap,&info); while(bRet) {hprocess= OpenProcess (Process_query_information |Process_vm_read, FALSE, Info.th32processid); if(hprocess) {strpath.empty (); DWORD Dwret=Getmodulefilenameex (hprocess, NULL, Strpath.getbuffersetlength (MAX_PATH), MAX_PATH); Strpath.releasebuffer (); Strpath.trimleft (_t ("\\?")); if(Strpath.left (Strsys.getlength ()). CompareNoCase (strsys) = =0) {TCHAR Szwin[max_path]= {0 }; GetWindowsDirectory (Szwin,max_path); Strpath.replace (Strsys,szwin); } shfileinfo Shinfo= {0 }; Shgetfileinfo (strpath,0, &shinfo,sizeof(shfileinfo), Shgfi_smallicon |Shgfi_icon); } idx=m_list. InsertItem (m_list. GetItemCount (), info.szexefile); Strid.empty (); Strid.format (_t ("%d"), INFO.TH32PROCESSID); M_list. Setitemtext (IDX,1, Strid); M_list. Setitemtext (IDX,2, strpath); BRet= Process32Next (Hsnap, &info); } returnTRUE;} BOOL ctasklistdlg::adjustprivileges () {HANDLE Htoken=NULL; token_privileges TP= {0 }; Token_privileges OLDTP= {0 }; DWORD dwsize=sizeof(token_privileges); LUID LUID= {0 }; if(! OpenProcessToken (GetCurrentProcess (), Token_adjust_privileges | Token_query, &htoken)) { if(GetLastError () = =error_call_not_implemented)returnTRUE; Else returnFALSE; } if(! Lookupprivilegevalue (NULL, Se_debug_name, &Luid)) {CloseHandle (htoken); returnFALSE; } TP. Privilegecount=1; Tp. privileges[0]. Luid =Luid; Tp. privileges[0]. Attributes =se_privilege_enabled; /*Adjust Token Privileges*/ if(! AdjustTokenPrivileges (Htoken, FALSE, &TP,sizeof(token_privileges), &OLDTP, &dwsize)) {CloseHandle (htoken); returnFALSE; } //Close HandlesCloseHandle (Htoken); returnTRUE;}voidctasklistdlg::onkillprocess () {//todo:add Your command handler code here//DWORD ProcessID = 0;DWORD dwprocess; wchar_t Szbuf[max_path]= {0}; intindex = m_list. Getselectionmark ();//gets the location of the cursorM_list. GetItemText (Index,1, Szbuf, MAX_PATH);//gets the number of row cursors and then obtains the dataDwprocess =_wtol (SZBUF); HANDLE hprocess=openprocess (process_all_access, FALSE, dwprocess); if(NULL = =hprocess) {MessageBox (L"Open the process handle failed! "); } if(TerminateProcess (hprocess,0) ) {Processitem*Pprocitem; Pprocitem= (processitem*) m_list. GetItemData (index); DeletePprocitem; M_list. DeleteItem (index); MessageBox (L"Process ended successfully"); } Else{MessageBox (L"End Process failed! "); }}voidCtasklistdlg::oninfoexit () {exit (0); //todo:add Your command handler code here}voidCtasklistdlg::oninforefresh () {//todo:add Your command handler code herem_list. SetRedraw (FALSE); M_list. Deleteallitems (); Getprocesslist (); M_list. SetRedraw (TRUE); M_list. Invalidate (); M_list. UpdateWindow ();}
MFC Task Manager Design