⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filepathview.cpp

📁 不错的资源管理器
💻 CPP
字号:
// FilePathView.cpp : implementation file
//

#include "stdafx.h"
#include "DriveExplorer.h"
#include "FilePathView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFilePathView

IMPLEMENT_DYNCREATE(CFilePathView, CTreeView)

CFilePathView::CFilePathView()
{
	m_pImageList = NULL;
}

CFilePathView::~CFilePathView()
{
	if(NULL != m_pImageList)
	{
		m_pImageList->DeleteImageList();
		delete m_pImageList;
		m_pImageList = NULL;
	}
}


BEGIN_MESSAGE_MAP(CFilePathView, CTreeView)
	//{{AFX_MSG_MAP(CFilePathView)
	ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelchanged)
	ON_NOTIFY_REFLECT(TVN_ITEMEXPANDING, OnItemexpanding)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFilePathView drawing

void CFilePathView::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// CFilePathView diagnostics

#ifdef _DEBUG
void CFilePathView::AssertValid() const
{
	CTreeView::AssertValid();
}

void CFilePathView::Dump(CDumpContext& dc) const
{
	CTreeView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CFilePathView message handlers

void CFilePathView::OnInitialUpdate() 
{
	CTreeView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
	CTreeCtrl& TreeCtrl = this->GetTreeCtrl();

	long lWndStyle = ::GetWindowLong(this->GetSafeHwnd(), GWL_STYLE);
	lWndStyle |= TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_DISABLEDRAGDROP;
	::SetWindowLong(this->GetSafeHwnd(), GWL_STYLE, lWndStyle);

	if(NULL == m_pImageList)
	{
		m_pImageList = new CImageList;
	}
	if(m_pImageList)
	{
		m_pImageList->Create(16, 16, ILC_COLOR8 | ILC_MASK, 0, 4);
	}
	HICON HIconTemp = AfxGetApp()->LoadIcon(MAKEINTRESOURCE(IDI_MYCOMP));
	m_pImageList->Add(HIconTemp);
	HIconTemp = AfxGetApp()->LoadIcon(MAKEINTRESOURCE(IDI_DRIVE));
	m_pImageList->Add(HIconTemp);
	HIconTemp = AfxGetApp()->LoadIcon(MAKEINTRESOURCE(IDI_FILEFOLDER));
	m_pImageList->Add(HIconTemp);
	
	TreeCtrl.SetImageList(m_pImageList, TVSIL_NORMAL);
	HTREEITEM hParent = TreeCtrl.InsertItem(_T("我的电脑"), 0, 0);

	int nPos = 0;
    CString strDrive = "?:\\";
	char  szDriveList[12] = {0};
    DWORD dwDriveList = ::GetLogicalDrives ();
	CString cTmp;
    while (dwDriveList)
	{
        if (dwDriveList & 1)
		{
			cTmp = strDrive;
            strDrive.SetAt (0, 0x41 + nPos);
            if(::GetDriveType(strDrive) == DRIVE_FIXED)
			{
				sprintf(szDriveList, "本地磁盘%c", 0x41 + nPos);
				HTREEITEM hItem = TreeCtrl.InsertItem(szDriveList, 1, 1, hParent, TVI_LAST);
				//GetTreeCtrl().InsertItem ("", hItem);
			}
        }
        dwDriveList >>= 1;
        nPos++;
    }
	
	GetDocument()->SetCurSelFilePathInTreeView("");
	GetDocument()->SetCurParentFilePath("");
	TreeCtrl.SetCheck(hParent, TRUE);
	TreeCtrl.Expand(hParent, TVE_EXPAND);
}

void CFilePathView::OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	HTREEITEM hItem = pNMTreeView->itemNew.hItem; 
	hCurItem = hItem;
	if(NULL == GetTreeCtrl().GetParentItem(hItem))
		return;
// 	CString str = GetDocument()->GetCurParentFilePath();
// 	CString strPath;
// 	if(str == "")
// 	{
// 		char chTemp = GetTreeCtrl().GetItemText(hItem).GetAt(8);
// 		strPath.Format("%c:\\", chTemp);
// 	}
// 	else
// 	{
// 		if(GetTreeCtrl().GetRootItem() == GetTreeCtrl().GetParentItem(hItem))
// 		{
// 			char chTemp = GetTreeCtrl().GetItemText(hItem).GetAt(8);
// 			strPath.Format("%c:\\", chTemp);
// 		}
// 		else
// 		{
// 			strPath.Format("%s%s\\", str, GetTreeCtrl().GetItemText(hItem));
// 		}
// 	}
	CString strPath;
	CString str ="";
	HTREEITEM hTempItem = hItem;
	while(hTempItem != GetTreeCtrl().GetRootItem())
	{
		strPath.Format("%s\\%s", GetTreeCtrl().GetItemText(hTempItem), str);
		str.Format("%s", strPath);
		hTempItem = GetTreeCtrl().GetParentItem(hTempItem);
	}
	strPath = strPath.Right(strPath.GetLength() - 8);
	strPath.Insert(1, ":");
	GetDocument()->SetCurParentFilePath(strPath);
	
   HTREEITEM hNextItem;
   HTREEITEM hChildItem = GetTreeCtrl().GetChildItem(hItem);
   while (hChildItem != NULL)
   {
      hNextItem = GetTreeCtrl().GetNextItem(hChildItem, TVGN_NEXT);
      GetTreeCtrl().DeleteItem(hChildItem);
      hChildItem = hNextItem;
   }

	WIN32_FIND_DATA fd;
	strPath += "*.*";
	HANDLE handle = FindFirstFile((LPCTSTR)strPath, &fd);
	if(INVALID_HANDLE_VALUE == handle)
		return;
	do
	{
		if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			if(fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
			{

			}
			else
			{
				if(strcmp(fd.cFileName, ".") == 0 || strcmp(fd.cFileName, "..") == 0)
					continue;
				HTREEITEM HFindDirct = GetTreeCtrl().InsertItem(fd.cFileName, 2, 2, hItem, TVI_LAST);
				//GetTreeCtrl().InsertItem ("", HFindDirct);
			}
		}
	}while(	FindNextFile(handle, &fd));
	*pResult = 0;
	
	GetTreeCtrl().Expand(hItem, TVE_EXPAND);
	GetDocument()->UpdateAllViews(this);
}

