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

📄 internetdlg.cpp

📁 这是书上的代码
💻 CPP
字号:
//--------------------------------------------------
//系统进程监控程序Internet,闫小勇,2002.12
//石家庄铁道学院交通工程系,kaiser@china.com.cn
//--------------------------------------------------

#include "stdafx.h"
#include "Internet.h"
#include "InternetDlg.h"
#include "Process.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define ID_TIMER 1
/////////////////////////////////////////////////////////////////////////////
// CInternetDlg dialog

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

void CInternetDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CInternetDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CInternetDlg, CDialog)
	//{{AFX_MSG_MAP(CInternetDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CInternetDlg message handlers

BOOL CInternetDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
    
	//初始化ToolHelp32
	if(!InitToolhelp32()) 
    { 
		MessageBox("初始化ToolHelp32失败!","警告",MB_OK|MB_ICONSTOP); 
		EndDialog(IDCANCEL); 
		return FALSE; 
    } 
	
	//注册为系统服务,使程序不出现在任务列表中
	RegisterServiceProcess(GetCurrentProcessId(),1);
	
	//隐藏对话框
	WINDOWPLACEMENT wp;
	wp.length=sizeof(WINDOWPLACEMENT);
	wp.flags=WPF_RESTORETOMAXIMIZED;
	wp.showCmd=SW_HIDE;
	SetWindowPlacement(&wp);
	ModifyStyleEx(WS_EX_APPWINDOW, WS_EX_TOOLWINDOW);
    
	//设置定时器,间隔时间为1秒
	SetTimer(ID_TIMER,1000,NULL);
			
	return TRUE;  
}

// 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 CInternetDlg::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 CInternetDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CInternetDlg::OnTimer(UINT nIDEvent) 
{
	//建立进程信息快照
	HANDLE hSnapshot=pCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); 
    if(!hSnapshot) return; 
	
	PROCESSENTRY32 pe; 
    pe.dwSize=sizeof(pe); 
	
	//遍历进程信息快照
    for(int i=pProcess32First(hSnapshot,&pe);i;
		i=pProcess32Next(hSnapshot,&pe)) 
    { 
		//如果含有无关程序的进程则终止其运行
		if(strstr(pe.szExeFile,"QQ.EXE"))
		//可以在这里加入一个无关程序的列表扩充功能
		{
			HANDLE hand=OpenProcess(PROCESS_TERMINATE,TRUE,pe.th32ProcessID);
			TerminateProcess(hand,0);
			break;
		}
	} 
    
	//关闭快照句柄
    CloseHandle(hSnapshot); 

	CDialog::OnTimer(nIDEvent);
}

void CInternetDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	//销毁定时器
	KillTimer(ID_TIMER);
}

⌨️ 快捷键说明

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