各位大侠,小弟遇到一个问题,希望各位帮忙解决一下:
我在EVC写了一个窗口切换对话框,但是对话框将系统所有前台文件和后台文件都显示出来,其实我只要显示桌面已经打开对话框就可以了,但是小弟不知怎样过滤这些后台文件。请各位帮忙!!!如下是我写的代码:
// 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
HWND g_hWndArray[50]; //存所有窗口句柄
CString g_strArray[50]; //存所有窗口标题
int g_iWndCount = 0; //计数器
/////////////////////////////////////////////////////////////////////////////
// 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
//设置进程列表框标题
CListCtrl * pListCtrl = (CListCtrl*)GetDlgItem(IDC_LISTPROCESS);
CRect rt;
pListCtrl->GetClientRect(&rt);
pListCtrl->InsertColumn(0,_T("四路激光名称"), LVCFMT_LEFT, rt.Width() * 0.35);
pListCtrl->InsertColumn(1,_T("激光标题"), LVCFMT_LEFT, rt.Width() * 0.35);
pListCtrl->InsertColumn(2, _T("线程数"), LVCFMT_LEFT, rt.Width() * 0.30);
return TRUE; // return TRUE unless you set the focus to a control
}
DWORD CTasklistDlg::GetTaskListCE(PTASK_LIST pList)
{
HINSTANCE hKernel = NULL;
HINSTANCE hProcessSnap = NULL;
PROCESSENTRY32 pe32 = {0};
DWORD dwTaskCount = 0;
//状态toolhelp.dll动态连接库
hKernel = LoadLibrary(_T("toolhelp.dll"));
if (!hKernel) {
::MessageBox(NULL, L"Toolhelp.dll加载失败", L"进程列举", MB_OK);
return 0;
}
//创建进程映射
hProcessSnap = (HINSTANCE)CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
//如果失败,就退出
if (hProcessSnap == (HANDLE)-1)
return 0;
dwTaskCount = 0;
//设置保存进程相关内容的变量的结构大小
pe32.dwSize = sizeof(PROCESSENTRY32);
//获取第一个进程,并将此进程信息写入进程结构的变量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; //进程数目加1
++pList; //移到下一个结构内存块
}
while (Process32Next(hProcessSnap, &pe32)); //移至下一个进程
}
else
dwTaskCount = 0; //
CloseHandle (hProcessSnap);
return dwTaskCount;
}
BOOL 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;
// CWnd* pwnd = CWnd::FromHandle(hwnd);//
// 根据窗口句柄,得到进程标识
if (!GetWindowThreadProcessId( hwnd, &pid )) {
return TRUE;
}
//查找进程标识和列表中相同的进程标识
for (i=0; i
if (tlist
.dwProcessId == pid) {
// if(::GetWindowLong(hwnd,GWL_STYLE)&&WS_VISIBLE){//
if (::IsWindowVisible(hwnd)) {//书本
// if(hwnd!=NULL&&pwnd->IsWindowVisible()){//
tlist.hwnd = hwnd;
int nCnt = ::GetWindowText( hwnd, buf, TITLE_SIZE );
buf[nCnt] = '\0';
if (nCnt) {
lstrcpy( tlist.WindowTitle, buf );
}
}
break;
}
}
//继续列举窗体
return TRUE;
*/
CWnd* pWnd = CWnd::FromHandle(hwnd);
if(hwnd!=NULL&&pWnd->IsWindowVisible())
{
pWnd->GetWindowText(g_strArray[g_iWndCount]);
if((!g_strArray[g_iWndCount].IsEmpty())&&(g_strArray[g_iWndCount].CompareNoCase(L"Desktop")!=0))
{
g_hWndArray[g_iWndCount]=hwnd;
g_iWndCount++;
}
}
return TRUE;
}
void CTasklistDlg::GetWindowTitles(PTASK_LIST_ENUM te)
{
//列举系统所有窗体
EnumWindows((WNDENUMPROC) EnumWindowsProc, (LPARAM) te );
}
bool CTasklistDlg::ActivateProcess(PTASK_LIST tlist)
{
//判断是否存在主窗口,如存在就激活它
if (tlist->hwnd && tlist->WindowTitle)
{
::SetForegroundWindow(tlist->hwnd);
::SetActiveWindow(tlist->hwnd);
return TRUE;
}
else
{
return FALSE;
}
}
bool CTasklistDlg::KillProcess(PTASK_LIST tlist)
{
HANDLE hProcess;
//如果没有主窗体,则用TerminateProcess终止进程
if (!tlist->hwnd)
{
//根据进程标识,返回进程句柄
hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, tlist->dwProcessId );
//成功的话,终止进程
if (hProcess)
{
if (!TerminateProcess( hProcess, 1 ))
{
CloseHandle( hProcess );
return FALSE;
}
CloseHandle( hProcess );
return TRUE;
}
else
{
return FALSE;
}
}
//有主窗体,发送主窗口关闭消息,终止线程
else
{
::PostMessage( tlist->hwnd, WM_CLOSE, 0, 0 );
return TRUE;
}
}
void CTasklistDlg::OnBtnrefresh()
{
TASK_LIST_ENUM processList; //进程列表信息
TCHAR numBuf[10]; //存储进程中的线程数
memset(&g_tlist, 0, sizeof(TASK_LIST) * MAX_TASKS);
//得到进程列表信息
DWORD nNumTasks = GetTaskListCE(g_tlist);
processList.numtasks = nNumTasks;
processList.tlist = g_tlist;
//更新进程列表信息,获取主窗口句柄和标题
GetWindowTitles(&processList);
CListCtrl * pListCtrl = (CListCtrl*)GetDlgItem(IDC_LISTPROCESS);
//清除列表框所有项目
pListCtrl->DeleteAllItems();
memset(&numBuf,0,sizeof(numBuf));
//向列表框中添加进程相关信息
for (int i=0;i
{
//进程exe名,标题名,线程数
pListCtrl->InsertItem(i,_T("Test"));
pListCtrl->SetItemText(i,0,processList.tlist.ProcessName);
pListCtrl->SetItemText(i,1,processList.tlist.WindowTitle);
_itow(processList.tlist.cntThreads,numBuf,10);
pListCtrl->SetItemText(i,2,numBuf);
}
}
void CTasklistDlg::OnBtnactive()
{
CListCtrl * pListCtrl = (CListCtrl*)GetDlgItem(IDC_LISTPROCESS);
POSITION pos = pListCtrl->GetFirstSelectedItemPosition();
if (pos)
{
int nItemIndex = pListCtrl->GetNextSelectedItem(pos);
//激活进程
ActivateProcess(&g_tlist[nItemIndex]);
}
}
void CTasklistDlg::OnBtnterminate()
{
CListCtrl * pListCtrl = (CListCtrl*)GetDlgItem(IDC_LISTPROCESS);
POSITION pos = pListCtrl->GetFirstSelectedItemPosition();
if (pos)
{
int nItemIndex = pListCtrl->GetNextSelectedItem(pos);
//终止进程
KillProcess(&g_tlist[nItemIndex]);
}
//刷新进程列表
SendMessage(WM_COMMAND, (WPARAM)IDC_BTNREFRESH, 0);
}