⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 processdlg.cpp

📁 发生的发的是发生的非收费收费是个符合规范和规范和非大哥大哥大哥
💻 CPP
字号:
// ProcessDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Process.h"
#include "ProcessDlg.h"
#include "tlhelp32.h"  //关键函数包含在这里面
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CProcessDlg dialog

CProcessDlg::CProcessDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CProcessDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CProcessDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_nWinVersion = WIN_UNKNOWN;
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CProcessDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CProcessDlg)
	DDX_Control(pDX, IDC_STATIC_WINVERSION, m_staWinVersion);
	DDX_Control(pDX, IDC_LIST, m_wndList);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CProcessDlg, CDialog)
	//{{AFX_MSG_MAP(CProcessDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_REFRESH, OnRefresh)
	ON_BN_CLICKED(IDC_STOPPROCESS, OnStopprocess)
	ON_BN_CLICKED(IDC_SHUTDOWN, OnShutdown)
	ON_LBN_SELCHANGE(IDC_LIST, OnSelchangeList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CProcessDlg message handlers

BOOL CProcessDlg::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);

	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);
		}
	}
	// 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
    // 得到当前Windows版本
    OSVERSIONINFOEX WinVersion;
	ZeroMemory(&WinVersion, sizeof(OSVERSIONINFOEX));
	WinVersion.dwOSVersionInfoSize =sizeof(OSVERSIONINFOEX);
	DWORD dwMajorVersion,dwMinorVersion,dwPlatformId;
	BOOL flag=GetVersionEx((OSVERSIONINFO *) &WinVersion);
	if(flag)
	{
	   dwMajorVersion=WinVersion.dwMajorVersion;
       dwMinorVersion=WinVersion.dwMinorVersion;
	   dwPlatformId=WinVersion.dwPlatformId;
	   if(dwMajorVersion==3)
	   {
		  m_nWinVersion=WIN_NT351;
	      m_staWinVersion.SetWindowText(_T("Windows NT 3.51"));
	   }
	   else if(dwMajorVersion==4 && dwMinorVersion==0 && dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)
	   {
		   m_nWinVersion=WIN_95;
		   m_staWinVersion.SetWindowText(_T("Windows 95"));
	   }
	   else if(dwMajorVersion==4 && dwMinorVersion==0 && dwPlatformId==VER_PLATFORM_WIN32_NT)
	   {
		  m_nWinVersion=WIN_NT40;
		  m_staWinVersion.SetWindowText(_T("Windows NT 4.0"));
	   }
	   else if(dwMajorVersion==4 && dwMinorVersion==10)
	   {
		  m_nWinVersion=WIN_98;
		  m_staWinVersion.SetWindowText(_T("Windows 98"));
	   }
	   else if(dwMajorVersion==4 && dwMinorVersion==90)
	   {
		  m_nWinVersion=WIN_ME;
		  m_staWinVersion.SetWindowText(_T("Windows Me"));
	   }
	   else if(dwMajorVersion==5 && dwMinorVersion==0)
	   {
		  m_nWinVersion=WIN_2000;
		  m_staWinVersion.SetWindowText(_T("Windows 2000"));
	   }
	   else if(dwMajorVersion==5 && dwMinorVersion==1)
	   {
		  m_nWinVersion=WIN_XP;
		  m_staWinVersion.SetWindowText(_T("Windows XP"));
	   }
	   else
	   {
		  m_nWinVersion=WIN_UNKNOWN;
		  m_staWinVersion.SetWindowText(_T("未知系统"));
	   }	      
	}
    //  如果不是以上Windows版本,则退出程序,否则刷新进程列表
	if(m_nWinVersion==WIN_UNKNOWN)
	{
	   AfxMessageBox(_T("未知操作系统!"),MB_OK|MB_ICONINFORMATION);
       ::PostMessage(this->m_hWnd,WM_QUIT,0,0);
	}
	else
	   OnRefresh();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CProcessDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.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 CProcessDlg::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 CProcessDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

// “刷新”按钮响应函数
void CProcessDlg::OnRefresh() 
{	
	// TODO: Add your control notification handler code here
	m_wndList.ResetContent();
	HANDLE hSnapshot;
	// 创建系统快照
	hSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
	PROCESSENTRY32 pe;
	Process32First(hSnapshot,&pe);
	do
	{
		int index=m_wndList.AddString(pe.szExeFile);
		m_wndList.SetItemData(index,pe.th32ProcessID);
	}
	while(Process32Next(hSnapshot,&pe));
	CloseHandle(hSnapshot);
    GetDlgItem(IDC_STOPPROCESS)->EnableWindow(FALSE);
}

// “结束任务”按钮响应函数
void CProcessDlg::OnStopprocess() 
{
	// TODO: Add your control notification handler code here
	int index = m_wndList.GetCurSel();
	DWORD data=m_wndList.GetItemData(index);
	HANDLE hProcess;
	// 打开进程
	hProcess=OpenProcess(PROCESS_TERMINATE,FALSE,data);
	if(hProcess)
	{
	   if(!TerminateProcess(hProcess,0))
	   {
		  CString strError;
	      strError.Format("错误号:%d",GetLastError());
	      AfxMessageBox(strError,MB_OK|MB_ICONINFORMATION,NULL);
	   }
	}
	else
	{
	    CString strError;
	    strError.Format("错误号:%d",GetLastError());
	    if(GetLastError()==ERROR_ACCESS_DENIED)
		   strError=_T("拒绝访问!")+strError;
	    AfxMessageBox(strError,MB_OK|MB_ICONINFORMATION,NULL);
	}
	Sleep(300);
	OnRefresh();	
}

// “关机”按钮响应函数
void CProcessDlg::OnShutdown() 
{
	// TODO: Add your control notification handler code here
	if(AfxMessageBox("确定关机么? ",MB_YESNO|MB_ICONQUESTION)==IDYES)
	   if(!ExitWindowsEx(EWX_SHUTDOWN,0))
	   {
	      CString strError;
		  strError.Format("错误码:%d。",GetLastError());
		  if(m_nWinVersion==WIN_95||m_nWinVersion==WIN_98||m_nWinVersion==WIN_ME)
             strError=_T("关机失败!")+strError;
		  else
			 strError=_T("您没有关机权限!")+strError;		 
		  AfxMessageBox(strError,MB_OK|MB_ICONINFORMATION);
	   }	
}

// List控件框中当选项改变时消息处理函数
void CProcessDlg::OnSelchangeList() 
{
    if(!GetDlgItem(IDC_STOPPROCESS)->IsWindowEnabled())	
		GetDlgItem(IDC_STOPPROCESS)->EnableWindow();
}

void CProcessDlg::OnOK() 
{
	CDialog::OnOK();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -