Advertisement

Populate on demand tree control help

Started by May 16, 2001 08:56 AM
-1 comments, last by Zeke 23 years, 8 months ago
Ive got a tree control and I am trying to add stuff to it only when the directories are expanded. So I catch TVN_EXPANDING, in OnItemExpanding() i call scanDir (which then inserts items) the problem is I need to pass in the path of the item just expanded as the root for scanDir to add its subdirectories to the tree control. This sounds confusing so let me try to clarify it with some code (I know this probably isnt the best way of doing all of this- any tips are welcome)
    
//Set root

void CLeftView::initTree(CTreeCtrl *tree)
{

	TCHAR curdir[_MAX_PATH];
	TCHAR path[_MAX_PATH];
	::GetCurrentDirectory(sizeof(curdir),curdir);
	::SetCurrentDirectory(_T("..\\.."));
	::GetCurrentDirectory(sizeof(path),path);

//	HTREEITEM 

	root = tree->InsertItem(path,0,0,TVI_ROOT);
//	scanDir(tree,root);

	RootExpanded();
	::SetCurrentDirectory(curdir);

}
//insert items into tree

void CLeftView::scanDir(CTreeCtrl *tree, HTREEITEM root)
{
	HTREEITEM dir=root;
	WIN32_FIND_DATA fd;
	CString ItemText, FileName;
	static CString ItemList;
	HANDLE h = ::FindFirstFile(_T("*.*"),&fd);
	while( FindNextFile(h,&fd) )
	{
		///////////////////////////

		if (!(ItemText.IsEmpty()))
		{
			if (strstr(ItemList,ItemText)==NULL)
			{
				ItemList+=ItemText;
				ItemList+=" ";
			}
		}
			ItemText.Format("%s",tree->GetItemText(dir));
		///////////////////////////////////

		FileName.Format("%s",fd.cFileName);
		if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && 
			fd.cFileName[0]!='.' ) &&
			(strstr(ItemList, FileName)==NULL) )    //TreeCtrl->GetItemText ( dir ) , fd.cFileName )!=0 ) )

		{
			TVINSERTSTRUCT InsStruct;
			TVINSERTSTRUCT* InsStructPtr=NULL;
			InsStruct.hParent=root;
			InsStruct.hInsertAfter=root;
			InsStruct.item.mask=TVIF_CHILDREN|TVIF_TEXT;
			InsStruct.item.cChildren=4;//TestChildren(FileName);

			InsStruct.item.pszText=fd.cFileName;
			InsStruct.item.cchTextMax=FileName.GetLength();

			InsStructPtr=&InsStruct

		dir = tree->InsertItem(InsStructPtr);
		//	dir = tree->InsertItem(fd.cFileName,0,0,root);

			ItemList+=FileName;
		
		//	::SetCurrentDirectory(fd.cFileName);

			scanDir(tree,root);
			::SetCurrentDirectory(_T(".."));
		}
	}
	::FindClose(h);
}
//Catch TVN_ITEMEXPANDING

void CLeftView::OnItemexpanding(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;

	scanDir(TreeCtrl,pNMTreeView->itemNew.hItem);
	*pResult = 0;
}
    
However because the root is still c:\ it just finds all the directories in C:\ not the directories in the subdirectory i am trying to expand. In OnItemExpanding I need to SetCurrentDirectory to the directory the user has just clicked on (e.g. if the user expands c:\program files, i need to set the current directory as c:\program files) Im sorry this post is so long but this problem is driving me crazy. If anyone can help i would be really really greatfull Edited by - Zeke on May 16, 2001 10:10:54 AM
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face

This topic is closed to new replies.

Advertisement