📄 exploreview.cpp
字号:
// ExploreView.cpp : implementation of the CExploreView class
//
#include "stdafx.h"
#include "Explore.h"
#include "ExploreDoc.h"
#include "ExploreView.h"
#include "MainFrm.h"
#include "LeftView.h"
#include "Globle.h"
#include "DlgAttribute.h"
#include "DldRename.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CExploreView
IMPLEMENT_DYNCREATE(CExploreView, CListView)
BEGIN_MESSAGE_MAP(CExploreView, CListView)
//{{AFX_MSG_MAP(CExploreView)
ON_COMMAND(ID_POPUP_DELETE1, OnPopupDelete1)
ON_COMMAND(ID_POPUP_PROPERTY1, OnPopupProperty1)
ON_COMMAND(ID_POPUP_RENAME1, OnPopupRename1)
ON_UPDATE_COMMAND_UI(ID_POPUP_PROPERTY1, OnUpdatePopupProperty1)
ON_UPDATE_COMMAND_UI(ID_POPUP_RENAME1, OnUpdatePopupRename1)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CExploreView construction/destruction
CExploreView::CExploreView()
{
// TODO: add construction code here
}
CExploreView::~CExploreView()
{
}
BOOL CExploreView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CListView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CExploreView drawing
void CExploreView::OnDraw(CDC* pDC)
{
CExploreDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CListCtrl& refCtrl = GetListCtrl();
refCtrl.InsertItem(0, "Item!");
// TODO: add draw code for native data here
}
// 初始化列表框
void CExploreView::OnInitialUpdate()
{
CListView::OnInitialUpdate();
// TODO: You may populate your ListView with items by directly accessing
// its list control through a call to GetListCtrl().
// 创建图像列表
m_LargeImage.Create( 32, 32, ILC_COLOR16, 4, 0 );
m_SmallImage.Create( 16, 16, ILC_COLOR16, 4, 0 );
//分别向两个图象列表添加4个图标
m_LargeImage.Add(AfxGetApp()->LoadIcon(IDI_HARDDISK) );
m_LargeImage.Add(AfxGetApp()->LoadIcon(IDI_FLOPPYDISK) );
m_LargeImage.Add(AfxGetApp()->LoadIcon(IDI_FORDER1) );
m_LargeImage.Add(AfxGetApp()->LoadIcon(IDR_EXPLORTYPE) );
m_SmallImage.Add(AfxGetApp()->LoadIcon(IDI_HARDDISK) );
m_SmallImage.Add(AfxGetApp()->LoadIcon(IDI_FLOPPYDISK) );
m_SmallImage.Add(AfxGetApp()->LoadIcon(IDI_FORDER1) );
m_SmallImage.Add(AfxGetApp()->LoadIcon(IDR_EXPLORTYPE) );
//取得列表框控件指针,并指出列表框的大图标和小图标分别从
//m_LargeImage和m_SmallImage中取得。
CListCtrl &refList = this->GetListCtrl();
refList.SetImageList( &m_LargeImage, LVSIL_NORMAL );
refList.SetImageList( &m_SmallImage, LVSIL_SMALL );
CRect rect;
refList.GetClientRect(&rect);
// 向列表框中加入四列
refList.InsertColumn(0, "名称", LVCFMT_LEFT, rect.Width()/4);
refList.InsertColumn(1, "大小", LVCFMT_LEFT, rect.Width()/4);
refList.InsertColumn(2, "类型", LVCFMT_LEFT, rect.Width()/4);
refList.InsertColumn(3, "修改时间", LVCFMT_LEFT, rect.Width()/4);
}
/////////////////////////////////////////////////////////////////////////////
// CExploreView printing
BOOL CExploreView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CExploreView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CExploreView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CExploreView diagnostics
#ifdef _DEBUG
void CExploreView::AssertValid() const
{
CListView::AssertValid();
}
void CExploreView::Dump(CDumpContext& dc) const
{
CListView::Dump(dc);
}
CExploreDoc* CExploreView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CExploreDoc)));
return (CExploreDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CExploreView message handlers
void CExploreView::OnStyleChanged(int nStyleType, LPSTYLESTRUCT lpStyleStruct)
{
//TODO: add code to react to the user changing the view style of your window
}
//将指定目录所包含的文件和子目录添加到列表框中
void CExploreView::ListBuild(CString csPath)
{
LV_ITEM m_lsItem;
m_lsItem.mask = LVIF_TEXT|LVIF_IMAGE;
int nItemNumber =0 ;
CListCtrl &refList = this->GetListCtrl();
refList.DeleteAllItems(); //删除所有列表框
char cTemp[50];
CFileFind FindFile;
CString csFileName, csFileType, csFileEditTime;
char sFileSize[10];
CTime ctLastTime;
csPath += "\\*.*";
BOOL bContinue = FindFile.FindFile(csPath);
while (bContinue == TRUE)
{
bContinue = FindFile.FindNextFile();
if(FindFile.IsDots())
continue;
if(FindFile.IsDirectory()) //子目录
{
csFileName = FindFile.GetFileName();
csFileName = csFileName.Left(50); // 文件名
csFileType = "文件夹";
FindFile.GetLastWriteTime(ctLastTime); // 最后修改时间
csFileEditTime = ctLastTime.Format("%y.%m.%d %H.%M.%S");
strcpy(sFileSize,"");
m_lsItem.iItem = nItemNumber;
strcpy(cTemp,csFileName);
m_lsItem.pszText = cTemp;
m_lsItem.iImage = 2;
// 加入一个列表项
refList.InsertItem(nItemNumber,csFileName,2);
refList.SetItemText(nItemNumber,1,sFileSize);
refList.SetItemText(nItemNumber,2,csFileType);
refList.SetItemText(nItemNumber,3,csFileEditTime);
nItemNumber++;
}
else //文件
{
csFileName = FindFile.GetFileName();
csFileName = csFileName.Left(50);
csFileType = "文件";
UINT nFileSize = FindFile.GetLength();
FindFile.GetLastWriteTime(ctLastTime);
csFileEditTime = ctLastTime.Format("%y.%m.%d %H.%M.%S");
itoa(nFileSize,sFileSize,10);
strcpy(cTemp,csFileName);
refList.InsertItem(nItemNumber,csFileName,3);
refList.SetItemText(nItemNumber,1,sFileSize);
refList.SetItemText(nItemNumber,2,csFileType);
refList.SetItemText(nItemNumber,3,csFileEditTime);
nItemNumber++;
}
}
FindFile.Close();
}
void CExploreView::OnPopupDelete1()
{
// TODO: Add your command handler code here
CListCtrl &refList = this->GetListCtrl();
CExploreApp *pApp=(CExploreApp *) AfxGetApp();
CMainFrame *pMainFrame = (CMainFrame *)pApp->GetMainWnd();
CLeftView *FirstView = (CLeftView *) pMainFrame->GetLeftPane();
CString csPath = FirstView->GetPathName();
CString strItemText;
CString strFullFileName;
CFileFind ff;
int nSeleteIndex[1000];
int i=0;
POSITION pos = refList.GetFirstSelectedItemPosition();
if (pos == NULL)
TRACE0("No items were selected!\n");
else
{
//对选中的每一项进行检查删除
while (pos)
{
nSeleteIndex[i] = refList.GetNextSelectedItem(pos);
strItemText = refList.GetItemText(nSeleteIndex[i] , 0);
strFullFileName = csPath + "\\" + strItemText;
ff.FindFile(strFullFileName);
ff.FindNextFile();
// 如果是文件夹
if(ff.IsDirectory())
{
// 删除该目录
RemoveDirectory(strFullFileName);
// 删除树型控件的响应结点
FirstView->DeleteSubItem(strItemText);
}
else // 是文件
{
// 删除该文件(包括只读文件)
DeleteReadOnlyFile(strFullFileName);
}
i++;
}
// 删除所有选中的列表项
while (i--)
{
refList.DeleteItem(nSeleteIndex[i]);
}
}
}
void CExploreView::OnPopupProperty1()
{
// TODO: Add your command handler code here
CListCtrl &refList = this->GetListCtrl();
CExploreApp *pApp=(CExploreApp *) AfxGetApp();
CMainFrame *pMainFrame = (CMainFrame *)pApp->GetMainWnd();
CLeftView *FirstView = (CLeftView *) pMainFrame->GetLeftPane();
CString strLocation = FirstView->GetPathName();
CString strItemText;
CString strFullFileName;
CFileFind ff;
POSITION pos = refList.GetFirstSelectedItemPosition();
int nItem = refList.GetNextSelectedItem(pos);
strItemText = refList.GetItemText(nItem , 0);
strFullFileName = strLocation + "\\" + strItemText;
ff.FindFile(strFullFileName);
ff.FindNextFile();
CDlgAttribute dlg;
if(ff.IsDirectory()) //如果是文件夹
{
int bAttribute[4];
CString strAttribute[3];
// 取得文件夹属性
GetAttribute(strFullFileName, bAttribute, strAttribute);
dlg.m_strName = strItemText;
dlg.m_strType = "文件夹";
dlg.m_strLocation = strLocation;
dlg.m_bReadOnly = bAttribute[0];
dlg.m_bHide = bAttribute[1];
dlg.m_bArchive = bAttribute[2];
dlg.m_bSystem = bAttribute[3];
dlg.m_strCreatetime = strAttribute[0];
dlg.m_strSize = strAttribute[1];
dlg.m_strFileNumber = strAttribute[2];
dlg.DoModal();
}
else //文件
{
dlg.m_strName = strItemText;
dlg.m_strType = "文件";
dlg.m_strLocation = strLocation;
dlg.m_bReadOnly = ff.IsReadOnly();
dlg.m_bHide = ff.IsHidden();
dlg.m_bArchive = ff.IsArchived();
dlg.m_bSystem = ff.IsSerializable();
CTime CreateTime;
int bSuccess = ff.GetCreationTime(CreateTime);
if(bSuccess != 0)
dlg.m_strCreatetime = CreateTime.Format("%y年 %m月 %d日 %H时 %M分 %S秒");
else
dlg.m_strCreatetime = "(未知)";
dlg.m_strSize.Format("%d", ff.GetLength());
dlg.m_strFileNumber = "";
dlg.DoModal();
}
}
void CExploreView::OnPopupRename1()
{
// TODO: Add your command handler code here
CListCtrl &refList = this->GetListCtrl();
CExploreApp *pApp=(CExploreApp *) AfxGetApp();
CMainFrame *pMainFrame = (CMainFrame *)pApp->GetMainWnd();
CLeftView *FirstView = (CLeftView *) pMainFrame->GetLeftPane();
CString strPath = FirstView->GetPathName();
CTreeCtrl &refTree = FirstView->GetTreeCtrl();
CString strItemText;
CString strFullFileName;
CFileFind ff;
POSITION pos = refList.GetFirstSelectedItemPosition();
int nItem = refList.GetNextSelectedItem(pos);
strItemText = refList.GetItemText(nItem , 0);
strFullFileName = strPath + "\\" + strItemText;
CDldRename dlg;
dlg.m_OldName = strItemText; // 原文件名
dlg.m_NewName = strItemText; // 新文件名
if(dlg.DoModal()==IDOK)
{
CString strNewPath = strPath + "\\";
CString strOldPath = strPath + "\\";
strNewPath += dlg.m_NewName;
strOldPath += dlg.m_OldName;
ff.FindFile(strFullFileName);
ff.FindNextFile();
if(ff.IsDirectory()) // 文件夹
{
// 改变树型控件相应结点的文本
HTREEITEM hSelected = refTree.GetSelectedItem();
HTREEITEM hChild = refTree.GetChildItem(hSelected);
while(hChild)
{
CString pszText = refTree.GetItemText(hChild);
if(pszText == strItemText) // 找到该结点
{
refTree.SetItemText(hChild, dlg.m_NewName);
break;
}
HTREEITEM hNext = refTree.GetNextSiblingItem(hChild);
hChild = hNext;
}
// 改变列表控件相应结点的文本
refList.SetItemText(nItem , 0, dlg.m_NewName);
// 将原文件夹的所有内容复制到新文件夹
CopyDirectory(strNewPath, strOldPath);
// 删除原文件佳
RemoveDirectory(strOldPath);
}
else // 文件
{
// 更改文件名
rename(strOldPath,strNewPath);
// 更改列表控件相应结点的文本
refList.SetItemText(nItem , 0, dlg.m_NewName);
}
}
}
void CExploreView::OnUpdatePopupProperty1(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
CListCtrl &refList = this->GetListCtrl();
// 如果在列表框中只选中一项,菜单有效
if( refList.GetSelectedCount() == 1)
pCmdUI->Enable(true);
// 如果在列表框中选中多项,菜单无效
else
pCmdUI->Enable(false);
}
void CExploreView::OnUpdatePopupRename1(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
CListCtrl &refList = this->GetListCtrl();
// 如果在列表框中只选中一项,菜单有效
if( refList.GetSelectedCount() == 1)
pCmdUI->Enable(true);
// 如果在列表框中选中多项,菜单无效
else
pCmdUI->Enable(false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -