主要界面如下:
主要代码如下:
1 BOOL CEnumProcessDlg::OnInitDialog() 2 { 3 CDialog::OnInitDialog(); 4 5 // 将“关于...”菜单项添加到系统菜单中。 6 7 // IDM_ABOUTBOX 必须在系统命令范围内。 8 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 9 ASSERT(IDM_ABOUTBOX < 0xF000); 10 11 CMenu* pSysMenu = GetSystemMenu(FALSE); 12 if (pSysMenu != NULL) 13 { 14 CString strAboutMenu; 15 strAboutMenu.LoadString(IDS_ABOUTBOX); 16 if (!strAboutMenu.IsEmpty()) 17 { 18 pSysMenu->AppendMenu(MF_SEPARATOR); 19 pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 20 } 21 } 22 23 // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动 24 // 执行此操作 25 SetIcon(m_hIcon, TRUE); // 设置大图标 26 SetIcon(m_hIcon, FALSE); // 设置小图标 27 28 // TODO: 在此添加额外的初始化代码 29 LONG lStyle; 30 lStyle = GetWindowLong(m_ctrlList.m_hWnd, GWL_STYLE); 31 lStyle &= ~LVS_TYPEMASK; 32 lStyle |= LVS_REPORT; 33 SetWindowLong(m_ctrlList.m_hWnd, GWL_STYLE, lStyle); 34 35 DWORD dwStyle = m_ctrlList.GetExtendedStyle(); 36 dwStyle |= LVS_EX_FULLROWSELECT; 37 dwStyle |= LVS_EX_GRIDLINES; 38 m_ctrlList.SetExtendedStyle(dwStyle); 39 m_ctrlList.InsertColumn(0, _T("Process ID"), LVCFMT_LEFT, 75); 40 m_ctrlList.InsertColumn(1, _T("Process Name"), LVCFMT_LEFT, 120); 41 m_ctrlList.InsertColumn(2, _T("Parent Process ID"), LVCFMT_LEFT, 115); 42 m_ctrlList.InsertColumn(3, _T("Thread Count"), LVCFMT_LEFT, 90); 43 ListProcess(); 44 45 return TRUE; // 除非将焦点设置到控件,否则返回 TRUE 46 } 47 48 void CEnumProcessDlg::OnSysCommand(UINT nID, LPARAM lParam) 49 { 50 if ((nID & 0xFFF0) == IDM_ABOUTBOX) 51 { 52 CAboutDlg dlgAbout; 53 dlgAbout.DoModal(); 54 } 55 else 56 { 57 CDialog::OnSysCommand(nID, lParam); 58 } 59 } 60 61 // 如果向对话框添加最小化按钮,则需要下面的代码 62 // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序, 63 // 这将由框架自动完成。 64 65 void CEnumProcessDlg::OnPaint() 66 { 67 if (IsIconic()) 68 { 69 CPaintDC dc(this); // 用于绘制的设备上下文 70 71 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0); 72 73 // 使图标在工作矩形中居中 74 int cxIcon = GetSystemMetrics(SM_CXICON); 75 int cyIcon = GetSystemMetrics(SM_CYICON); 76 CRect rect; 77 GetClientRect(&rect); 78 int x = (rect.Width() - cxIcon + 1) / 2; 79 int y = (rect.Height() - cyIcon + 1) / 2; 80 81 // 绘制图标 82 dc.DrawIcon(x, y, m_hIcon); 83 } 84 else 85 { 86 CDialog::OnPaint(); 87 } 88 } 89 90 //当用户拖动最小化窗口时系统调用此函数取得光标显示。 91 // 92 HCURSOR CEnumProcessDlg::OnQueryDragIcon() 93 { 94 return static_cast<HCURSOR>(m_hIcon); 95 } 96 97 98 void CEnumProcessDlg::ListProcess(void) 99 { 100 HANDLE hProcessSnap; 101 PROCESSENTRY32 pe32; 102 103 hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 104 if (hProcessSnap == INVALID_HANDLE_VALUE) 105 { 106 CString strErr; 107 strErr.Format(_T("CreateToolhelp32Snapshot failed:%d"), GetLastError()); 108 AfxMessageBox(strErr); 109 return; 110 } 111 112 pe32.dwSize = sizeof(PROCESSENTRY32); 113 114 int index = 0; 115 if (!Process32First(hProcessSnap, &pe32)) 116 { 117 CString strErr; 118 strErr.Format(_T("Process32First failed:%d"), GetLastError()); 119 AfxMessageBox(strErr); 120 CloseHandle(hProcessSnap); 121 return; 122 } 123 124 do 125 { 126 CString strID; 127 strID.Format(_T("%d"), pe32.th32ProcessID); 128 m_ctrlList.InsertItem(index, strID); 129 m_ctrlList.SetItemText(index, 1, pe32.szExeFile); 130 strID.Format(_T("%d"), pe32.th32ParentProcessID); 131 m_ctrlList.SetItemText(index, 2, strID); 132 strID.Format(_T("%d"), pe32.cntThreads); 133 m_ctrlList.SetItemText(index++, 3, strID); 134 } while (Process32Next(hProcessSnap, &pe32)); 135 136 CloseHandle(hProcessSnap); 137 } 138 139 void CEnumProcessDlg::OnContextMenu(CWnd* pWnd, CPoint point) 140 { 141 // TODO: 在此处添加消息处理程序代码 142 CMenu menu; 143 144 if (menu.CreatePopupMenu() == FALSE) 145 { 146 return; 147 } 148 149 if (pWnd == &m_ctrlList) 150 { 151 menu.AppendMenu(MF_STRING , ID_SCANMODULE , _T("查看依赖模块")); 152 if(m_ctrlList.GetSelectedCount() < 0) 153 { 154 menu.EnableMenuItem(ID_SCANMODULE , MF_DISABLED | MF_GRAYED); 155 } 156 } 157 158 menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this); 159 } 160 161 void CEnumProcessDlg::ScanModule(UINT nID) 162 { 163 switch (nID) 164 { 165 case ID_SCANMODULE: 166 { 167 CString strProcessID = m_ctrlList.GetItemText(m_ctrlList.GetSelectionMark(), 0); 168 CProcessModule dlgProcessModule; 169 dlgProcessModule.m_dwProcessID = _tcstoul(strProcessID, 0, 10); 170 dlgProcessModule.DoModal(); 171 } 172 break; 173 default: 174 break; 175 } 176 } 177 178 void CEnumProcessDlg::OnNMDblclkListProcess(NMHDR *pNMHDR, LRESULT *pResult) 179 { 180 // TODO: 在此添加控件通知处理程序代码 181 CString strProcessID = m_ctrlList.GetItemText(m_ctrlList.GetSelectionMark(), 0); 182 CProcessModule dlgProcessModule; 183 dlgProcessModule.m_dwProcessID = _tcstoul(strProcessID, 0, 10); 184 dlgProcessModule.DoModal(); 185 *pResult = 0; 186 }
下载地址如下: