使用方法:
1.添加TreeCtrl控件,右键ClassWizard中关联变量CTreeCtrl m_TreeCtrl,添加变量CImageList m_ImageList。
2.在OnInitDialog()函数加入代码:
m_ImageList.Create(16,16,ILC_COLOR24,10,0);
m_ImageList.SetBkColor(RGB(255,255,255));
m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON1));
m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON2));
m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON3));
m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON4));
m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON5));
m_TreeCtrl.SetImageList(&m_ImageList,TVSIL_NORMAL);
HTREEITEM hRoot = m_TreeCtrl.InsertItem(_T("我的电脑"),3,4);
HTREEITEM hRoot2_C = m_TreeCtrl.InsertItem(_T("C盘"),1,1,hRoot);
m_TreeCtrl.InsertItem(_T("D盘"),1,2,hRoot);
m_TreeCtrl.InsertItem(_T("E盘"),1,2,hRoot);
m_TreeCtrl.InsertItem(_T("files"),2,4,hRoot2_C);
m_TreeCtrl.Expand(hRoot,TVE_EXPAND);
显示文件列表:
1.在OnInitDialog()函数加入代码:
m_ImageList.Create(16,16,ILC_COLOR24,10,0);
m_ImageList.SetBkColor(RGB(255,255,255));
m_TreeCtrl.SetImageList(&m_ImageList,TVSIL_NORMAL);
char *pDriver,buf[50]={0};
//得到所有驱动盘号
GetLogicalDriveStrings(sizeof(buf),buf);
//依次添加作为根
for(pDriver=buf;*pDriver;pDriver+=strlen(pDriver)+1)
{
SHFILEINFO shfi;
SHGetFileInfo(pDriver,FILE_ATTRIBUTE_NORMAL,&shfi,sizeof(shfi),SHGFI_ICON|SHGFI_USEFILEATTRIBUTES|SHGFI_TYPENAME);
int nImageID = m_ImageList.Add(shfi.hIcon);
HTREEITEM hDriver = m_TreeCtrl.InsertItem(pDriver,nImageID,nImageID);
//以此驱动盘为根,在其下查找文件
InsertNode(pDriver,hDriver);
}
2.添加函数InsertNode:
void CCTreeCtrlDlg::InsertNode(CString szPath, HTREEITEM hNode)
{
CFileFind fileFind;
BOOL bRet = fileFind.FindFile(szPath + "\\*.*");
while (bRet)
{
bRet = fileFind.FindNextFile();
if (fileFind.IsDots())
continue;
//获取图标
SHFILEINFO shfi;
SHGetFileInfo(fileFind.GetFilePath(),0,&shfi,sizeof(shfi),SHGFI_ICON|SHGFI_TYPENAME);
int nImageID = m_ImageList.Add(shfi.hIcon);
HTREEITEM hChild = m_TreeCtrl.InsertItem(fileFind.GetFileName(),nImageID,nImageID,hNode);
if (fileFind.IsDirectory())
{
m_TreeCtrl.InsertItem(NULL,hChild);//插入空结点,借以显示+号
}
}
}
3.为TreeCtrl控件添加TVN_ITEMEXPANDED消息,此消息是:当点击+号时展示子结点。添加方法是:在TreeCtrl控件上点击右键-》“事件Event”-》选择TVN_ITEMEXPANDED-》选择“Add and Edit”
void CCTreeCtrlDlg::OnItemexpandedTree1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
if (TVE_EXPAND == pNMTreeView->action)
{
HTREEITEM hNode = pNMTreeView->itemNew.hItem;
while (m_TreeCtrl.ItemHasChildren(hNode))//删除全部子结点,如不删除,当再次点击后将重复添加文件
m_TreeCtrl.DeleteItem(m_TreeCtrl.GetChildItem(hNode));
InsertNode(GetFullPath(hNode),hNode);
}
*pResult = 0;
}
4.添加函数GetFullPath:
CString CCTreeCtrlDlg::GetFullPath(HTREEITEM hNode)
{
CString szPath = m_TreeCtrl.GetItemText(hNode);
while (hNode = m_TreeCtrl.GetParentItem(hNode))
szPath = m_TreeCtrl.GetItemText(hNode) + "\\" + szPath;
return szPath;
}
获取进程:
BOOL CPage1::OnInitDialog()
{
CDialog::OnInitDialog();
m_ImageList.Create(16,16,ILC_COLOR24,10,0);
m_ImageList.SetBkColor(RGB(255,255,255));
m_TreeCtrl.SetImageList(&m_ImageList,TVSIL_NORMAL);
GetProcess();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CPage1::GetProcess()
{
HANDLE hProcess=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
PROCESSENTRY32 lppe;
lppe.dwSize=sizeof(lppe);
BOOL bProcess=Process32First(hProcess,&lppe);
while(bProcess)
{
//获取进程路径
EnablePrivilege();
HANDLE hP=OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION,false,lppe.th32ProcessID);
char PathBuf[MAX_PATH]={0};
GetModuleFileNameEx(hP,NULL,PathBuf,sizeof(PathBuf));
SHFILEINFO shfi;
SHGetFileInfo(PathBuf,0,&shfi,sizeof(shfi),SHGFI_ICON|SHGFI_TYPENAME);
int nImageID = m_ImageList.Add(shfi.hIcon);
//进程名
HTREEITEM h1 = m_TreeCtrl.InsertItem(lppe.szExeFile,nImageID,nImageID);
//进程路径
m_TreeCtrl.InsertItem(PathBuf,nImageID,nImageID,h1);
//查找下一进程
bProcess=Process32Next(hProcess,&lppe);
}
}
void CPage1::EnablePrivilege()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken);
LookupPrivilegeValue(NULL,SE_DEBUG_NAME,&tkp.Privileges[0].Luid);
//一定要设置成SE_DEBUG_NAME,不然无法查看系统进程信息
tkp.PrivilegeCount=1;
tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken,FALSE,&tkp,0,(PTOKEN_PRIVILEGES)NULL,0);
}
如果想显示父子进程关系,则修改GetProcess()代码:
void CPage1::GetProcess()
{
HANDLE hProcess=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
PROCESSENTRY32 lppe;
lppe.dwSize=sizeof(lppe);
BOOL bProcess=Process32First(hProcess,&lppe);
CMap<DWORD,DWORD,HTREEITEM,HTREEITEM> map;//映射字典
while(bProcess)
{
//获取进程路径
EnablePrivilege();
HANDLE hP=OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION,false,lppe.th32ProcessID);
char PathBuf[MAX_PATH]={0};
GetModuleFileNameEx(hP,NULL,PathBuf,sizeof(PathBuf));
//获取图标
SHFILEINFO shfi;
SHGetFileInfo(PathBuf,0,&shfi,sizeof(shfi),SHGFI_ICON|SHGFI_TYPENAME);
int nImageID = m_ImageList.Add(shfi.hIcon);
HTREEITEM h1;
if (map[lppe.th32ParentProcessID])//判断是否有父进程ID
{
//有父进程则以父进程为根添加item
h1 = m_TreeCtrl.InsertItem(lppe.szExeFile,nImageID,nImageID,map[lppe.th32ParentProcessID]);
}
else//无父进程ID
{
//直接插入item
h1 = m_TreeCtrl.InsertItem(lppe.szExeFile,nImageID,nImageID);
}
//当前进程ID添加到map中
map[lppe.th32ProcessID] = h1;
//查找下一进程
bProcess=Process32Next(hProcess,&lppe);
}
//展开进程列表
POSITION pos = map.GetStartPosition();
while (pos != NULL)
{
HTREEITEM h1;
DWORD dwKey;
map.GetNextAssoc(pos,dwKey,h1);
m_TreeCtrl.Expand(h1,TVE_EXPAND);
}
}