// listprocessDlg.cpp : implementation file
//
#include "stdafx.h"
#include "listprocess.h"
#include "listprocessDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <tlhelp32.h>
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CListprocessDlg dialog
CListprocessDlg::CListprocessDlg(CWnd* pParent /*=NULL*/)//构造函数
: CDialog(CListprocessDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CListprocessDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);//主图标,任务栏中
m_hCursor = AfxGetApp()->LoadCursor(IDC_CURSOR1);//鼠标图标========================有用
}
void CListprocessDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CListprocessDlg)
DDX_Control(pDX, IDC_LIST2, m_list);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CListprocessDlg, CDialog)
//{{AFX_MSG_MAP(CListprocessDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnKill)//终止进程
ON_BN_CLICKED(IDC_BUTTON2, OnButtonForceKill)//强制终止进程
ON_WM_SETCURSOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CListprocessDlg message handlers
BOOL CListprocessDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
/*上面两行代码的作用:检测IDM_ABOUTBOX这个常量的值是否系统命令范围之内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); //要是16的倍数
ASSERT(IDM_ABOUTBOX < 0xF000); //要小于0xF000
这两个条件符合就在系统命令范围之内了。*/
CMenu* pSysMenu = GetSystemMenu(FALSE);//FALSE得到控制菜单的一个拷贝,TRUE,则返回值不确定
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
//Reads a Windows string resource, identified by nID, into an existing CStringT object.
if (!strAboutMenu.IsEmpty())//得到的字符串非空
{
/*BOOL AppendMenu( UINT nFlags, UINT nIDNewItem = 0, LPCTSTR lpszNewItem = NULL )
参数1:nFlags为MF_SEPARATOR,则 nIDNewItem被忽略,若为MF_STRING,则得到字符串的指针;
参数2:得到一个命令ID或一个menu句柄;
参数3:Specifies the content of the new menu item.
*/
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
OnOK();
return TRUE; // return TRUE unless you set the focus to a control
}
void CListprocessDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
/*检查范围,“nID & 0xFFF0”,这主要也是解决 Windows 95 的 bug*/
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();//DoModal 实际上实现了对话框的创建、显示、关闭的全过程,实际上是一个函数中完成的
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CListprocessDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
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;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CListprocessDlg::OnQueryDragIcon()//===============有用
{
return (HCURSOR) m_hIcon;
}
BOOL CListprocessDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) //===============有用
{
// TODO: Add your message handler code here and/or call default
SetCursor(m_hCursor);
return TRUE;
}
void CListprocessDlg::OnOK()
{
m_list.DeleteAllItems();
while(m_list.DeleteColumn(0));
m_list.ModifyStyle(0,LVS_REPORT);
m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
m_list.InsertColumn(0,"进程ID",LVCFMT_LEFT,80);
m_list.InsertColumn(1,"进程名称",LVCFMT_CENTER,156);
HANDLE handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
PROCESSENTRY32* info = new PROCESSENTRY32;
info->dwSize = sizeof(PROCESSENTRY32);
int i=0;
if(Process32First(handle,info))
{
if(GetLastError()==ERROR_NO_MORE_FILES )
{
AfxMessageBox("No More Process");
}
else
{
CString id;
do
{
id.Format("%6d",info->th32ProcessID);
m_list.InsertItem(i,id);
m_list.SetItemData(i,info->th32ProcessID);
id.Format("%s",info->szExeFile);
m_list.SetItemText(i,1,id);
i++;
}while(Process32Next(handle,info)!=FALSE);
}
}
CloseHandle(handle);
}
void CListprocessDlg::OnCancel()
{
CDialog::OnCancel();
}
void CListprocessDlg::OnKill()
{
int select;
POSITION pos;
pos = m_list.GetFirstSelectedItemPosition();
if(pos == NULL)
{
MessageBox("请选择要终止的进程!","提示信息",MB_ICONINFORMATION);
return ;
}
else
{
if(MessageBox("真的要终止该进程吗?","终止进程询问",MB_OKCANCEL|MB_ICONQUESTION|256)==IDOK)
{
select = m_list.GetNextSelectedItem(pos);
HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS,TRUE,m_list.GetItemData(select));
if(handle!=NULL)
{
TerminateProcess(handle,0);
CloseHandle(handle);
}
else
{
MessageBox("系统重要进程,不能终止!!!","警告!!!",MB_ICONSTOP);
}
OnOK();
}
}
}
void CListprocessDlg::OnButtonForceKill()
{
// TODO: Add your control notification handler code here
int select;
int pid;
CString strPid;
CString killcode;
POSITION pos;
pos = m_list.GetFirstSelectedItemPosition();
if(pos == NULL)
{
MessageBox("请选择要强制终止的进程!","提示信息",MB_ICONINFORMATION);
return ;
}
else
{
if(MessageBox("真的要强制终止该进程吗?","终止进程询问",MB_OKCANCEL|MB_ICONQUESTION|256)==IDOK)
{
select = m_list.GetNextSelectedItem(pos);
pid = m_list.GetItemData(select);
strPid.Format("%d",pid);
killcode = "/c ntsd -c q -p " + strPid;
HINSTANCE n=ShellExecute(NULL, "open", "cmd", killcode, NULL, SW_HIDE);
OnOK();
}
}
}