?? tasklistdlg.cpp
字號:
// TaskListDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TaskList.h"
#include "TaskListDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTaskListDlg dialog
CTaskListDlg::CTaskListDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTaskListDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTaskListDlg)
// 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 CTaskListDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTaskListDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTaskListDlg, CDialog)
//{{AFX_MSG_MAP(CTaskListDlg)
ON_BN_CLICKED(IDC_BTNACTIVE, OnBtnactive)
ON_BN_CLICKED(IDC_BTNREFRESH, OnBtnrefresh)
ON_BN_CLICKED(IDC_BTNTERMINATE, OnBtnterminate)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTaskListDlg message handlers
BOOL CTaskListDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
//設(shè)置進(jìn)程列表框標(biāo)題
CListCtrl * pListCtrl = (CListCtrl*)GetDlgItem(IDC_LISTPROCESS);
CRect rt;
pListCtrl->GetClientRect(&rt);
pListCtrl->InsertColumn(0,_T("進(jìn)程名"), LVCFMT_LEFT, rt.Width() * 0.35);
pListCtrl->InsertColumn(1,_T("主窗體標(biāo)題"), LVCFMT_LEFT, rt.Width() * 0.35);
pListCtrl->InsertColumn(2, _T("線程數(shù)"), LVCFMT_LEFT, rt.Width() * 0.30);
return TRUE; // return TRUE unless you set the focus to a control
}
/*
*函數(shù)介紹:得到CE系統(tǒng)中運(yùn)行的任務(wù)列表
*入口參數(shù):(無)
*出口參數(shù):pList :存儲得到任務(wù)列表信息
*返回值:返回得到的任務(wù)數(shù)
*/
DWORD CTaskListDlg::GetTaskListCE( PTASK_LIST pList) {
HINSTANCE hKernel = NULL;
HINSTANCE hProcessSnap = NULL;
PROCESSENTRY32 pe32 = {0};
DWORD dwTaskCount = 0;
//狀態(tài)toolhelp.dll動態(tài)連接庫
hKernel = LoadLibrary(_T("toolhelp.dll"));
if (!hKernel) {
::MessageBox(NULL, L"Toolhelp.dll加載失敗", L"進(jìn)程列舉", MB_OK);
return 0;
}
//創(chuàng)建進(jìn)程映射
hProcessSnap = (HINSTANCE)CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
//如果失敗,就退出
if (hProcessSnap == (HANDLE)-1)
return 0;
dwTaskCount = 0;
//設(shè)置保存進(jìn)程相關(guān)內(nèi)容的變量的結(jié)構(gòu)大小
pe32.dwSize = sizeof(PROCESSENTRY32);
//獲取第一個進(jìn)程,并將此進(jìn)程信息寫入進(jìn)程結(jié)構(gòu)的變量pe32中
if (Process32First(hProcessSnap, &pe32)) {
do {
LPTSTR pCurChar;
if(_tcsstr(pe32.szExeFile, L"\\"))
pCurChar = _tcsrchr(pe32.szExeFile, '\\');
else
pCurChar = pe32.szExeFile;
lstrcpy(pList-> ProcessName, pCurChar);
pList ->dwProcessId = pe32.th32ProcessID;
pList ->cntThreads = pe32.cntThreads;
++dwTaskCount; //進(jìn)程數(shù)目加1
++pList; //移到下一個結(jié)構(gòu)內(nèi)存塊
}
while (Process32Next(hProcessSnap, &pe32)); //移至下一個進(jìn)程
}
else
dwTaskCount = 0; //
CloseHandle (hProcessSnap);
return dwTaskCount;
}
/*
*函數(shù)介紹:回調(diào)函數(shù),被EnumWindows調(diào)用
*入口參數(shù):hwnd :窗體句柄
* lParam : 進(jìn)程信息列表指針
*出口參數(shù):lParam :進(jìn)程信息列表指針
*返回值: True代表成功,F(xiàn)alse代表失敗
*/
BOOL CALLBACK CTaskListDlg::EnumWindowsProc( HWND hwnd, DWORD lParam ) {
DWORD pid = 0;
DWORD i;
TCHAR buf[TITLE_SIZE];
PTASK_LIST_ENUM te = (PTASK_LIST_ENUM)lParam;
PTASK_LIST tlist = te->tlist;
DWORD numTasks = te->numtasks;
// 根據(jù)窗口句柄,得到進(jìn)程標(biāo)識
if (!GetWindowThreadProcessId( hwnd, &pid )) {
return TRUE;
}
//查找進(jìn)程標(biāo)識和列表中相同的進(jìn)程標(biāo)識
for (i=0; i<numTasks; i++) {
if (tlist[i].dwProcessId == pid) {
if (::IsWindowVisible(hwnd)) {
tlist[i].hwnd = hwnd;
int nCnt = ::GetWindowText( hwnd, buf, TITLE_SIZE );
buf[nCnt] = '\0';
if (nCnt) {
lstrcpy( tlist[i].WindowTitle, buf );
}
}
break;
}
}
//繼續(xù)列舉窗體
return TRUE;
}
/*
*函數(shù)介紹:得到任務(wù)的主窗體標(biāo)題和句柄
*入口參數(shù):te :進(jìn)程信息列表指針
*出口參數(shù):te :進(jìn)程信息列表指針
*返回值: (無)
*/
void CTaskListDlg::GetWindowTitles( PTASK_LIST_ENUM te ) {
//列舉系統(tǒng)所有窗體
EnumWindows((WNDENUMPROC) EnumWindowsProc, (LPARAM) te );
}
/*
*函數(shù)介紹:激活進(jìn)程方法
*入口參數(shù):tlist : 進(jìn)程信息
*出口參數(shù):(無)
*返回值: True:成功;False:失敗
*/
bool CTaskListDlg::ActivateProcess(PTASK_LIST tlist)
{
//判斷是否存在主窗口,如存在就激活它
if (tlist->hwnd && tlist->WindowTitle)
{
::SetForegroundWindow(tlist->hwnd);
::SetActiveWindow(tlist->hwnd);
return TRUE;
}
else
{
return FALSE;
}
}
/*
*函數(shù)介紹:終止進(jìn)程方法
*入口參數(shù):tlist : 進(jìn)程信息
*出口參數(shù):(無)
*返回值: True:成功;False:失敗
*/
bool CTaskListDlg::KillProcess( PTASK_LIST tlist )
{
HANDLE hProcess;
//如果沒有主窗體,則用TerminateProcess終止進(jìn)程
if (!tlist->hwnd)
{
//根據(jù)進(jìn)程標(biāo)識,返回進(jìn)程句柄
hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, tlist->dwProcessId );
//成功的話,終止進(jìn)程
if (hProcess)
{
if (!TerminateProcess( hProcess, 1 ))
{
CloseHandle( hProcess );
return FALSE;
}
CloseHandle( hProcess );
return TRUE;
}
else
{
return FALSE;
}
}
//有主窗體,發(fā)送主窗口關(guān)閉消息,終止線程
else
{
::PostMessage( tlist->hwnd, WM_CLOSE, 0, 0 );
return TRUE;
}
}
/*
*函數(shù)介紹:刷新按鈕單擊事件
*入口參數(shù):(無)
*出口參數(shù):(無)
*返回值: (無)
*/
void CTaskListDlg::OnBtnrefresh()
{
TASK_LIST_ENUM processList; //進(jìn)程列表信息
TCHAR numBuf[10]; //存儲進(jìn)程中的線程數(shù)
memset(&g_tlist, 0, sizeof(TASK_LIST) * MAX_TASKS);
//得到進(jìn)程列表信息
DWORD nNumTasks = GetTaskListCE(g_tlist);
processList.numtasks = nNumTasks;
processList.tlist = g_tlist;
//更新進(jìn)程列表信息,獲取主窗口句柄和標(biāo)題
GetWindowTitles(&processList);
CListCtrl * pListCtrl = (CListCtrl*)GetDlgItem(IDC_LISTPROCESS);
//清除列表框所有項目
pListCtrl->DeleteAllItems();
memset(&numBuf,0,sizeof(numBuf));
//向列表框中添加進(jìn)程相關(guān)信息
for (int i=0;i<processList.numtasks;i++)
{
//進(jìn)程exe名,標(biāo)題名,線程數(shù)
pListCtrl->InsertItem(i,_T("Test"));
pListCtrl->SetItemText(i,0,processList.tlist[i].ProcessName);
pListCtrl->SetItemText(i,1,processList.tlist[i].WindowTitle);
_itow(processList.tlist[i].cntThreads,numBuf,10);
pListCtrl->SetItemText(i,2,numBuf);
}
}
/*
*函數(shù)介紹:激活進(jìn)程按鈕單擊事件
*入口參數(shù):(無)
*出口參數(shù):(無)
*返回值: (無)
*/
void CTaskListDlg::OnBtnactive()
{
CListCtrl * pListCtrl = (CListCtrl*)GetDlgItem(IDC_LISTPROCESS);
POSITION pos = pListCtrl->GetFirstSelectedItemPosition();
if (pos)
{
int nItemIndex = pListCtrl->GetNextSelectedItem(pos);
//激活進(jìn)程
ActivateProcess(&g_tlist[nItemIndex]);
}
}
/*
*函數(shù)介紹:終止進(jìn)程按鈕單擊事件
*入口參數(shù):(無)
*出口參數(shù):(無)
*返回值: (無)
*/
void CTaskListDlg::OnBtnterminate()
{
CListCtrl * pListCtrl = (CListCtrl*)GetDlgItem(IDC_LISTPROCESS);
POSITION pos = pListCtrl->GetFirstSelectedItemPosition();
if (pos)
{
int nItemIndex = pListCtrl->GetNextSelectedItem(pos);
//終止進(jìn)程
KillProcess(&g_tlist[nItemIndex]);
}
//刷新進(jìn)程列表
SendMessage(WM_COMMAND, (WPARAM)IDC_BTNREFRESH, 0);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -