前序
上一步我们实现了双击响应事件,这节我们将在里面添加响应函数。函数主要是用于双击一个文件夹后进入到该文件夹内部,并可以通过双击 ..两个点这一栏回到上级目录。
实现
1、我们来到LocalFileSysDlg.h文件下,我们在CLocalFileSysDlg类里面声明如下一些变量及函数
VOID FixedLocalFileList(CString strDirectory = "");
CString GetParentDirectory(CString strPath);
CString m_Local_File_Path;
其中:
FixedLocalFileList函数主要是用于文件夹跳转
GetParentDirectory函数主要是获取上级目录地址
m_Local_File_Path变量则是用于保存当前的文件夹路径。
2、我们来到LocalFileSysDlg.cpp文件下,在这里添加上面声明的函数的实现体。
CString CLocalFileSysDlg::GetParentDirectory(CString strPath)
{
CString strCurrentPath = strPath;
int iIndex = strCurrentPath.ReverseFind('\\'); // 查找最后一个'\\'
if (-1 == iIndex) {
return strCurrentPath;
}
CString strCurrentSubPath = strCurrentPath.Left(iIndex); // 取左边
iIndex = strCurrentSubPath.ReverseFind('\\');
if (-1 == iIndex) {
strCurrentPath = "";
return strCurrentPath;
}
strCurrentPath = strCurrentSubPath.Left(iIndex);
if (strCurrentPath.Right(1) != "\\")
strCurrentPath += "\\";
return strCurrentPath;
}
VOID CLocalFileSysDlg::FixedLocalFileList(CString strDirectory)
{
if (strDirectory.GetLength() == 0)
{
int iItem = m_local_list.GetSelectionMark(); // 获取选中的项
// 在list控件中选中
if (iItem != -1)
{
if (m_local_list.GetItemData(iItem) == 1)
{
strDirectory = m_local_list.GetItemText(iItem, 0); // 获取路径地址
}
}
}
// 每次打开一个文件夹时 第一行使用 ".."代表上级目录
// 双击".." 时返回上级目录
if (strDirectory == "..")
{
m_Local_File_Path = GetParentDirectory(m_Local_File_Path);
}
// 刷新当前目录
else if (strDirectory != ".")
{
m_Local_File_Path += strDirectory;
if (m_Local_File_Path.Right(1) != "\\")
{
m_Local_File_Path += "\\";
}
}
// 是驱动器的根目录,返回磁盘列表
if (m_Local_File_Path.GetLength() == 0)
{
InitLocalDiskDriverList(); // ..
return;
}
// 插入新的文件目录
m_local_list.DeleteAllItems();
while (m_local_list.DeleteColumn(0) != 0); // 删除
m_local_list.InsertColumn(0, "名称", LVCFMT_LEFT, 200);
m_local_list.InsertColumn(1, "大小", LVCFMT_LEFT, 100);
m_local_list.InsertColumn(2, "类型", LVCFMT_LEFT, 100);
m_local_list.InsertColumn(3, "修改日期", LVCFMT_LEFT, 115);
int iItemIndex = 0;
// InsertItem向list控件中插入列表项
m_local_list.SetItemData(m_local_list.InsertItem(iItemIndex++, "..",
GetLocalIconIndex(NULL, FILE_ATTRIBUTE_DIRECTORY)), 1);
// i 为 0 时列目录,i 为 1时列文件
for (int i = 0; i < 2; i++)
{
CFileFind FindFile;
BOOL bContinue;
bContinue = FindFile.FindFile(m_Local_File_Path + "*.*"); // 文件搜索
while (bContinue)
{
bContinue = FindFile.FindNextFile(); // 查找下一个
if (FindFile.IsDots()) // 是 '.'
continue;
BOOL bIsInsert = !FindFile.IsDirectory() == i; // 判断是否为目录
if (!bIsInsert)
continue;
int iItem = m_local_list.InsertItem(iItemIndex, FindFile.GetFileName(),
GetLocalIconIndex(FindFile.GetFileName(), GetFileAttributes(FindFile.GetFilePath())));
m_local_list.SetItemData(iItem, FindFile.IsDirectory());
// 获取文件/文件夹信息
SHFILEINFO sfi;
SHGetFileInfo(FindFile.GetFileName(), FILE_ATTRIBUTE_NORMAL, &sfi,sizeof(SHFILEINFO),
SHGFI_TYPENAME | SHGFI_USEFILEATTRIBUTES);
if (FindFile.IsDirectory()) {
m_local_list.SetItemText(iItem, 2, "文件夹");
}
else {
m_local_list.SetItemText(iItem, 2, sfi.szTypeName);
}
// 插入文件/文件夹的大小
CString strFileLength;
strFileLength.Format("%10d KB", (FindFile.GetLength() / 1024));
m_local_list.SetItemText(iItem, 1, strFileLength);
// 插入文件/文件夹的修改时间
CTime time;
FindFile.GetLastWriteTime(time);
m_local_list.SetItemText(iItem, 3, time.Format("%Y-%m-%d %H:%M"));
}
}
}
上面的代码有点多,不过我在这里已经做了大量注释了,相信不会太难看得懂的。
3、我们来到上节讲的双击函数中将MessageBox()函数注释掉,并添加FixedLocalFileList();,具体如下:
VOID CLocalFileSysDlg::OnNMDblclkListLocal(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
if (m_local_list.GetSelectedCount() == 0 ||
m_local_list.GetItemData(m_local_list.GetSelectionMark()) != 1)
return;
// MessageBox("双击了");
FixedLocalFileList();
*pResult = 0;
}
添加完上面代码我们此时既可以通过双击进入到下一级目录了,效果图如下:
到这里我们已经可以进入到文件夹内部了,如果后续还需要增加对文件夹/文件的增、删、重命名、打开等操作的话,可以给我留言,有时间的话我会继续在博客中完善这些功能的。