ListProcessesDlg.cpp
上传用户:xsxdsb
上传日期:2009-12-14
资源大小:672k
文件大小:5k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // ListProcessesDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ListProcesses.h"
  5. #include "ListProcessesDlg.h"
  6. #include <tlhelp32.h>
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CAboutDlg dialog used for App About
  14. class CAboutDlg : public CDialog
  15. {
  16. public:
  17. CAboutDlg();
  18. // Dialog Data
  19. //{{AFX_DATA(CAboutDlg)
  20. enum { IDD = IDD_ABOUTBOX };
  21. //}}AFX_DATA
  22. // ClassWizard generated virtual function overrides
  23. //{{AFX_VIRTUAL(CAboutDlg)
  24. protected:
  25. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  26. //}}AFX_VIRTUAL
  27. // Implementation
  28. protected:
  29. //{{AFX_MSG(CAboutDlg)
  30. //}}AFX_MSG
  31. DECLARE_MESSAGE_MAP()
  32. };
  33. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  34. {
  35. //{{AFX_DATA_INIT(CAboutDlg)
  36. //}}AFX_DATA_INIT
  37. }
  38. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CDialog::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CAboutDlg)
  42. //}}AFX_DATA_MAP
  43. }
  44. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  45. //{{AFX_MSG_MAP(CAboutDlg)
  46. // No message handlers
  47. //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CListProcessesDlg dialog
  51. CListProcessesDlg::CListProcessesDlg(CWnd* pParent /*=NULL*/)
  52. : CDialog(CListProcessesDlg::IDD, pParent)
  53. {
  54. //{{AFX_DATA_INIT(CListProcessesDlg)
  55. // NOTE: the ClassWizard will add member initialization here
  56. //}}AFX_DATA_INIT
  57. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  58. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  59. }
  60. void CListProcessesDlg::DoDataExchange(CDataExchange* pDX)
  61. {
  62. CDialog::DoDataExchange(pDX);
  63. //{{AFX_DATA_MAP(CListProcessesDlg)
  64. DDX_Control(pDX, IDC_LIST_PROCESSES, m_ctrlLstPorcesses);
  65. //}}AFX_DATA_MAP
  66. }
  67. BEGIN_MESSAGE_MAP(CListProcessesDlg, CDialog)
  68. //{{AFX_MSG_MAP(CListProcessesDlg)
  69. ON_WM_SYSCOMMAND()
  70. ON_WM_PAINT()
  71. ON_WM_QUERYDRAGICON()
  72. //}}AFX_MSG_MAP
  73. END_MESSAGE_MAP()
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CListProcessesDlg message handlers
  76. BOOL CListProcessesDlg::OnInitDialog()
  77. {
  78. CDialog::OnInitDialog();
  79. // Add "About..." menu item to system menu.
  80. // IDM_ABOUTBOX must be in the system command range.
  81. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  82. ASSERT(IDM_ABOUTBOX < 0xF000);
  83. CMenu* pSysMenu = GetSystemMenu(FALSE);
  84. if (pSysMenu != NULL)
  85. {
  86. CString strAboutMenu;
  87. strAboutMenu.LoadString(IDS_ABOUTBOX);
  88. if (!strAboutMenu.IsEmpty())
  89. {
  90. pSysMenu->AppendMenu(MF_SEPARATOR);
  91. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  92. }
  93. }
  94. // Set the icon for this dialog.  The framework does this automatically
  95. //  when the application's main window is not a dialog
  96. SetIcon(m_hIcon, TRUE); // Set big icon
  97. SetIcon(m_hIcon, FALSE); // Set small icon
  98. // TODO: Add extra initialization here
  99. //列出进程
  100. ListProcesses();
  101. return TRUE;  // return TRUE  unless you set the focus to a control
  102. }
  103. void CListProcessesDlg::OnSysCommand(UINT nID, LPARAM lParam)
  104. {
  105. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  106. {
  107. CAboutDlg dlgAbout;
  108. dlgAbout.DoModal();
  109. }
  110. else
  111. {
  112. CDialog::OnSysCommand(nID, lParam);
  113. }
  114. }
  115. // If you add a minimize button to your dialog, you will need the code below
  116. //  to draw the icon.  For MFC applications using the document/view model,
  117. //  this is automatically done for you by the framework.
  118. void CListProcessesDlg::OnPaint() 
  119. {
  120. if (IsIconic())
  121. {
  122. CPaintDC dc(this); // device context for painting
  123. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  124. // Center icon in client rectangle
  125. int cxIcon = GetSystemMetrics(SM_CXICON);
  126. int cyIcon = GetSystemMetrics(SM_CYICON);
  127. CRect rect;
  128. GetClientRect(&rect);
  129. int x = (rect.Width() - cxIcon + 1) / 2;
  130. int y = (rect.Height() - cyIcon + 1) / 2;
  131. // Draw the icon
  132. dc.DrawIcon(x, y, m_hIcon);
  133. }
  134. else
  135. {
  136. CDialog::OnPaint();
  137. }
  138. }
  139. // The system calls this to obtain the cursor to display while the user drags
  140. //  the minimized window.
  141. HCURSOR CListProcessesDlg::OnQueryDragIcon()
  142. {
  143. return (HCURSOR) m_hIcon;
  144. }
  145. void CListProcessesDlg::ListProcesses()
  146. {
  147. //初始化
  148. HANDLE hProcessSnap = NULL;
  149. PROCESSENTRY32 pe32 = {0};
  150. //获得句柄
  151. hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  152. if(hProcessSnap == (HANDLE)-1)
  153. {
  154. printf("nCreateToolhelp32Snapshot()failed:%d",GetLastError());
  155. return;
  156. }
  157. pe32.dwSize = sizeof(PROCESSENTRY32);
  158. //列举所有进程名称
  159. if (Process32First(hProcessSnap, &pe32))
  160. {
  161. do
  162. {
  163. //将进程名加到列表中
  164. m_ctrlLstPorcesses.AddString(pe32.szExeFile);
  165. }
  166. while (Process32Next(hProcessSnap, &pe32));//直到列举完毕
  167. }
  168. else
  169. {
  170. printf("nProcess32Firstt() failed:%d",GetLastError());
  171. }
  172. //关闭句柄
  173. CloseHandle (hProcessSnap);   
  174. }