以下代码是将指定目录或文件 lpcszResPath 添加到指定的 pParentItem 树节点下。(如果 lpcszResPath 是一目录,则递归其所有子目录及文件)
代码如下:
void CLocalResourceView::Add(HTREEITEM pParentItem /* = NULL */, const char* lpcszResPath /* = NULL */)
{
if (!::PathFileExists((LPCTSTR)lpcszResPath))
return ;
char szName[MAX_PATH] = { '\0' };
GLOBALFUNCTION::GetNameByPath(lpcszResPath, szName);
HTREEITEM hItem = NULL;
if (PathIsDirectory((LPCSTR)lpcszResPath))
hItem = m_wndLocalResTree.InsertItem(szName, 0, 0, pParentItem, NULL);
else
hItem = m_wndLocalResTree.InsertItem(szName, 2, 2, pParentItem, NULL);
if (PathIsDirectory((LPCSTR)lpcszResPath))
{
//* 末尾添加上斜杠
GLOBALFUNCTION::IncludeDirTailSlash(lpcszResPath, szName);
string strPath(szName);
string strTemp("");
HANDLE hSearch;
WIN32_FIND_DATA data;
strTemp = strPath + "*.*";
hSearch = FindFirstFile(strTemp.c_str(), &data);
if (INVALID_HANDLE_VALUE != hSearch)
{
do
{
if (!strcmp(data.cFileName, ".") || !strcmp(data.cFileName, ".."))
continue;
strTemp = strPath + data.cFileName;
this->Add(hItem, strTemp.c_str());
} while (FindNextFile(hSearch, &data));
FindClose(hSearch);
}
}
}