#include "stdafx.h" #include "LaiPuLaser.h" #include "afxdialogex.h" #include "DlgChildConfigHistory.h" #include "GlobalFunction.h" #include "LogMgr.h" #include "FileMgr.h" IMPLEMENT_DYNAMIC(CDlgChildConfigHistory, CMyDlgView) BEGIN_MESSAGE_MAP(CDlgChildConfigHistory, CMyDlgView) ON_BN_CLICKED(IDC_SEARCH_BTN, &CDlgChildConfigHistory::OnBnClickedSearchBtn) ON_NOTIFY(TVN_ITEMEXPANDED, IDC_DATA_FOLDE_TREE, &CDlgChildConfigHistory::OnTvnItemexpandedDataFoldeTree) ON_NOTIFY(TVN_SELCHANGED, IDC_DATA_FOLDE_TREE, &CDlgChildConfigHistory::OnTvnSelchangedDataFoldeTree) ON_BN_CLICKED(IDC_SEARCH_BTN2, &CDlgChildConfigHistory::OnBnClickedExport) ON_BN_CLICKED(IDC_SEARCH_BTN3, &CDlgChildConfigHistory::OnBnClickedSearchByDate) END_MESSAGE_MAP() #define Data_List_Item_Cnt 20 CDlgChildConfigHistory::CDlgChildConfigHistory(CWnd* pParent /*=NULL*/) : CMyDlgView(CDlgChildConfigHistory::IDD, pParent) , m_SelDate(COleDateTime::GetCurrentTime()) { } CDlgChildConfigHistory::~CDlgChildConfigHistory() { } void CDlgChildConfigHistory::DoDataExchange(CDataExchange* pDX) { CMyDlgView::DoDataExchange(pDX); DDX_Control(pDX, IDC_DATA_FOLDE_TREE, m_MonitorDataTree); DDX_Control(pDX, IDC_DATA_LIST, m_DataList); DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER1, m_SelDate); } BOOL CDlgChildConfigHistory::OnInitDialog() { CMyDlgView::OnInitDialog(); CTreeCtrl &m_tree = m_MonitorDataTree; m_tree.ModifyStyle(NULL,TVS_HASBUTTONS|TVS_HASLINES|TVS_LINESATROOT|TVS_EDITLABELS); InitDataList(); UpdateData(FALSE); return TRUE; } //响应view 打开的时候 void CDlgChildConfigHistory::OnViewOpen() { if(m_bFirstOpen) { OnBnClickedSearchBtn(); m_bFirstOpen = false; } } //函数功能:获取驱动器 参数:路径名 void CDlgChildConfigHistory::GetLogicalDrives(HTREEITEM hParent) { CTreeCtrl &m_tree = m_MonitorDataTree; CString path = gProgramLaserTuiHuo->GetLaipuLaserDataDir("\\ConfigHistory\\"); m_tree.InsertItem(path,hParent); //在父节点hParent下添加盘符 } void CDlgChildConfigHistory::GetLogicalDrives(HTREEITEM hParent, COleDateTime SelDate) { CTreeCtrl &m_tree = m_MonitorDataTree; CString path = gProgramLaserTuiHuo->GetLaipuLaserDataDir("\\ConfigHistory\\"); CString DateStr = SelDate.Format("%Y\\%m\\%d\\"); m_tree.InsertItem(path+ DateStr, hParent); //在父节点hParent下添加盘符 } //函数功能:获取驱动盘符下所有子项文件夹 void CDlgChildConfigHistory::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 CDlgChildConfigHistory::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 CDlgChildConfigHistory::AddSubDir(HTREEITEM hParent) { CTreeCtrl &m_tree = m_MonitorDataTree; CString strPath = GetFullPath(hParent); //获取全路径 if(strPath.Find("xlsx")!=-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 CDlgChildConfigHistory::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 CDlgChildConfigHistory::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 CDlgChildConfigHistory::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 CDlgChildConfigHistory::InitDataList() { //设置风格 m_DataList.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES); //设置列 //m_DataList.InsertColumn(0,"Idx",LVCFMT_LEFT,50,-1); CString s; for(int k=0;k(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("xlsx")!=-1) { m_DataList.DeleteAllItems(); vector> StrVec; CString log("ReadFile : "); log += strPath; gLogMgr->WriteDebugLog(log); CFileMgr FileMgr; FileMgr.ReadFileToStrVec(strPath,StrVec,true); int size = StrVec.size(); for(int k=0;k=Data_List_Item_Cnt) break; m_DataList.SetItemText(k,i,StrVec[k][i]);//序号 } } } if(strPath.Find(".bmp")!=-1)//图像 { //用windows的图片查看器打开 PictureWindows(strPath); } *pResult = 0; } void CDlgChildConfigHistory::OnBnClickedExport() { CFileMgr FileMgr; bool bFile = m_CurSelPath.Find(".xlsx") > 0; CString destDir; if (bFile) { CFileDialog dlg( FALSE, _T(".xlsx"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("表格文件(*.xlsx)|*.xlsx")); if (dlg.DoModal() == IDOK) { destDir = dlg.GetPathName(); FileMgr.CopyDirOrFile(m_CurSelPath, destDir); } } else { CFolderPickerDialog dlg(NULL, 0, this, 0); if (dlg.DoModal() == IDOK) { destDir = dlg.GetPathName(); FileMgr.CopyDirOrFile(m_CurSelPath, destDir); } } } void CDlgChildConfigHistory::OnBnClickedSearchByDate() { UpdateData(TRUE); CTreeCtrl &m_tree = m_MonitorDataTree; m_tree.DeleteItem(m_hRoot);//全部删除 m_hRoot = m_tree.InsertItem("root"); //插入根节点 GetLogicalDrives(m_hRoot,m_SelDate); //自定义函数 获取驱动 GetDriveDir(m_hRoot); //自定义函数 获取驱动子项 m_tree.Expand(m_hRoot, TVE_EXPAND); //展开或折叠子项列表 TVE_EXPAND展开列表 }