LeftView.cpp
资源名称:explorer.rar [点击查看]
上传用户:jorden
上传日期:2022-08-09
资源大小:83k
文件大小:12k
源码类别:
TreeView控件
开发平台:
Visual C++
- // LeftView.cpp : implementation of the CLeftView class
- //
- #include "stdafx.h"
- #include "explorer.h"
- #include "explorerDoc.h"
- #include "LeftView.h"
- #include "explorerView.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- ////////////////////////////////////////////////////////////////////////////
- // define my own data
- #define ILI_CDDRV 0
- #define ILI_CLSDFLD 1
- #define ILI_DRIVE 2
- #define ILI_FLOPPYDRV 3
- #define ILI_MYCOMP 4
- #define ILI_OPENFLD 5
- #define ILI_TEXTFILE 6
- #define MYCOMPUTER "My Computer"
- // define my own data
- ////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////
- // CLeftView
- IMPLEMENT_DYNCREATE(CLeftView, CTreeView)
- BEGIN_MESSAGE_MAP(CLeftView, CTreeView)
- //{{AFX_MSG_MAP(CLeftView)
- ON_WM_DESTROY()
- ON_NOTIFY_REFLECT(TVN_ITEMEXPANDING, OnItemexpanding)
- ON_NOTIFY_REFLECT(TVN_SELCHANGING, OnSelchanging)
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CTreeView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CTreeView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CTreeView::OnFilePrintPreview)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CLeftView construction/destruction
- CLeftView::CLeftView()
- {
- // TODO: add construction code here
- }
- CLeftView::~CLeftView()
- {
- }
- BOOL CLeftView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- cs.style |= TVS_HASBUTTONS | TVS_LINESATROOT | TVS_HASLINES;
- return CTreeView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CLeftView drawing
- void CLeftView::OnDraw(CDC* pDC)
- {
- CExplorerDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- }
- /////////////////////////////////////////////////////////////////////////////
- // CLeftView printing
- BOOL CLeftView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CLeftView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CLeftView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- void CLeftView::OnInitialUpdate()
- {
- CTreeView::OnInitialUpdate();
- m_pImageList = new CImageList();
- CWinApp* pApp = AfxGetApp();
- // ASSERT(m_pImageList != NULL); // serious allocation failure checking
- m_pImageList->Create(16, 16, ILC_COLOR8 | ILC_MASK, 9, 9);
- m_pImageList->Add(pApp->LoadIcon(ICO_CDDRV));
- m_pImageList->Add(pApp->LoadIcon(ICO_CLSDFLD));
- m_pImageList->Add(pApp->LoadIcon(ICO_DRIVE));
- m_pImageList->Add(pApp->LoadIcon(ICO_FLOPPYDRV));
- m_pImageList->Add(pApp->LoadIcon(ICO_MYCOMP));
- m_pImageList->Add(pApp->LoadIcon(ICO_OPENFLD));
- m_pImageList->Add(pApp->LoadIcon(ICO_TEXTFILE));
- GetTreeCtrl().SetImageList(m_pImageList , TVSIL_NORMAL);
- HTREEITEM hParent = GetTreeCtrl().InsertItem(MYCOMPUTER, ILI_MYCOMP, ILI_MYCOMP);
- InitTreeView(hParent);
- GetTreeCtrl().Expand(hParent, TVE_EXPAND);
- // TODO: You may populate your TreeView with items by directly accessing
- // its tree control through a call to GetTreeCtrl().
- }
- /////////////////////////////////////////////////////////////////////////////
- // CLeftView diagnostics
- #ifdef _DEBUG
- void CLeftView::AssertValid() const
- {
- CTreeView::AssertValid();
- }
- void CLeftView::Dump(CDumpContext& dc) const
- {
- CTreeView::Dump(dc);
- }
- CExplorerDoc* CLeftView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CExplorerDoc)));
- return (CExplorerDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CLeftView message handlers
- void CLeftView::InitTreeView(HTREEITEM hParent)
- {
- int nPos = 0;
- UINT nCount = 0;
- CString strDrive = "?:\";
- DWORD dwDriveList = ::GetLogicalDrives ();
- CString cTmp;
- while (dwDriveList) {
- if (dwDriveList & 1) {
- cTmp = strDrive;
- strDrive.SetAt (0, 0x41 + nPos);
- if (AddDrives(strDrive , hParent))
- nCount++;
- }
- dwDriveList >>= 1;
- nPos++;
- }
- return;
- }
- BOOL CLeftView::AddDrives(CString strDrive, HTREEITEM hParent)
- {
- HTREEITEM hItem;
- UINT nType = ::GetDriveType ((LPCTSTR) strDrive);
- UINT nDrive = (UINT) strDrive[0] - 0x41;
- switch (nType) {
- case DRIVE_REMOVABLE:
- hItem = GetTreeCtrl().InsertItem(strDrive, ILI_FLOPPYDRV, ILI_FLOPPYDRV, hParent);
- AddDummyNode(hItem);
- break;
- case DRIVE_FIXED:
- hItem = GetTreeCtrl().InsertItem(strDrive, ILI_DRIVE, ILI_DRIVE, hParent);
- AddDummyNode(hItem);
- break;
- case DRIVE_REMOTE:
- hItem = GetTreeCtrl().InsertItem(strDrive, ILI_DRIVE, ILI_DRIVE, hParent);
- AddDummyNode(hItem);
- break;
- case DRIVE_CDROM:
- hItem = GetTreeCtrl().InsertItem(strDrive, ILI_CDDRV, ILI_CDDRV, hParent);
- AddDummyNode(hItem);
- break;
- case DRIVE_RAMDISK:
- hItem = GetTreeCtrl().InsertItem(strDrive, ILI_CDDRV, ILI_CDDRV, hParent);
- AddDummyNode(hItem);
- break;
- default:
- return FALSE;
- }
- return true;
- }
- void CLeftView::OnDestroy()
- {
- CTreeView::OnDestroy();
- // TODO: Add your message handler code here
- if(m_pImageList != NULL)
- m_pImageList = NULL;
- delete m_pImageList;
- }
- void CLeftView::AddDummyNode(HTREEITEM hItem)
- {
- GetTreeCtrl().InsertItem ("", 0, 0, hItem);
- }
- CString CLeftView::GetPathFromItem(HTREEITEM hItem)
- {
- CString strPathName;
- while (hItem != NULL)
- {
- CString string = GetTreeCtrl().GetItemText (hItem);
- if ((string.Right (1) != "\") && !strPathName.IsEmpty ())
- string += "\";
- strPathName = string + strPathName;
- hItem = GetTreeCtrl().GetParentItem (hItem);
- }
- if(strPathName.Left(11) == MYCOMPUTER && strPathName.GetLength() > 11)
- strPathName = strPathName.Mid(12);
- return strPathName;
- }
- BOOL CLeftView::IsPathValid(CString &strPathName)
- {
- if (strPathName.GetLength () == 3)
- return TRUE;
- HANDLE hFind;
- WIN32_FIND_DATA fd;
- BOOL bResult = FALSE;
- if ((hFind = ::FindFirstFile ((LPCTSTR) strPathName, &fd)) !=
- INVALID_HANDLE_VALUE) {
- if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
- bResult = TRUE;
- ::CloseHandle (hFind);
- }
- return bResult;
- }
- BOOL CLeftView::IsMediaValid(CString &strPathName)
- {
- // Return TRUE if the drive doesn't support removable media.
- UINT nDriveType = GetDriveType ((LPCTSTR) strPathName);
- if ((nDriveType != DRIVE_REMOVABLE) && (nDriveType != DRIVE_CDROM))
- return TRUE;
- }
- HTREEITEM CLeftView::GetDriveNode(HTREEITEM hItem)
- {
- HTREEITEM hParent;
- do {
- hParent = GetTreeCtrl().GetParentItem (hItem);
- if (hParent != NULL)
- hItem = hParent;
- } while (hParent != NULL);
- return hItem;
- }
- UINT CLeftView::DeleteChildren(HTREEITEM hItem)
- {
- UINT nCount = 0;
- HTREEITEM hChild = GetTreeCtrl().GetChildItem (hItem);
- while (hChild != NULL) {
- HTREEITEM hNextItem = GetTreeCtrl().GetNextSiblingItem (hChild);
- GetTreeCtrl().DeleteItem (hChild);
- hChild = hNextItem;
- nCount++;
- // dwTreeItemCount--;
- }
- return nCount;
- }
- UINT CLeftView::AddDirectoryNodes(HTREEITEM hItem, CString &strPathName)
- {
- HANDLE hFind;
- WIN32_FIND_DATA fd;
- UINT nCount = 0;
- CString strFileSpec = strPathName;
- if (strFileSpec.Right (1) != "\")
- strFileSpec += "\";
- strFileSpec += "*.*";
- if ((hFind = ::FindFirstFile ((LPCTSTR) strFileSpec, &fd)) ==
- INVALID_HANDLE_VALUE) {
- if (IsDriveNode (hItem))
- AddDummyNode (hItem);
- return 0;
- }
- CWaitCursor wait;
- CExplorerDoc* pDoc = GetDocument();
- pDoc->m_ExplorerView->DeleteAllItems();
- do {
- if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
- {
- CString strFileName = (LPCTSTR) &fd.cFileName;
- if ((strFileName != ".") && (strFileName != "..")
- && (fd.dwFileAttributes != 22))
- {
- HTREEITEM hChild =
- GetTreeCtrl().InsertItem ((LPCTSTR) &fd.cFileName,
- ILI_CLSDFLD , ILI_OPENFLD , hItem , TVI_SORT);
- CString strNewPathName = strPathName;
- if (strNewPathName.Right (1) != "\")
- strNewPathName += "\";
- strNewPathName += (LPCTSTR) &fd.cFileName;
- SetButtonState (hChild, strNewPathName);
- nCount++;
- }
- }
- else
- {
- pDoc->m_ExplorerView->AddToListView(&fd);
- }
- } while (::FindNextFile (hFind, &fd));
- ::FindClose (hFind);
- return nCount;
- }
- void CLeftView::SetButtonState(HTREEITEM hItem, CString &strPathName)
- {
- if (HasSubdirectory (strPathName))
- AddDummyNode (hItem);
- }
- BOOL CLeftView::HasSubdirectory(CString &strPathName)
- {
- HANDLE hFind;
- WIN32_FIND_DATA fd;
- BOOL bResult = FALSE;
- CString strFileSpec = strPathName;
- if (strFileSpec.Right (1) != "\")
- strFileSpec += "\";
- strFileSpec += "*.*";
- if ((hFind = ::FindFirstFile ((LPCTSTR) strFileSpec, &fd)) !=
- INVALID_HANDLE_VALUE) {
- do {
- if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
- CString strFileName = (LPCTSTR) &fd.cFileName;
- if ((strFileName != ".") && (strFileName != ".."))
- bResult = TRUE;
- }
- } while (::FindNextFile (hFind, &fd) && !bResult);
- ::FindClose (hFind);
- }
- return bResult;
- }
- BOOL CLeftView::IsDriveNode(HTREEITEM hItem)
- {
- return (GetTreeCtrl().GetParentItem (hItem) == NULL) ? TRUE : FALSE;
- }
- void CLeftView::OnItemexpanding(NMHDR* pNMHDR, LRESULT* pResult)
- {
- NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
- // TODO: Add your control notification handler code here
- HTREEITEM hItem = pNMTreeView->itemNew.hItem;
- CString strPathName = GetPathFromItem (hItem);
- if (!IsMediaValid (strPathName))
- {
- HTREEITEM hRoot = GetDriveNode (hItem);
- GetTreeCtrl().Expand (hRoot, TVE_COLLAPSE);
- DeleteChildren (hRoot);
- AddDummyNode (hRoot);
- *pResult = TRUE;
- return;
- }
- // Delete the item if strPathName no longer specifies a valid path.
- if (!IsPathValid (strPathName))
- {
- if(strPathName != MYCOMPUTER && strPathName != "")
- {
- GetTreeCtrl().DeleteItem (hItem);
- *pResult = TRUE;
- return;
- }
- }
- CWaitCursor wait;
- // If the item is expanding, delete the dummy item attached to it
- // and add folder items. If the item is collapsing instead, delete
- // its folder items and add a dummy item if appropriate.
- if (pNMTreeView->action == TVE_EXPAND)
- {
- if(strPathName != MYCOMPUTER)
- {
- DeleteChildren (hItem);
- if (!AddDirectoryNodes (hItem, strPathName))
- *pResult = TRUE;
- }
- }
- else {
- if(strPathName != MYCOMPUTER)
- {
- DeleteChildren (hItem);
- if (IsDriveNode (hItem))
- AddDummyNode (hItem);
- else
- SetButtonState (hItem, strPathName);
- }
- }
- m_LocalPath = strPathName;
- *pResult = 0;
- }
- void CLeftView::OnSelchanging(NMHDR* pNMHDR, LRESULT* pResult)
- {
- NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
- // TODO: Add your control notification handler code here
- HTREEITEM hItem = pNMTreeView->itemNew.hItem;
- CString strPathName = GetPathFromItem (hItem);
- *pResult = FALSE;
- if(strPathName == MYCOMPUTER)
- return;
- CWaitCursor wait;
- if (!AddDirectoryNodes (hItem, strPathName))
- *pResult = TRUE;
- m_LocalPath = strPathName;
- *pResult = 0;
- }