// myExplorerView.cpp : implementation of the CMyExplorerView class // #include "stdafx.h" #include "myExplorer.h" #include "MainFrm.h" #include "myExplorerDoc.h" #include "myExplorerView.h" #include #include "DirTreeView.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMyExplorerView IMPLEMENT_DYNCREATE(CMyExplorerView, CListView) BEGIN_MESSAGE_MAP(CMyExplorerView, CListView) //{{AFX_MSG_MAP(CMyExplorerView) ON_WM_LBUTTONDBLCLK() //}}AFX_MSG_MAP // Standard printing commands ON_COMMAND(ID_FILE_PRINT, CListView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CListView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, CListView::OnFilePrintPreview) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMyExplorerView construction/destruction CMyExplorerView::CMyExplorerView() { // TODO: add construction code here } CMyExplorerView::~CMyExplorerView() { } BOOL CMyExplorerView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CListView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CMyExplorerView drawing void CMyExplorerView::OnDraw(CDC* pDC) { CMyExplorerDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here } void CMyExplorerView::OnInitialUpdate() { CListView::OnInitialUpdate(); CListCtrl &filelist=GetListCtrl(); //图表列表的设置 SHFILEINFO sfi;//存放文件信息的结构 sys_large_icon = (HIMAGELIST)SHGetFileInfo(NULL,0,&sfi,sizeof(sfi), SHGFI_SYSICONINDEX|SHGFI_LARGEICON);//获得系统图像列表 m_IconList.Attach (sys_large_icon); filelist.SetImageList(&m_IconList,LVSIL_NORMAL); m_IconList.Detach (); } ///////////////////////////////////////////////////////////////////////////// // CMyExplorerView printing BOOL CMyExplorerView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CMyExplorerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CMyExplorerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CMyExplorerView diagnostics #ifdef _DEBUG void CMyExplorerView::AssertValid() const { CListView::AssertValid(); } void CMyExplorerView::Dump(CDumpContext& dc) const { CListView::Dump(dc); } CMyExplorerDoc* CMyExplorerView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyExplorerDoc))); return (CMyExplorerDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMyExplorerView message handlers BOOL CMyExplorerView::AddListItem(CString wholepath) { CListCtrl &filelist=GetListCtrl(); CString filename; CFileFind finder; SHFILEINFO flargeicon; int i=0; //将当前目录设为要列举文件的目录 if(SetCurrentDirectory(wholepath)) { filelist.DeleteAllItems(); //把目录中的文件全部列举出来 BOOL bWorking = finder.FindFile("*.*"); while (bWorking) { bWorking = finder.FindNextFile(); filename.Format("%s",finder.GetFileName()); if(filename=="") break; if((filename!=".")&&(filename!="..")) { SHGetFileInfo(filename, NULL, &flargeicon, sizeof(flargeicon), SHGFI_DISPLAYNAME | SHGFI_ICON|SHGFI_LARGEICON); filelist.InsertItem(i,(LPCTSTR)filename,flargeicon.iIcon); i++; } } finder.Close (); return TRUE; } return FALSE; } void CMyExplorerView::AddListItem(LPSHELLFOLDER lpsf, LPITEMIDLIST lpifq, HTREEITEM hParent) { LPENUMIDLIST lpe=NULL; LPITEMIDLIST lpi=NULL, lpifqThisItem=NULL; LPTVITEMDATA lptvid=NULL; LPMALLOC lpMalloc=NULL; SHFILEINFO sfi; ULONG ulFetched; HRESULT hr; char szBuff[256]; UINT uFlags; int iIcon,i=0; HWND hwnd=::GetParent(m_hWnd); CMainFrame* mFrm = (CMainFrame*)AfxGetMainWnd(); DirTreeView *pLView = (DirTreeView *)(mFrm->m_wndSplitter.GetPane(0,0)); CTreeCtrl &filetree=pLView->GetTreeCtrl (); CListCtrl &filelist = GetListCtrl(); hr=::SHGetMalloc(&lpMalloc);//获得系统的IMalloc接口 if (SUCCEEDED(hr)) { //枚举当前元素中的各个文件对象 hr=lpsf->EnumObjects(hwnd, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &lpe); if (SUCCEEDED(hr)) { filelist.DeleteAllItems (); while (S_OK==lpe->Next(1, &lpi, &ulFetched)) { ULONG ulAttrs = SFGAO_HASSUBFOLDER | SFGAO_FOLDER |SFGAO_FILESYSTEM|SFGAO_BROWSABLE; lpsf->GetAttributesOf(1, (LPCITEMIDLIST*) &lpi, &ulAttrs); if (ulAttrs & (SFGAO_HASSUBFOLDER | SFGAO_FOLDER|SFGAO_FILESYSTEM |SFGAO_FILESYSANCESTOR )) { lptvid = (LPTVITEMDATA)lpMalloc->Alloc(sizeof(TVITEMDATA)); if (!lptvid) goto Done; if (!pLView->GetName(lpsf, lpi, SHGDN_NORMAL, szBuff)) goto Done; //出错,不能获得元素的名字 lpifqThisItem=pLView->ConcatPidls(lpifq, lpi); lptvid->lpi=(LPITEMIDLIST)lpMalloc-> Alloc(lpi->mkid.cb+sizeof(lpi->mkid.cb)); CopyMemory((PVOID)lptvid->lpi, (CONST VOID *)lpi, lpi->mkid.cb+sizeof(lpi->mkid.cb)); lptvid->lpsfParent=lpsf; lptvid->lpifq=pLView->ConcatPidls(lpifq, lpi); ZeroMemory(&sfi,sizeof(sfi)); uFlags=SHGFI_SYSICONINDEX|SHGFI_PIDL|SHGFI_LARGEICON; SHGetFileInfo((LPCSTR)lpifqThisItem,0,&sfi, sizeof(SHFILEINFO),uFlags); iIcon=sfi.iIcon ;//获得列表对象的图标 pLView->GetName(lpsf, lpi, SHGDN_NORMAL, szBuff);//获得列表的名称 filelist.InsertItem (i,szBuff,iIcon);//插入文件列表项 } lpMalloc->Free(lpifqThisItem); //释放分配的IMalloc接口 i++;//表示插入列表文件的位置 } lpMalloc->Free(lpi); lpi=0; return; } } else return; Done: if (lpe) lpe->Release(); if (lpi && lpMalloc) lpMalloc->Free(lpi); if (lpifqThisItem && lpMalloc) lpMalloc->Free(lpifqThisItem); if (lpMalloc) lpMalloc->Release(); } void CMyExplorerView::OnLButtonDblClk(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CMainFrame* mFrm = (CMainFrame*)AfxGetMainWnd(); DirTreeView *pLView = (DirTreeView *)(mFrm->m_wndSplitter.GetPane(0,0)); CTreeCtrl &filetree=pLView->GetTreeCtrl (); CListCtrl & filelist = GetListCtrl(); POSITION pos = filelist.GetFirstSelectedItemPosition (); int nItem=filelist.GetNextSelectedItem (pos); CString listname; listname=filelist.GetItemText (nItem,0);//得到双击项的文件名称 CString treename; HTREEITEM hItem; hItem = filetree.GetSelectedItem ();//hItem即为所选树节点 treename=filetree.GetItemText (hItem);//当前焦点所在的树节点名称 //判断树控件中选中的是不是桌面,若是桌面,另行处理 //为了双击桌面的快捷方式把它打开 LPSHELLFOLDER lpsf=NULL; LPITEMIDLIST lpi=NULL; LPENUMIDLIST lpe=0; TCHAR szPath[MAX_PATH]; ULONG ulFetched; char szBuff[MAX_PATH]; HTREEITEM hNextItem; HTREEITEM hChildItem; CString childname; CString wholepath; CString filename; DWORD file_or_folder; ULONG ulAttrs = SFGAO_HASSUBFOLDER | SFGAO_FOLDER|SFGAO_FILESYSTEM; SHGetDesktopFolder(&lpsf); SHGetSpecialFolderLocation(NULL,CSIDL_DESKTOP,&lpi); pLView->GetName(lpsf,lpi,SHGDN_NORMAL,szPath); //表示如果树控件焦点所在节点是“桌面” if(treename==szPath) { lpsf->EnumObjects(m_hWnd,SHCONTF_FOLDERS | SHCONTF_NONFOLDERS,&lpe);//获得“桌面”文件夹的IenumiDList接口指针lpe while(S_OK == lpe->Next(1,&lpi,&ulFetched))//枚举“桌面”下的各个子文件夹 { pLView->GetName(lpsf,lpi,SHGDN_NORMAL,szBuff);//获得lpi表示的子文件夹的显示名称 if(szBuff==listname) { lpsf->GetAttributesOf(1, (LPCITEMIDLIST*) &lpi, &ulAttrs); //如果双击桌面上的文件夹(包括真实的和虚拟的) if(ulAttrs & (SFGAO_HASSUBFOLDER|SFGAO_FOLDER)) { filetree.Expand (hItem,TVE_EXPAND); hChildItem = filetree.GetChildItem (hItem); while(hChildItem!=NULL) { childname=filetree.GetItemText (hChildItem); if (childname==listname) { filetree.Select(hChildItem, TVGN_CARET); filetree.Expand (hChildItem,TVE_EXPAND); break; } hNextItem = filetree.GetNextItem(hChildItem, TVGN_NEXT); hChildItem = hNextItem; } } //如果双击“桌面”上的快捷方式 else { if(szBuff==listname) { TCHAR pszPath[MAX_PATH]; SHGetPathFromIDList(lpi,pszPath); ShellExecute(m_hWnd, "open", pszPath, NULL, NULL, SW_SHOWNORMAL); } } break; } } } //表示如果树控件焦点所在节点不是“桌面” else { //如果此时为空,则说明选择的不是可得到真实路径的对象 if(DirTreeView::g_path_selected!="") { wholepath=DirTreeView::g_path_selected; wholepath=DirTreeView::g_path_selected+"\\"+listname; file_or_folder=GetFileAttributes(wholepath); //若是文件,双击它将它启动 if((file_or_folder & FILE_ATTRIBUTE_DIRECTORY)==0) { ShellExecute(m_hWnd, "open", wholepath, NULL, NULL, SW_SHOWNORMAL); return; } } if (filetree.ItemHasChildren (hItem)) { filetree.Expand (hItem,TVE_EXPAND); hChildItem = filetree.GetChildItem (hItem); while(hChildItem!=NULL) { childname=filetree.GetItemText (hChildItem); if (childname==listname) { //双击文件夹时,树控件对应的项呈展开的样子 filetree.Select(hChildItem, TVGN_CARET); filetree.Expand (hChildItem,TVE_EXPAND); break; } hNextItem = filetree.GetNextItem(hChildItem, TVGN_NEXT); hChildItem = hNextItem; } } } // *pResult = 0; CListView::OnLButtonDblClk(nFlags, point); } void CMyExplorerView::pathBack() { CMyExplorerDoc *pDoc = GetDocument(); CMainFrame* mFrm = (CMainFrame*)AfxGetMainWnd(); DirTreeView *pLView = (DirTreeView *)(mFrm->m_wndSplitter.GetPane(0,0)); CTreeCtrl &filetree=pLView->GetTreeCtrl (); PPATHNODE pNode; if((pNode = pDoc->PPathStack->Pop()) != NULL) { HTREEITEM hItem = pNode->hItem; filetree.Select(hItem, TVGN_CARET); pDoc->pForwardStack->Push(pNode->hItem); } else { filetree.Select(pLView->hParent, TVGN_CARET); } } void CMyExplorerView::pathForward() { CMyExplorerDoc *pDoc = GetDocument(); CMainFrame* mFrm = (CMainFrame*)AfxGetMainWnd(); DirTreeView *pLView = (DirTreeView *)(mFrm->m_wndSplitter.GetPane(0,0)); CTreeCtrl &filetree=pLView->GetTreeCtrl (); PPATHNODE pNode; HTREEITEM hItem; if((pNode = pDoc->pForwardStack->Pop()) != NULL) { hItem = pNode->hItem; filetree.Select(hItem, TVGN_CARET); pDoc->PPathStack->Push(hItem); } else { filetree.Select(pLView->hParent, TVGN_CARET); } } void CMyExplorerView::pathUp() { CMyExplorerDoc *pDoc = GetDocument(); CMainFrame* mFrm = (CMainFrame*)AfxGetMainWnd(); DirTreeView *pLView = (DirTreeView *)(mFrm->m_wndSplitter.GetPane(0,0)); CTreeCtrl &filetree=pLView->GetTreeCtrl (); HTREEITEM hItem,hParent; hItem = filetree.GetSelectedItem ();//hItem即为所选树节点 if(hItem == pLView->hParent) return; hParent = filetree.GetParentItem(hItem); filetree.Select(hParent, TVGN_CARET); }