📄 selectprogdlg.cpp
字号:
// SelectProgDlg.cpp : implementation file
//
#include <afxwin.h>
#include <afxdlgs.h> //for CFileDialog declare...
#include "resource.h"
#include "SelectProgDlg.h"
#ifdef _WIN98
#include "tlhelp32.h"
#else
#include "psapi.h"
#endif
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSelectProgDlg dialog
CSelectProgDlg::CSelectProgDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSelectProgDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSelectProgDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
iItemType = TYPE_UNKNOWN;
iSelectID = 0;
strProgName.Empty();
}
void CSelectProgDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSelectProgDlg)
DDX_Control(pDX, IDC_LST_PROG_LIST, m_lstProgList);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSelectProgDlg, CDialog)
//{{AFX_MSG_MAP(CSelectProgDlg)
ON_BN_CLICKED(ID_ADD, OnAdd)
ON_LBN_DBLCLK(IDC_LST_PROG_LIST, OnDblclkLstProgList)
ON_LBN_SELCHANGE(IDC_LST_PROG_LIST, OnSelchangeLstProgList)
ON_BN_CLICKED(IDC_REFRESH_PROGID, OnRefreshProgid)
ON_LBN_SETFOCUS(IDC_LST_PROG_LIST, OnSetfocusLstProgList)
ON_EN_SETFOCUS(IDC_EDT_PROGNAME, OnSetfocusEdtProgname)
ON_EN_CHANGE(IDC_EDT_PROGNAME, OnChangeEdtProgname)
ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSelectProgDlg message handlers
void CSelectProgDlg::OnAdd()
{
// TODO: Add your control notification handler code here
BOOL bOK;
switch( iItemType){
case TYPE_PROGRAM_FILENAME:
bOK = (strProgName.GetLength()>0)?TRUE:FALSE;
break;
case TYPE_PROGRAM_ID:
bOK = (iSelectID != 0)?TRUE:FALSE;
break;
default:
bOK = FALSE;
}
if( !bOK)
MessageBox("没有选择正确的程序名称或者id号");
else
OnOK();
}
void CSelectProgDlg::OnDblclkLstProgList()
{
// TODO: Add your control notification handler code here
OnSelchangeLstProgList();
OnAdd();
}
void CSelectProgDlg::OnSelchangeLstProgList()
{
// TODO: Add your control notification handler code here
int iSel, iPos;
CString strName;
iSel = m_lstProgList.GetCurSel();
if( iSel != LB_ERR){
iSelectID = m_lstProgList.GetItemData( iSel);
m_lstProgList.GetText( iSel, strName);
iPos = strName.ReverseFind('\\');
strIDName = strName.Right( strName.GetLength() - iPos -1);
#ifdef _WIN98
strProgName = strIDName;
iItemType = TYPE_PROGRAM_FILENAME;
#else
iItemType = TYPE_PROGRAM_ID;
#endif
}
else{
iSelectID = 0;
strIDName.Empty();
iItemType = TYPE_PROGRAM_ID;
}
ShowSelectResult();
}
BOOL CSelectProgDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
OnRefreshProgid();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CSelectProgDlg::OnRefreshProgid()
{
// TODO: Add your control notification handler code here
m_lstProgList.ResetContent();
#ifdef _WIN98
PROCESSENTRY32 entry32;
HANDLE snapHandle;
int i;
snapHandle = CreateToolhelp32Snapshot( TH32CS_SNAPALL ,0);
if( snapHandle != INVALID_HANDLE_VALUE){
entry32.dwSize = sizeof( PROCESSENTRY32);
Process32First( snapHandle, &entry32);
i = 0;
while( 1){
m_lstProgList.AddString( entry32.szExeFile);
if( !Process32Next( snapHandle, &entry32)) break;
}
CloseHandle( snapHandle);
}
#else
DWORD dwProcessArray[1024], dwSizeNeed, dwProcessNum, i, j, dwModNum;
HANDLE hProcess;
HMODULE hMods[1024];
char szModName[1024], szTemp[1024+20];
int iItem;
if( !EnumProcesses( dwProcessArray, sizeof( dwProcessArray), &dwSizeNeed)){
MessageBox("不能枚举 process key.");
return;
}
dwProcessNum = dwSizeNeed/sizeof(DWORD);
for( i=0; i<dwProcessNum; i++){
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION|PROCESS_VM_READ, FALSE, dwProcessArray[i]);
if( EnumProcessModules( hProcess, hMods, sizeof( hMods), &dwSizeNeed)){
dwModNum = dwSizeNeed/sizeof(HMODULE);
for( j=0; j<dwModNum; j++){
if( GetModuleFileNameEx( hProcess, hMods[j], szModName, sizeof(szModName))){
wsprintf( szTemp,"%d - %s", dwProcessArray[i], szModName);
iItem = m_lstProgList.AddString( szTemp);
m_lstProgList.SetItemData( iItem, dwProcessArray[i]);
break; //skip other sub thread.
}
}
}
CloseHandle( hProcess);
}
#endif
}
void CSelectProgDlg::ShowSelectResult()
{
char szTemp[512];
switch( iItemType){
case TYPE_PROGRAM_FILENAME:
wsprintf( szTemp,"程序名称: %s", strProgName);
break;
case TYPE_PROGRAM_ID:
wsprintf( szTemp,"进程ID: %d (%s)", iSelectID, (LPCTSTR)strIDName);
break;
default:
strcpy( szTemp,"没有选择");
break;
}
SetDlgItemText(IDC_SELECT_RESULT, szTemp);
}
void CSelectProgDlg::OnSetfocusLstProgList()
{
// TODO: Add your control notification handler code here
if( m_lstProgList.GetCurSel() != LB_ERR)
OnSelchangeLstProgList();
}
//when get focus, update the text.
void CSelectProgDlg::OnSetfocusEdtProgname()
{
// TODO: Add your control notification handler code here
OnChangeEdtProgname();
}
void CSelectProgDlg::OnChangeEdtProgname()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
GetDlgItemText( IDC_EDT_PROGNAME, strProgName);
iItemType = TYPE_PROGRAM_FILENAME;
ShowSelectResult();
}
void CSelectProgDlg::OnBrowse()
{
// TODO: Add your control notification handler code here
CFileDialog fileDlg( TRUE,".exe", NULL,0,"exe file (*.exe)|*.exe||", this);
if( fileDlg.DoModal() == IDOK){
SetDlgItemText( IDC_EDT_PROGNAME, fileDlg.GetFileName());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -