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

📄 processwatcheddialog.cpp

📁 关于进程&线程查看器 用过vc++自带的Process Viewer
💻 CPP
字号:
// ProcessWatchedDialog.cpp : implementation file
//

#include "stdafx.h"
#include "processdir.h"
#include "ProcessWatchedDialog.h"
#include "ProcessInfo.h"

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

/////////////////////////////////////////////////////////////////////////////
// CProcessWatchedDialog dialog


CProcessWatchedDialog::CProcessWatchedDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CProcessWatchedDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CProcessWatchedDialog)
	m_strProcessWatched = _T("");
	//}}AFX_DATA_INIT
}


void CProcessWatchedDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CProcessWatchedDialog)

	//DDX_Text(pDX, IDC_EDIT1, m_strProcessWatched);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CProcessWatchedDialog, CDialog)
	//{{AFX_MSG_MAP(CProcessWatchedDialog)
	ON_CBN_EDITCHANGE(IDC_COMBO1, OnEditchangeCombo1)
	ON_CBN_DROPDOWN (IDC_COMBO1, OnListDropDownCombo1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CProcessWatchedDialog message handlers

void CProcessWatchedDialog::OnOK() 
{  
    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;
    }
	
  CComboBox*pCombo1=(CComboBox*)GetDlgItem(IDC_COMBO1);
  CComboBox*pCombo2=(CComboBox*)GetDlgItem(IDC_COMBO2);
  CString strSelectedProcessName;
  CString strSelectedPriority;
  pCombo1->GetLBText(pCombo1->GetCurSel(),strSelectedProcessName); 
  pCombo2->GetLBText(pCombo2->GetCurSel(),strSelectedPriority); 
  HANDLE hSelectedProcess;
  
  CProcessInfo processinfo(TH32CS_SNAPALL);
  PROCESSENTRY32 pe={sizeof(pe)};
  BOOL fOk=processinfo.ProcessFirst(&pe);
  for( int n=0;fOk;fOk=processinfo.ProcessNext(&pe),n++)
  {   
	 LPCTSTR pszExeFile=pe.szExeFile;
     LPSTR pCurChar;
	   for(pCurChar=(pe.szExeFile+lstrlen(pe.szExeFile));(pCurChar!=pe.szExeFile)&&(*pCurChar!='\\');--pCurChar)
	   {;}
	   CString strProcessName;
	   if(osInfo.dwPlatformId==VER_PLATFORM_WIN32_NT)
	   {
		//CString strProcessName(pCurChar); 
		   strProcessName=pCurChar;
       }
	   else
	   {
	    //CString strProcessName(pCurChar+1); 
          strProcessName=pCurChar+1;
       }
	   if(strProcessName==strSelectedProcessName)
	   {  
		 hSelectedProcess= OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID );
	   }
  }
  
  ASSERT(SetSelectedProcessPriority(hSelectedProcess,strSelectedPriority));
  ::CloseHandle(hSelectedProcess);
  CDialog::OnOK();
  
}

BOOL CProcessWatchedDialog::SetSelectedProcessPriority(HANDLE hProcess,CString strPriority)
{
   //if(nPriorty<0||nPriorty>31)
	  // return FALSE;
   BOOL flags=FALSE;
   char*pFirst=strPriority.GetBuffer(0);
   char CaptionOfPriorty=*pFirst;
   switch(CaptionOfPriorty)
   {
     case 'N':
		 if(::SetPriorityClass(hProcess,NORMAL_PRIORITY_CLASS))
			 flags=TRUE;
		 break;
	 case 'H':
		 if(::SetPriorityClass(hProcess,HIGH_PRIORITY_CLASS))
			 flags=TRUE;
		 break;
     case 'I':
		 if(::SetPriorityClass(hProcess,IDLE_PRIORITY_CLASS))
			 flags=TRUE;
		 break;

	 case 'R':
		 if(::SetPriorityClass(hProcess,REALTIME_PRIORITY_CLASS))
			 flags=TRUE;
		 break;
	 default:
		 ;
   }
   

   return flags;
}
BOOL CProcessWatchedDialog::OnInitDialog() 
{   
	
	CDialog::OnInitDialog();
	CComboBox*pCombo1=(CComboBox*)GetDlgItem(IDC_COMBO1);
    CComboBox*pCombo2=(CComboBox*)GetDlgItem(IDC_COMBO2);
	char*pPriority[4]={"Idle","Normal","High","Realtime"};
	CString strPriority;
	/*for( int i=0;i<32;++i)
	{
		strPriority.Format("%d",i);
		//填充优先级组合框0~31
		pCombo2->AddString(LPCTSTR(strPriority));
	}*/
	//填充优先级组合框
    for(int i=0;i<4;++i)
	{
		strPriority.Format("%s",*(pPriority+i));
        pCombo2->AddString(LPCTSTR(strPriority));
	}
    pCombo2->SetCurSel(1);
	
	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)
	 {;}
	 
	 if(osInfo.dwPlatformId==VER_PLATFORM_WIN32_NT)
	 {
		CString strProcessName(pCurChar); 
        
		pCombo1->AddString(LPCTSTR(strProcessName));
	 }
	 else
	 {
	   CString strProcessName(pCurChar+1); 
        
	   pCombo1->AddString(LPCTSTR(strProcessName));
	 }
      
	}
    pCombo1->SetCurSel(0);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CProcessWatchedDialog::OnEditchangeCombo1() 
{
	// TODO: Add your control notification handler code here
	
}
void CProcessWatchedDialog::OnListDropDownCombo1()//动态调整组合框
{
    CComboBox*pCombo1=(CComboBox*)GetDlgItem(IDC_COMBO1);
	int nindex=pCombo1->GetCurSel();
	pCombo1->ResetContent();
	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)
	  {;}
	 
	  if(osInfo.dwPlatformId==VER_PLATFORM_WIN32_NT)
	  {
		CString strProcessName(pCurChar); 
        
		pCombo1->AddString(LPCTSTR(strProcessName));
	  }
	  else
	  {
	   CString strProcessName(pCurChar+1); 
        
	   pCombo1->AddString(LPCTSTR(strProcessName));
	  }
      
	}
   pCombo1->SetCurSel(nindex);
	
}

⌨️ 快捷键说明

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