#include "stdafx.h" #include "LaiPuLaser.h" #include "afxdialogex.h" #include "DlgMapHistory.h" #include "GlobalFunction.h" #include "LogMgr.h" #include "FileMgr.h" #include "CStringFuc.h" #include "WorkFileMgr.h" #include "LaiPuLaserView.h" #define MAP_HISTORY_PATH "\\MapHistory\\" #define MAP_HISTORY_FILE_SUFFIX "obj" IMPLEMENT_DYNAMIC(CDlgMapHistory, CDialogEx) BEGIN_MESSAGE_MAP(CDlgMapHistory, CDialogEx) ON_NOTIFY(TVN_ITEMEXPANDED, IDC_DATA_FOLDE_TREE, &CDlgMapHistory::OnTvnItemexpandedDataFoldeTree) ON_NOTIFY(TVN_SELCHANGED, IDC_DATA_FOLDE_TREE, &CDlgMapHistory::OnTvnSelchangedDataFoldeTree) ON_BN_CLICKED(IDC_SEARCH_BY_NAME, &CDlgMapHistory::OnBnClickedSearchByName) END_MESSAGE_MAP() CDlgMapHistory::CDlgMapHistory(CWnd* pParent /*=NULL*/) : CDialogEx(CDlgMapHistory::IDD, pParent) { } CDlgMapHistory::~CDlgMapHistory() { } void CDlgMapHistory::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Control(pDX, IDC_DATA_FOLDE_TREE, m_MonitorDataTree); } BOOL CDlgMapHistory::OnInitDialog() { CDialogEx::OnInitDialog(); CTreeCtrl &m_tree = m_MonitorDataTree; m_tree.ModifyStyle(NULL,TVS_HASBUTTONS|TVS_HASLINES|TVS_LINESATROOT|TVS_EDITLABELS); OnBnClickedSearchBtn(); UpdateData(FALSE); return TRUE; } //函数功能:获取驱动器 参数:路径名 void CDlgMapHistory::GetLogicalDrives(HTREEITEM hParent) { CTreeCtrl &m_tree = m_MonitorDataTree; CString path = gProgramLaserTuiHuo->GetLaipuLaserDataDir(MAP_HISTORY_PATH); m_tree.InsertItem(path,hParent); //在父节点hParent下添加盘符 } void CDlgMapHistory::GetLogicalDrives(HTREEITEM hParent, COleDateTime SelDate,bool bByMonth) { CTreeCtrl &m_tree = m_MonitorDataTree; CString path = gProgramLaserTuiHuo->GetLaipuLaserDataDir(MAP_HISTORY_PATH); CString DateStr; if(bByMonth) DateStr = SelDate.Format("%Y\\%m\\"); else DateStr = SelDate.Format("%Y\\%m\\%d\\"); m_tree.InsertItem(path+ DateStr, hParent); //在父节点hParent下添加盘符 } //函数功能:获取驱动盘符下所有子项文件夹 void CDlgMapHistory::GetDriveDir(HTREEITEM hParent) { CTreeCtrl &m_tree = m_MonitorDataTree; HTREEITEM hChild = m_tree.GetChildItem(hParent); //获取指定位置中的子项 while(hChild) { CString strText = m_tree.GetItemText(hChild); //检索列表中项目文字 if(strText.Right(1) != "\\") //从右边1开始获取从右向左nCount个字符 strText += _T("\\"); strText += "*.*"; //将当前目录下文件枚举并InsertItem树状显示 CFileFind file; //定义本地文件查找 BOOL bContinue = file.FindFile(strText); //查找包含字符串的文件 while(bContinue) { bContinue = file.FindNextFile(); //查找下一个文件 if(file.IsDirectory() && !file.IsDots()) //找到文件为内容且不为点"." { m_tree.InsertItem(file.GetFileName(),hChild); //添加盘符路径下树状文件夹 } } GetDriveDir(hChild); //递归调用 hChild = m_tree.GetNextItem(hChild,TVGN_NEXT); //获取树形控件TVGN_NEXT下兄弟项 } } //函数功能:获取树项目全根路径 CString CDlgMapHistory::GetFullPath(HTREEITEM hCurrent) { CTreeCtrl &m_tree = m_MonitorDataTree; CString strTemp; CString strReturn = ""; while(hCurrent != m_hRoot) { strTemp = m_tree.GetItemText(hCurrent); //检索列表中项目文字 if(strTemp.Right(1) != "\\") strTemp += "\\"; strReturn = strTemp + strReturn; hCurrent = m_tree.GetParentItem(hCurrent); //返回父项目句柄 } return strReturn; } //函数功能:添加子目录 void CDlgMapHistory::AddSubDir(HTREEITEM hParent) { CTreeCtrl &m_tree = m_MonitorDataTree; CString strPath = GetFullPath(hParent); //获取全路径 if(strPath.Find(MAP_HISTORY_FILE_SUFFIX)!=-1) { int len = strPath.GetLength(); strPath.Delete(len-1,1);//删除最后一个'\' return; } if(strPath.Right(1) != "\\") strPath += "\\"; strPath += "*.*"; CFileFind file; BOOL bContinue = file.FindFile(strPath); //查找包含字符串的文件 while(bContinue) { bContinue = file.FindNextFile(); //查找下一个文件 //if(file.IsDirectory() && !file.IsDots()) if(!file.IsDots()) { m_tree.InsertItem(file.GetFileName(),hParent); } } } //删除hItem 所有子节点 void CDlgMapHistory::DelAllChildOfItem(HTREEITEM hItem) { CTreeCtrl &m_Tree = m_MonitorDataTree; { HTREEITEM hNextItem=NULL; HTREEITEM hChildItem=m_Tree.GetChildItem(hItem); while (hChildItem!=NULL) { hNextItem=m_Tree.GetNextItem(hChildItem,TVGN_NEXT); m_Tree.DeleteItem(hChildItem); hChildItem=hNextItem; } } } //响应节点展开的事件 void CDlgMapHistory::OnTvnItemexpandedDataFoldeTree(NMHDR *pNMHDR, LRESULT *pResult) { LPNMTREEVIEW pNMTreeView = reinterpret_cast(pNMHDR); CTreeCtrl &m_tree = m_MonitorDataTree; TVITEM item = pNMTreeView->itemNew; //发送\接受关于树形视图项目信息 if(item.hItem == m_hRoot) return; //获取item.hItem 的第一个ChildItem HTREEITEM hChild = m_tree.GetChildItem(item.hItem); //删除当前的子节点(这样就能保持点开的时候是最新状态) if(m_tree.ItemHasChildren(hChild)) return; //插入子节点 while(hChild) { AddSubDir(hChild); //添加子目录 hChild = m_tree.GetNextItem(hChild,TVGN_NEXT); //获取树形控件TVGN_NEXT下兄弟项 } *pResult = 0; } //更新 void CDlgMapHistory::OnBnClickedSearchBtn() { CTreeCtrl &m_tree = m_MonitorDataTree; m_tree.DeleteItem(m_hRoot);//全部删除 m_hRoot = m_tree.InsertItem("root"); //插入根节点 GetLogicalDrives(m_hRoot); //自定义函数 获取驱动 GetDriveDir(m_hRoot); //自定义函数 获取驱动子项 m_tree.Expand(m_hRoot,TVE_EXPAND); //展开或折叠子项列表 TVE_EXPAND展开列表 } void CDlgMapHistory::OnTvnSelchangedDataFoldeTree(NMHDR *pNMHDR, LRESULT *pResult) { LPNMTREEVIEW pNMTreeView = reinterpret_cast(pNMHDR); // NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR; TVITEM item = pNMTreeView->itemNew; if(item.hItem == m_hRoot) return; CString strPath = GetFullPath(item.hItem); int len = strPath.GetLength(); strPath.Delete(len-1,1);//删除最后一个'\' gLogMgr->WriteDebugLog(strPath); m_CurSelPath = strPath; if(strPath.Find(MAP_HISTORY_FILE_SUFFIX)!=-1) { gWorkFileMgr->ReadObjTemplateFile(strPath); m_pView->RefreshView(); CFileMgr FileMgr; CString FileName = FileMgr.GetFileNameFromPath(strPath,true); int len1 = FileName.GetLength(); int idx = FileName.Find("]"); if(idx!=-1)//只保留map 名称 { FileName = FileName.Right(len1-idx-1); } GetDlgItem(IDC_MAP_NAME_EDIT)->SetWindowText(FileName); } *pResult = 0; } //查找路径下的节点,递归函数 void CDlgMapHistory::GetDriveDir_ByPath(HTREEITEM hParent,CString FindPath,CString SearchName) { COleDateTime SelDateStart; COleDateTime SelDateEnd = COleDateTime::GetCurrentTime(); CFileMgr FileMgr; vector DirPathVec;//子目录路径 FileMgr.GetChildFileOrDirName(true,FindPath,DirPathVec,""); //没有子目录表示根目录 if(DirPathVec.empty()) { CString DataPath = gProgramLaserTuiHuo->GetLaipuLaserDataDir(MAP_HISTORY_PATH); int FindPathLen = FindPath.GetLength(); int DataPathLen = DataPath.GetLength(); if(DataPathLen>=FindPathLen) return; CString ChildDirName = FindPath.Right(FindPathLen-DataPathLen);//子节点的名称 CTreeCtrl &m_tree = m_MonitorDataTree; HTREEITEM hChild = m_tree.GetChildItem(hParent); //获取指定位置中的子项 bool bChild2Creat = false; vector FilePathVec;//文件路径 FileMgr.GetChildFileOrDirName(false,FindPath,FilePathVec,MAP_HISTORY_FILE_SUFFIX); int size = FilePathVec.size(); for(int k=0;kWriteDebugLog("Func-->OnBnClickedSearchByName"); CString SearchName; GetDlgItem(IDC_MAP_NAME_EDIT)->GetWindowText(SearchName); if(SearchName=="") { OnBnClickedSearchBtn(); return; } CTreeCtrl &m_tree = m_MonitorDataTree; m_tree.DeleteItem(m_hRoot);//全部删除 m_hRoot = m_tree.InsertItem("root");//插入根节点 CString path = gProgramLaserTuiHuo->GetLaipuLaserDataDir(MAP_HISTORY_PATH); m_tree.InsertItem(path, m_hRoot);//在父节点hParent下添加盘符 m_bFirstDir = true;//第一个目录 GetDriveDir_ByPath(m_hRoot,path,SearchName);//自定义函数 获取驱动子项 //m_tree.Expand(m_hRoot, TVE_EXPAND); //展开或折叠子项列表 TVE_EXPAND展开列表 ExpandTree(&m_tree,m_hRoot);//展开树的所有节点 } //展开树的所有节点 void CDlgMapHistory::ExpandTree(CTreeCtrl* pTreeCtrl,HTREEITEM hItem) { HTREEITEM hChild,hNext,hParent; if((!hItem)||(!pTreeCtrl)) return; //存在则展开节点 pTreeCtrl->Expand(hItem,TVE_EXPAND); hChild = pTreeCtrl->GetNextItem(hItem,TVGN_CHILD); if(hChild) {//如果有子节点,展开子节点 ExpandTree(pTreeCtrl,hChild); } else {//没有子节点,寻找其兄弟节点 hNext = pTreeCtrl->GetNextItem(hItem,TVGN_NEXT); if(hNext) {//存在兄弟节点 ExpandTree(pTreeCtrl,hNext); } else {//不存在兄弟节点,继续找父节点 hParent = pTreeCtrl->GetParentItem(hItem); hNext = pTreeCtrl->GetNextItem(hParent,TVGN_NEXT); if(hNext) ExpandTree(pTreeCtrl,hNext); } } } void CDlgMapHistory::SearchByName(CString SearchName) { GetDlgItem(IDC_MAP_NAME_EDIT)->SetWindowText(SearchName); OnBnClickedSearchByName(); }