我在http://www.vckbase.com/document/viewdoc/?id=836下载了一个控件演示的程序,从留言中得知当树控件触发OnItemexpandedTree事件,为其子项添加下一级目录时,会多次为其子项添加下一级目录,自己下载后实验的时候确实是这样,请问各位怎么样才能让其只添加一次?有人说先判断是否添加过,然后在添加或不加.我想是不是可以将添加的删除掉,然后再添加??可惜我刚学,做不到.大家能否给出解决问题的详细代码,谢谢了.
主要代码如下:
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//函数功能:展开事件函数
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void CTreeViewDlg::OnItemexpandedTree(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
TVITEM item = pNMTreeView->itemNew;
if(item.hItem == m_hRoot)
return;
HTREEITEM hChild = m_tree.GetChildItem(item.hItem);
while(hChild)
{
AddSubDir(hChild);
hChild = m_tree.GetNextItem(hChild,TVGN_NEXT);
}
*pResult = 0;
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//函数功能:获取树项目全跟径
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
CString CTreeViewDlg::GetFullPath(HTREEITEM hCurrent)
{
CString strTemp;
CString strReturn = "";
while(hCurrent != m_hRoot)
{
strTemp = m_tree.GetItemText(hCurrent);
if(strTemp.Right(1) != "\\")
strTemp += "\\";
strReturn = strTemp + strReturn;
hCurrent = m_tree.GetParentItem(hCurrent);
}
return strReturn;
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//添加子目录
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void CTreeViewDlg::AddSubDir(HTREEITEM hParent)
{
CString strPath = GetFullPath(hParent);
if(strPath.Right(1) != "\\")
strPath += "\\";
strPath += "*.*";
CFileFind file;
BOOL bContinue = file.FindFile(strPath);
while(bContinue)
{
m_tree.DeleteItem(hParent);
bContinue = file.FindNextFile();
if(file.IsDirectory() && !file.IsDots())
m_tree.InsertItem(file.GetFileName(),hParent);
}
}
主要代码如下:
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//函数功能:展开事件函数
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void CTreeViewDlg::OnItemexpandedTree(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
TVITEM item = pNMTreeView->itemNew;
if(item.hItem == m_hRoot)
return;
HTREEITEM hChild = m_tree.GetChildItem(item.hItem);
while(hChild)
{
AddSubDir(hChild);
hChild = m_tree.GetNextItem(hChild,TVGN_NEXT);
}
*pResult = 0;
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//函数功能:获取树项目全跟径
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
CString CTreeViewDlg::GetFullPath(HTREEITEM hCurrent)
{
CString strTemp;
CString strReturn = "";
while(hCurrent != m_hRoot)
{
strTemp = m_tree.GetItemText(hCurrent);
if(strTemp.Right(1) != "\\")
strTemp += "\\";
strReturn = strTemp + strReturn;
hCurrent = m_tree.GetParentItem(hCurrent);
}
return strReturn;
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//添加子目录
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void CTreeViewDlg::AddSubDir(HTREEITEM hParent)
{
CString strPath = GetFullPath(hParent);
if(strPath.Right(1) != "\\")
strPath += "\\";
strPath += "*.*";
CFileFind file;
BOOL bContinue = file.FindFile(strPath);
while(bContinue)
{
m_tree.DeleteItem(hParent);
bContinue = file.FindNextFile();
if(file.IsDirectory() && !file.IsDots())
m_tree.InsertItem(file.GetFileName(),hParent);
}
}
解决方案 »
- 有两个按钮,如何实现按下一个按钮后该按钮颜色显示按下,而之前的那一个按钮颜色还原,求教。
- 求各位朋友指导下入门小程序?
- 工具条重写MouseHover//rc.right=rc.left+8;是什么作用呢?
- socket收发数据问题?
- vc6.0 到vs2005 为什么出错
- UNICODE工程下DeleteUrlCacheEntryW报错?
- 内存溢出问题
- 怎么才能让ActiveMovie Window不显示
- 基于对话框的工程,里面有文档处理,如何写注册表将文档和我的程序关联?
- 请问怎样把两个图片文件合并成一个图片文件呢?
- 请教大家一个问题:如何获得具有统一格式的int 变量
- 为什么ExecWB函数调用失败!没有像预期那样出现打印对话框?
char tvinamenew[MAX_PATH];
pNew=&(pNMTreeView->itemNew);
if(pNew && pNew->hItem)
{
UINT ret=LeftTree.GetItemState(pNew->hItem,TVIS_EXPANDEDONCE);
if(ret & TVIS_EXPANDEDONCE)
{
return;// already
}....