|
项目开发用到了STreeview这个控件,不是特别明白,现在先占个位子。
- class CAdapterLocalFile :public STreeAdapterBase<LocalFileInfo> // 类 - 目录树适配器
- {
- public:
- void addTree(SStringT& strCurDir, HSTREEITEM root = STVI_ROOT, int deep = 10); // 子方法 - 增加目录树 最多伸展10次
- bool onSwitchClick(EventArgs *pEvt); // 事件 - 收缩 伸展 目录树
- SOUI::HTREEITEM FindItem(const SStringT & path,SOUI::HTREEITEM hRoot=STVI_ROOT) const;
- void clear();
- protected:
- virtual void getView(SOUI::HTREEITEM loc, SWindow * pItem, pugi::xml_node xmlTemplate); // 重载 - 获取视图
- };
复制代码- #include "stdafx.h"
- #include "pageLocalGallery.h"
- void CAdapterLocalFile::addTree(SStringT& strCurDir, HSTREEITEM root /* = STVI_ROOT */, int deep /* = 5*/)
- {
- SStringT strDir = strCurDir,strNextDir;
- strDir.Append(_T("\\*"));
- if(deep-- <= 0) return; // 搜索深度
- LocalFileInfo fi;
- WIN32_FIND_DATA fd;
- HANDLE hDir = ::FindFirstFile(strDir.GetBuffer(0), &fd);
- if (hDir == INVALID_HANDLE_VALUE) return ;
- if(root == STVI_ROOT){
- TCHAR* name = _tcsrchr(strCurDir.GetBuffer(0),_T('\\'));
- lstrcpy(fi.path, strCurDir.GetBuffer(0));
- lstrcpy(fi.name, name + 1);
- root = InsertItem(fi);
- }
- do{
- strNextDir = strCurDir;
- strNextDir.Append(_T("\"));
- if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
- SStringT strFileName = fd.cFileName;
- if (strFileName.Compare(_T(".")) == 0 || strFileName.Compare(_T("..")) == 0)
- continue;
- strNextDir.Append(fd.cFileName);
- lstrcpy(fi.name, fd.cFileName);
- lstrcpy(fi.path, strNextDir.GetBuffer(0));
- HSTREEITEM hRoot = InsertItem(fi, root);
- this->addTree(strNextDir, hRoot, deep);
- }
- } while (::FindNextFile(hDir, &fd));
- ::FindClose(hDir);
- }
- void CAdapterLocalFile::getView(SOUI::HTREEITEM loc, SWindow * pItem, pugi::xml_node xmlTemplate)
- {
- if (pItem->GetChildrenCount() == 0)
- pItem->InitFromXml(xmlTemplate);
-
- ItemInfo & ii = m_tree.GetItemRef((HSTREEITEM)loc);
- pItem->SetUserData((ULONG_PTR)&(ii.data));
- SWindow *text_red = pItem->FindChildByName(uiDirName);
- text_red->SetWindowText(ii.data.name);
- SToggle *pSwitch = pItem->FindChildByName2<SToggle>(uiDirSwitch);
- SASSERT(pSwitch);
- pSwitch->SetVisible(HasChildren(loc));
- pSwitch->SetToggle(IsItemExpanded(loc));
- pSwitch->GetEventSet()->subscribeEvent(EVT_CMD, Subscriber(&CAdapterLocalFile::onSwitchClick, this));
- }
- bool CAdapterLocalFile::onSwitchClick(EventArgs *pEvt)
- {
- SToggle *pToggle = sobj_cast<SToggle>(pEvt->sender);
- SItemPanel *pItem = sobj_cast<SItemPanel>(pToggle->GetRoot());
- SOUI::HTREEITEM loc = (SOUI::HTREEITEM)pItem->GetItemIndex();
- ExpandItem(loc, ITvAdapter::TVC_TOGGLE);
- return true;
- }
- SOUI::HTREEITEM CAdapterLocalFile::FindItem(const SStringT & path,SOUI::HTREEITEM hRoot) const
- {
- SOUI::HTREEITEM hItem = this->GetFirstChildItem(hRoot);
- while(hItem)
- {
- const LocalFileInfo & data= GetItemData(hItem);
- if(_tcscmp(path,data.path) == 0)
- {
- break;
- }
- if(this->HasChildren(hItem))
- {
- SOUI::HTREEITEM hFind = FindItem(path,hItem);
- if(hFind)
- {
- hItem = hFind;
- break;
- }
- }
- hItem = this->GetNextSiblingItem(hItem);
- }
- return hItem;
- }
- void CAdapterLocalFile::clear()
- {
- HSTREEITEM root = this->ITEM_ROOT;
- this->DeleteItem(root);
- }
复制代码
|
|