CDriveExplorerDoc* CFilePathView::GetDocument()
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDriveExplorerDoc)));
	return (CDriveExplorerDoc*)m_pDocument;
}

void CFilePathView::OnItemexpanding(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
 	if(hCurItem == pNMTreeView->itemNew.hItem)
 		return;
	OnSelchanged(pNMHDR, pResult);
	*pResult = 0;
}

void CFilePathView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	// TODO: Add your specialized code here and/or call the base class
   //HTREEITEM hNextItem;
   HTREEITEM hChildItem = GetTreeCtrl().GetChildItem(hCurItem);
   while (hChildItem != NULL)
   {
     if(lstrcmp(GetTreeCtrl().GetItemText(hChildItem), GetDocument()->GetCurSelFilePathInTreeView()) == 0)
		break;
      hChildItem = GetTreeCtrl().GetNextItem(hChildItem, TVGN_NEXT);
   }
   //hCurItem = hChildItem;
  
  NM_TREEVIEW NMTreeView;
   NMTreeView.action = TVN_ITEMEXPANDING;
   NMTreeView.itemNew.hItem = hChildItem;
   NMTreeView.itemOld.hItem = hCurItem;
 //   CRect rect;
//     GetTreeCtrl().GetItemRect(hChildItem, rect, FALSE);
 //  NMTreeView->ptDrag = rect.TopLeft();
   LRESULT Result;
    OnItemexpanding((NMHDR*)&NMTreeView, &Result);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -