📄 driveview.cpp
字号:
// DriveView.cpp : implementation of the CDriveView class
//
#include "stdafx.h"
#include "Drive.h"
#include "DriveDoc.h"
#include "DriveView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//Image indexes
#define ILI_HARD_DISK 0
#define ILI_FLOPPY 1
#define ILI_CD_ROM 2
#define ILI_NET_DRIVE 3
#define ILI_CLOSED_FOLDER 4
#define ILI_OPEN_FOLDER 5
/////////////////////////////////////////////////////////////////////////////
// CDriveView
IMPLEMENT_DYNCREATE(CDriveView, CTreeView)
BEGIN_MESSAGE_MAP(CDriveView, CTreeView)
//{{AFX_MSG_MAP(CDriveView)
ON_NOTIFY_REFLECT(TVN_ITEMEXPANDING,OnItemExpanding)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CDriveView construction/destruction
CDriveView::CDriveView()
{
// TODO: add construction code here
}
CDriveView::~CDriveView()
{
}
BOOL CDriveView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
//return CTreeView::PreCreateWindow(cs);
if (!CTreeView::PreCreateWindow(cs))
return FALSE;
cs.style|= TVS_HASLINES|TVS_LINESATROOT|TVS_HASBUTTONS|TVS_SHOWSELALWAYS;
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CDriveView drawing
void CDriveView::OnDraw(CDC* pDC)
{
CDriveDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
void CDriveView::OnInitialUpdate()
{
CTreeView::OnInitialUpdate();
// TODO: You may populate your TreeView with items by directly accessing
// its tree control through a call to GetTreeCtrl().
//m_ilDrives.Create(IDB_DRIVEIMAGES,16,1,RGB(255,0,255));
GetTreeCtrl().SetImageList(&m_ilDrives,TVSIL_NORMAL);
AddDrives();
TCHAR szPath[MAX_PATH];
::GetCurrentDirectory(sizeof(szPath) / sizeof(TCHAR),szPath);
CString strPath = szPath;
strPath = strPath.Left(3);
HTREEITEM hItem = GetTreeCtrl().GetNextItem(NULL,TVGN_ROOT);
while (hItem!=NULL)
{
if (GetTreeCtrl().GetItemText(hItem) == strPath)
break;
hItem = GetTreeCtrl().GetNextSiblingItem(hItem);
}
if (hItem!=NULL)
{
GetTreeCtrl().Expand(hItem,TVE_EXPAND);
GetTreeCtrl().Select(hItem,TVGN_CARET);
}
}
/////////////////////////////////////////////////////////////////////////////
// CDriveView printing
BOOL CDriveView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDriveView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CDriveView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CDriveView diagnostics
#ifdef _DEBUG
void CDriveView::AssertValid() const
{
CTreeView::AssertValid();
}
void CDriveView::Dump(CDumpContext& dc) const
{
CTreeView::Dump(dc);
}
CDriveDoc* CDriveView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDriveDoc)));
return (CDriveDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDriveView message handlers
int CDriveView::AddDrives()
{
int nPos = 0;
int nDrivesAdded = 0;
CString string = _T("?:\\");
DWORD dwDriveList = ::GetLogicalDrives();
while (dwDriveList)
{
if (dwDriveList & 1)
{
string.SetAt(0,_T('A')+nPos);
if(AddDriveItem(string))
nDrivesAdded++;
}
dwDriveList >>= 1;
nPos++;
}
return nDrivesAdded;
}
BOOL CDriveView::AddDriveItem(LPCTSTR pszDrive)
{
CString string;
HTREEITEM hItem;
UINT nType = ::GetDriveType(pszDrive);
switch(nType) {
case DRIVE_REMOVABLE:
hItem = GetTreeCtrl().InsertItem(pszDrive,ILI_FLOPPY,ILI_FLOPPY);
GetTreeCtrl().InsertItem(_T(" "),ILI_CLOSED_FOLDER,ILI_CLOSED_FOLDER,hItem);
break;
case DRIVE_FIXED:
case DRIVE_RAMDISK:
hItem = GetTreeCtrl().InsertItem(pszDrive,ILI_HARD_DISK,ILI_HARD_DISK);
SetButtonState(hItem,pszDrive);
break;
case DRIVE_REMOTE:
hItem = GetTreeCtrl().InsertItem(pszDrive,ILI_NET_DRIVE,ILI_NET_DRIVE);
SetButtonState(hItem,pszDrive);
break;
case DRIVE_CDROM:
hItem = GetTreeCtrl().InsertItem(pszDrive,ILI_CD_ROM,ILI_CD_ROM);
GetTreeCtrl().InsertItem(_T(""),ILI_CLOSED_FOLDER,ILI_CLOSED_FOLDER,hItem);
break;
default:
return FALSE;
}
return TRUE;
}
BOOL CDriveView::SetButtonState(HTREEITEM hItem,LPCTSTR pszPath)
{
HANDLE hFind;
WIN32_FIND_DATA fd;
BOOL bResult = FALSE;
CString strPath = pszPath;
if(strPath.Right(1)!=_T("\\"))
strPath+=_T("\\");
strPath+=_T("*.*");
if ((hFind = ::FindFirstFile(strPath,&fd)) == INVALID_HANDLE_VALUE)
return bResult;
do{
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
CString strComp = (LPCTSTR) &fd.cFileName;
if ((strComp!=_T("."))&&(strComp!=_T("..")))
{
GetTreeCtrl().InsertItem(_T(""),ILI_CLOSED_FOLDER,ILI_CLOSED_FOLDER,hItem);
bResult = TRUE;
break;
}
}
}while(::FindNextFile(hFind,&fd));
::FindClose(hFind);
return bResult;
}
void CDriveView::OnItemExpanding(NMHDR* pNMHDR,LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
HTREEITEM hItem = pNMTreeView->itemNew.hItem;
CString string = GetPathFromItem(hItem);
*pResult = FALSE;
if (pNMTreeView->action == TVE_EXPAND)
{
DeleteFirstChild(hItem);
if (AddDirectories(hItem,string)==0)
*pResult = TRUE;
}
else{
DeleteAllChildren(hItem);
if (GetTreeCtrl().GetParentItem(hItem) == NULL)
GetTreeCtrl().InsertItem(_T(""),ILI_CLOSED_FOLDER,ILI_CLOSED_FOLDER,hItem);
else
SetButtonState(hItem,string);
}
}
CString CDriveView::GetPathFromItem(HTREEITEM hItem)
{
CString strResult = GetTreeCtrl().GetItemText(hItem);
HTREEITEM hParent;
while ((hParent = GetTreeCtrl().GetParentItem(hItem))!=NULL)
{
CString string = GetTreeCtrl().GetItemText(hParent);
if(string.Right(1)!=_T("\\"))
string += _T("\\");
strResult = string + strResult;
hItem = hParent;
}
return strResult;
}
void CDriveView::DeleteFirstChild(HTREEITEM hItem)
{
HTREEITEM hChildItem;
if((hChildItem = GetTreeCtrl().GetChildItem(hItem))!=NULL)
GetTreeCtrl().DeleteItem(hChildItem);
}
void CDriveView::DeleteAllChildren(HTREEITEM hItem)
{
HTREEITEM hChildItem;
if((hChildItem = GetTreeCtrl().GetChildItem(hItem))!=NULL)
return;
do {
HTREEITEM hNextItem = GetTreeCtrl().GetNextSiblingItem(hChildItem);
GetTreeCtrl().DeleteItem(hChildItem);
hChildItem = hNextItem;
} while(hChildItem != NULL);
}
int CDriveView::AddDirectories(HTREEITEM hItem,LPCTSTR pszPath)
{
HANDLE hFind;
WIN32_FIND_DATA fd;
HTREEITEM hNewItem;
int nCount = 0;
CString strPath = pszPath;
if(strPath.Right(1)!=_T("\\"))
strPath+=_T("\\");
strPath+=_T("*.*");
if ((hFind = ::FindFirstFile(strPath,&fd)) == INVALID_HANDLE_VALUE)
{
if (GetTreeCtrl().GetParentItem(hItem)==NULL)
GetTreeCtrl().InsertItem(_T(""),ILI_CLOSED_FOLDER,ILI_CLOSED_FOLDER,hItem);
return 0;
}
do{
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
CString strComp = (LPCTSTR) &fd.cFileName;
if ((strComp!=_T("."))&&(strComp!=_T("..")))
{
hNewItem =
GetTreeCtrl().InsertItem((LPCTSTR) &fd.cFileName,
ILI_CLOSED_FOLDER,ILI_CLOSED_FOLDER,hItem);
CString strNewPath = pszPath;
if (strNewPath.Right(1)!=_T("\\"))
strNewPath += _T("\\");
strNewPath += (LPCTSTR)&fd.cFileName;
SetButtonState(hNewItem,strNewPath);
nCount++;
}
}
}while(::FindNextFile(hFind,&fd));
::FindClose(hFind);
return nCount;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -