主要界面如下:主要代碼如下:BOOL CEnumProcessDlg::OnInitDialog(){ CDialog::OnInitDialog(); // 將“關於...”菜單項添加到系統菜單中。 // IDM_ABOUTBOX 必須在系統命令範圍內。 ASSERT((I...
主要界面如下:
主要代碼如下:
BOOL CEnumProcessDlg::OnInitDialog() { CDialog::OnInitDialog(); // 將“關於...”菜單項添加到系統菜單中。 // IDM_ABOUTBOX 必須在系統命令範圍內。 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // 設置此對話框的圖標。當應用程式主視窗不是對話框時,框架將自動 // 執行此操作 SetIcon(m_hIcon, TRUE); // 設置大圖標 SetIcon(m_hIcon, FALSE); // 設置小圖標 // TODO: 在此添加額外的初始化代碼 LONG lStyle; lStyle = GetWindowLong(m_ctrlList.m_hWnd, GWL_STYLE); lStyle &= ~LVS_TYPEMASK; lStyle |= LVS_REPORT; SetWindowLong(m_ctrlList.m_hWnd, GWL_STYLE, lStyle); DWORD dwStyle = m_ctrlList.GetExtendedStyle(); dwStyle |= LVS_EX_FULLROWSELECT; dwStyle |= LVS_EX_GRIDLINES; m_ctrlList.SetExtendedStyle(dwStyle); m_ctrlList.InsertColumn(0, _T("Process ID"), LVCFMT_LEFT, 75); m_ctrlList.InsertColumn(1, _T("Process Name"), LVCFMT_LEFT, 120); m_ctrlList.InsertColumn(2, _T("Parent Process ID"), LVCFMT_LEFT, 115); m_ctrlList.InsertColumn(3, _T("Thread Count"), LVCFMT_LEFT, 90); ListProcess(); return TRUE; // 除非將焦點設置到控制項,否則返回 TRUE } void CEnumProcessDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // 如果向對話框添加最小化按鈕,則需要下麵的代碼 // 來繪製該圖標。對於使用文檔/視圖模型的 MFC 應用程式, // 這將由框架自動完成。 void CEnumProcessDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // 用於繪製的設備上下文 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0); // 使圖標在工作矩形中居中 int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // 繪製圖標 dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } //當用戶拖動最小化視窗時系統調用此函數取得游標顯示。 // HCURSOR CEnumProcessDlg::OnQueryDragIcon() { return static_cast<HCURSOR>(m_hIcon); } void CEnumProcessDlg::ListProcess(void) { HANDLE hProcessSnap; PROCESSENTRY32 pe32; hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hProcessSnap == INVALID_HANDLE_VALUE) { CString strErr; strErr.Format(_T("CreateToolhelp32Snapshot failed:%d"), GetLastError()); AfxMessageBox(strErr); return; } pe32.dwSize = sizeof(PROCESSENTRY32); int index = 0; if (!Process32First(hProcessSnap, &pe32)) { CString strErr; strErr.Format(_T("Process32First failed:%d"), GetLastError()); AfxMessageBox(strErr); CloseHandle(hProcessSnap); return; } do { CString strID; strID.Format(_T("%d"), pe32.th32ProcessID); m_ctrlList.InsertItem(index, strID); m_ctrlList.SetItemText(index, 1, pe32.szExeFile); strID.Format(_T("%d"), pe32.th32ParentProcessID); m_ctrlList.SetItemText(index, 2, strID); strID.Format(_T("%d"), pe32.cntThreads); m_ctrlList.SetItemText(index++, 3, strID); } while (Process32Next(hProcessSnap, &pe32)); CloseHandle(hProcessSnap); } void CEnumProcessDlg::OnContextMenu(CWnd* pWnd, CPoint point) { // TODO: 在此處添加消息處理程式代碼 CMenu menu; if (menu.CreatePopupMenu() == FALSE) { return; } if (pWnd == &m_ctrlList) { menu.AppendMenu(MF_STRING , ID_SCANMODULE , _T("查看依賴模塊")); if(m_ctrlList.GetSelectedCount() < 0) { menu.EnableMenuItem(ID_SCANMODULE , MF_DISABLED | MF_GRAYED); } } menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this); } void CEnumProcessDlg::ScanModule(UINT nID) { switch (nID) { case ID_SCANMODULE: { CString strProcessID = m_ctrlList.GetItemText(m_ctrlList.GetSelectionMark(), 0); CProcessModule dlgProcessModule; dlgProcessModule.m_dwProcessID = _tcstoul(strProcessID, 0, 10); dlgProcessModule.DoModal(); } break; default: break; } } void CEnumProcessDlg::OnNMDblclkListProcess(NMHDR *pNMHDR, LRESULT *pResult) { // TODO: 在此添加控制項通知處理程式代碼 CString strProcessID = m_ctrlList.GetItemText(m_ctrlList.GetSelectionMark(), 0); CProcessModule dlgProcessModule; dlgProcessModule.m_dwProcessID = _tcstoul(strProcessID, 0, 10); dlgProcessModule.DoModal(); *pResult = 0; }