📄 processdirview.cpp
字号:
/*
+------------------------------------------------------------+
| |
| (c) Copyright 2002, bigwhite, Inc. |
| |
| ALL RIGHTS RESERVED |
| |
+------------------------------------------------------------+
*/
/*==================================================================*
* processdirView.cpp : implementation of the CProcessdirView class | |
* Version 1.1 |
*=================================================================*/
//
#include "stdafx.h"
#include "processdir.h"
#include "processdirDoc.h"
#include "processdirView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CProcessdirView
IMPLEMENT_DYNCREATE(CProcessdirView, CListView)
BEGIN_MESSAGE_MAP(CProcessdirView, CListView)
//{{AFX_MSG_MAP(CProcessdirView)
ON_COMMAND(ID_PROCESS_KILL, OnProcessKill)
ON_COMMAND(ID_PROCESS_REFRESH, OnProcessRefresh)
ON_UPDATE_COMMAND_UI(ID_PROCESS_KILL, OnUpdateProcessKill)
ON_NOTIFY_REFLECT(LVN_ITEMCHANGED, OnItemchanged)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CListView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CListView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CListView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CProcessdirView construction/destruction
CProcessdirView::CProcessdirView()
{
// TODO: add construction code here
}
CProcessdirView::~CProcessdirView()
{
}
BOOL CProcessdirView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style&=~LVS_TYPEMASK;
//remove the default mode
cs.style|=LVS_REPORT;
//use 'LVS_REPORT' mode
return CListView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CProcessdirView drawing
void CProcessdirView::OnDraw(CDC* pDC)
{
CProcessdirDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
void CProcessdirView::OnInitialUpdate()
{
CListView::OnInitialUpdate();
m_ilSmall.Create(IDB_SMALLDOC,16,1,RGB(255,0,255));
GetListCtrl().SetImageList(&m_ilSmall,LVSIL_SMALL);
GetListCtrl().InsertColumn(0,"process",LVCFMT_CENTER,128);
GetListCtrl().InsertColumn(1,"index",LVCFMT_CENTER,48);
GetListCtrl ().InsertColumn (2, "pID", LVCFMT_CENTER, 72);
GetListCtrl ().InsertColumn (3, "Base priority", LVCFMT_CENTER, 128);
GetListCtrl ().InsertColumn(4,"NUM of Thread",LVCFMT_CENTER,98);
GetListCtrl ().InsertColumn(5,"full path",LVCFMT_LEFT,292);
ShowProcessInfo();
// its list control through a call to GetListCtrl().
}
/////////////////////////////////////////////////////////////////////////////
// CProcessdirView diagnostics
#ifdef _DEBUG
void CProcessdirView::AssertValid() const
{
CListView::AssertValid();
}
void CProcessdirView::Dump(CDumpContext& dc) const
{
CListView::Dump(dc);
}
CProcessdirDoc* CProcessdirView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CProcessdirDoc)));
return (CProcessdirDoc*)m_pDocument;
}
#endif //_DEBUG
void CProcessdirView::ShowProcessInfo()
{
OSVERSIONINFOEX osInfo;
BOOL bOsVersionInfoEx;
ZeroMemory(&osInfo, sizeof(OSVERSIONINFOEX));
osInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFOEX);
if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osInfo)) )
{
// If OSVERSIONINFOEX doesn't work, try OSVERSIONINFO.
osInfo.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
if (! GetVersionEx ( (OSVERSIONINFO *) &osInfo) )
{}//return FALSE;
}
CProcessInfo processinfo(TH32CS_SNAPALL);
PROCESSENTRY32 pe={sizeof(pe)};
BOOL fOk=processinfo.ProcessFirst(&pe);
for( int n=0;fOk;fOk=processinfo.ProcessNext(&pe),n++)
{
//LPCTSTR pszExeFile1=_tcsrchr(pe.szExeFile,'\\');
//another way to remove the absolute path to make sure that only the process'name be left
LPCTSTR pszExeFile=pe.szExeFile;
LPSTR pCurChar;
for(pCurChar=(pe.szExeFile+lstrlen(pe.szExeFile));(pCurChar!=pe.szExeFile)&&(*pCurChar!='\\');--pCurChar)
{;}
CString numofthread;
CString index;
CString processid;
CString priority;
index.Format("%d",n+1);//convert 'DWORD'type to 'CString' type
processid.Format("%8.8X",pe.th32ProcessID);
priority.Format("%d",pe.pcPriClassBase);
numofthread.Format("%d",pe.cntThreads);
if(osInfo.dwPlatformId==VER_PLATFORM_WIN32_NT)
//if( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
{
GetListCtrl ().InsertItem(n,pCurChar);
}
else
{
GetListCtrl ().InsertItem(n,pCurChar+1);
}
GetListCtrl ().SetItemText(n,5,pszExeFile);
GetListCtrl ().SetItemText(n,4,numofthread);
GetListCtrl ().SetItemText(n,3,priority);
GetListCtrl ().SetItemText(n,2,processid);
GetListCtrl ().SetItemText(n,1,index);
}
}
/////////////////////////////////////////////////////////////////////////////
// CProcessdirView message handlers
void CProcessdirView::OnProcessKill()
{ //int nSelected=-1;
int CurItem=GetListCtrl().GetNextItem(-1,LVIS_SELECTED);
CProcessInfo processinfo(TH32CS_SNAPALL);
PROCESSENTRY32 pe={sizeof(pe)};
BOOL fOk=processinfo.ProcessFirst(&pe);
for( int n=0;fOk;fOk=processinfo.ProcessNext(&pe),n++)
{ CString processid;
processid.Format("%8.8X",pe.th32ProcessID);
if(processid==GetListCtrl().GetItemText(CurItem,2))
{
if((AfxMessageBox("Do you really want to kill the process?",MB_OKCANCEL|MB_DEFBUTTON2))==IDOK)
{HANDLE hProcess= OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID );
if(::TerminateProcess(hProcess,1))
{
AfxMessageBox(" The process has been successfully killed",MB_OK);
OnProcessRefresh() ;
break;
}
}
}
}
}
/////////////////////////////////////////////////
void CProcessdirView::OnProcessRefresh()
{
if(GetListCtrl().DeleteAllItems())
ShowProcessInfo();
CString refreshinfo("refresh");
GetDocument()->UpdateAllViews(this,0x6A,(CObject*)(LPCTSTR)refreshinfo);
//'0x6A' is user-defined message identify code
}
////////////////////////////////////////////
void CProcessdirView::OnUpdateProcessKill(CCmdUI* pCmdUI)
{
int nSelected=-1;
int k=GetListCtrl().GetNextItem(nSelected,LVIS_SELECTED);
pCmdUI->Enable(k+1);
}
void CProcessdirView::OnItemchanged(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
CString processid=GetListCtrl().GetItemText(pNMListView->iItem,2);
GetDocument()->UpdateAllViews(this,0x5A,(CObject*)(LPCTSTR)processid);//0x6A是自定义消息识别码,而refreshinfo使传递给另一个视图OnUpdate
//消息处理函数的参数值
*pResult = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -