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

📄 mfctlistdlg.cpp

📁 一个进程管理的源程序.zip
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MFCTListDlg.cpp : implementation file
//


#include "stdafx.h"
#include "MFCTList.h"
#include "MFCTListDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CMFCTListDlg dialog

CMFCTListDlg::CMFCTListDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMFCTListDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMFCTListDlg)
		// 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); 

   if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT|WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC, 
		WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP)
		||		!m_wndToolBar.LoadToolBar(IDR_MYTOOL)
	)
	{
	   TRACE0("Failed to create toolbar\n");
		return;      // fail to create
	}
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);

}

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

BEGIN_MESSAGE_MAP(CMFCTListDlg, CDialog)
	//{{AFX_MSG_MAP(CMFCTListDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_KILL, OnKill)
	ON_BN_CLICKED(IDC_REFRESH, OnRefresh)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_TASKLIST, OnItemchangedTasklist)
	ON_BN_CLICKED(IDC_PRIORITY, OnPriority)
	ON_NOTIFY(LVN_COLUMNCLICK, IDC_TASKLIST, OnColumnclickTasklist)
	ON_NOTIFY(HDN_ITEMCHANGING, IDC_ThreadList, OnItemchangingThreadList)
	ON_NOTIFY(LVN_COLUMNCLICK, IDC_ThreadList, OnColumnclickThreadList)
	ON_COMMAND(IDD_ABOUTBOX, OnAboutbox)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMFCTListDlg message handlers

BOOL CMFCTListDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	flag=1;

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

	// add columns to list control
     SetWindowText(_T("My program!"));
	CListCtrl* pList = (CListCtrl*) GetDlgItem(IDC_TASKLIST);
	ASSERT(pList != NULL);

	pList->InsertColumn(0, _T("任务名"));
	pList->InsertColumn(1, _T("进程号"));
	pList->InsertColumn(2, _T("父进程号"));
    pList->InsertColumn(3, _T("优先数"));
	pList->InsertColumn(4, _T("线程数"));
    pList->InsertColumn(5, _T("窗口名"));
	pList->InsertColumn(6, _T("执行文件名"));

	// load the control
    CListCtrl *pList1 = (CListCtrl*) GetDlgItem(IDC_ThreadList);
	ASSERT(pList1 != NULL);

	pList1->InsertColumn(0, _T("任务名"),LVCFMT_CENTER,60);
	pList1->InsertColumn(1, _T("进程号"),LVCFMT_CENTER,60);
    pList1->InsertColumn(2, _T("优先数"),LVCFMT_CENTER,60);
    pList1->InsertColumn(3, _T("执行时间"),LVCFMT_CENTER,80);
	pList1->InsertColumn(4, _T("执行文件名"),LVCFMT_CENTER,160);

	DoRefresh();
	
	return TRUE;
	// return TRUE  unless you set the focus to a control
}

void CMFCTListDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

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

void CMFCTListDlg::OnKill() 
{

	// find the selected task and kill it
	CListCtrl* pList = (CListCtrl*) GetDlgItem(IDC_TASKLIST);
	ASSERT(pList != NULL);

	int nItem = pList->GetNextItem(-1, LVIS_SELECTED);

	// no sorting, so we know nItem is still an index into
	// our array

	CMFCTListApp* pApp = (CMFCTListApp*) AfxGetApp();
	CTaskList& refTaskList = pApp->GetTaskList();

	BOOL bResult = KillProcess(refTaskList[nItem], TRUE);
	if (bResult)
	{
		MessageBox("Kill worked!");
	    DoRefresh();
        DoTRefresh();
	}
	else
		MessageBox("Kill failed!");
}

void CMFCTListDlg::OnRefresh() 
{
	DoRefresh();
    DoTRefresh();
}


void CMFCTListDlg::DoRefresh()
{
	
	
    ClearThread();
	CListCtrl* pList = (CListCtrl*) GetDlgItem(IDC_TASKLIST);
	ASSERT(pList != NULL);

	// reset what's there

	pList->DeleteAllItems();

    //
    // Determine what system we're on and do the right thing
    //

    LPGetTaskList     GetTaskList;
    LPEnableDebugPriv EnableDebugPriv;

    OSVERSIONINFO verInfo;
	memset(&verInfo, 0, sizeof(verInfo));

    verInfo.dwOSVersionInfoSize = sizeof(verInfo);
    GetVersionEx(&verInfo);

    switch (verInfo.dwPlatformId)
    {
    case VER_PLATFORM_WIN32_NT:
		GetTaskList     = GetTaskListNT;
		EnableDebugPriv = EnableDebugPrivNT;
		break;

    case VER_PLATFORM_WIN32_WINDOWS:
		GetTaskList = GetTaskList95;
		EnableDebugPriv = EnableDebugPriv95;
		break;

    default:
//		m_bBroken = TRUE;
		pList->InsertItem(0, _T("Requires Win95 or WinNT"));
		return;
    }

    EnableDebugPriv();

    // get the task list for the system

	CMFCTListApp* pApp = (CMFCTListApp*) AfxGetApp();
	pApp->ClearTaskList();
	CTaskList& refTaskList = pApp->GetTaskList();

    DWORD numTasks = GetTaskList(refTaskList);

    // enumerate all windows and try to get the window
    // titles for each task

    GetWindowTitles(refTaskList);

    //
    // print the task list
    //

	int n;
	for (n = 0; n < refTaskList.GetSize(); n++)
	{   
	//	HANDLE  hProcess;
		CString str,str1;
		str.Format("0x%8.8X", refTaskList[n]->dwProcessId);
      //  hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, refTaskList[n]->dwProcessId );

		str.Format("0x%8.8X", refTaskList[n]->dwProcessId);
		int	nItem;
		nItem=pList->InsertItem(n, refTaskList[n]->ProcessName);
		pList->SetItemText(nItem, 1, str);
		CString pid;
		pid.Format("0x%8.8X",refTaskList[n]->th32ParentProcessID);
        pList->SetItemText(nItem, 2,pid );
		//DWORD pi=GetPriorityClass(hProcess);
  
		//pApp->m_tasklist[n]->SetPriority(pi);

⌨️ 快捷键说明

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