用MFC实现简单资源管理器(2)

2019-02-15 21:54

InitTreeView(hParent);

GetTreeCtrl().Expand(hParent, TVE_EXPAND); }

#ifdef _DEBUG

void CLeftView::AssertValid() const {

CTreeView::AssertValid(); }

void CLeftView::Dump(CDumpContext& dc) const {

CTreeView::Dump(dc); }

CDriveExplorerDoc* CLeftView::GetDocument() // non-debug version is inline {

ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDriveExplorerDoc))); return (CDriveExplorerDoc*)m_pDocument; }

#endif //_DEBUG

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 (\}

CString CLeftView::GetPathFromItem(HTREEITEM hItem) {

CString strPathName; while (hItem != NULL) {

CString string = GetTreeCtrl().GetItemText (hItem);

if ((string.Right (1) != \ 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); }

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++; }

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;

CDriveExplorerDoc* pDoc = GetDocument(); pDoc->m_ExplorerView->DeleteAllItems(); do {

if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {

CString strFileName = (LPCTSTR) &fd.cFileName;

if ((strFileName != \ {

if (hParent != NULL) hItem = hParent; } while (hParent != NULL); return hItem;

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 != \ bResult = TRUE; }

} while (::FindNextFile (hFind, &fd) && !bResult);


用MFC实现简单资源管理器(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:城市道路交通管理规划方法与应用研究三

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: