CodePudding user response:
The file path and then to control the displayCodePudding user response:
//DirTreeCtrl. CPP: implementation file//
# include "stdafx. H"
# include "DirTreeCtrl. H"
# include "shlobj. H"
//CDirTreeCtrl
IMPLEMENT_DYNAMIC (CDirTreeCtrl CTreeCtrl)
CDirTreeCtrl: : CDirTreeCtrl ()
{
}
CDirTreeCtrl: : ~ CDirTreeCtrl ()
{
}
BEGIN_MESSAGE_MAP (CDirTreeCtrl CTreeCtrl)
ON_NOTIFY_REFLECT (TVN_ITEMEXPANDED OnTvnItemexpanded)
ON_WM_CREATE ()
END_MESSAGE_MAP ()
//CDirTreeCtrl message handler
Void CDirTreeCtrl: : OnTvnItemexpanded (NMHDR * pNMHDR, LRESULT * pResult)
{
LPNMTREEVIEW pNMTreeView=reinterpret_cast & lt; LPNMTREEVIEW> (pNMHDR);
//TODO: add the control notification handler code
TV_ITEM itm=pNMTreeView - & gt; ItemNew;
If (itm) state & amp; TVIS_EXPANDED)
ExpandBranch (itm) hItem);
The else
DeleteAllChild (itm) hItem);
* pResult=0;
}
Int CDirTreeCtrl: : OnCreate (LPCREATESTRUCT LPCREATESTRUCT)
{
If (CTreeCtrl: : OnCreate (lpCreateStruct)==1)
return -1;
//TODO: here to add your special creation code
The Initialize ();
return 0;
}
Void CDirTreeCtrl: : Initialize (bool clear)
{
LPITEMIDLIST itemList.
TCHAR buf [60].
Int desktopIcon;
Int mycomputerIcon;
Int ret.
If (clear)
DeleteAllItems ();
SetImageList (NULL, 0);
SHGetSpecialFolderLocation (this - & gt; M_hWnd, CSIDL_DESKTOP, & amp; ItemList);
SHGetPathFromIDList (itemList, buf);
M_desktop_path=buf;
M_img. Create (GetSystemMetrics (SM_CXSMICON), GetSystemMetrics (SM_CYSMICON), ILC_COLOR24, 50, 50);
M_img. SetBkColor (GetSysColor (COLOR_WINDOW));
CBitmap BMP.
CBitmap * old;
CBitmap * newbmp;
The CDC dc;
Dc. CreateCompatibleDC (NULL);
BMP. CreateBitmap (32,32,1,24, NULL);
Old=dc. SelectObject (& amp; BMP);
Dc. FillSolidRect (0,0,31,31, GetSysColor (COLOR_WINDOW));
Newbmp=dc. SelectObject (old);
Dc. DeleteDC ();
Ret=m_img. Add (newbmp, a COLORREF (0 x00));
DesktopIcon=m_img. Add (ExtractIcon (AfxGetApp () - & gt; M_hInstance, shell32. DLL, 34));
MycomputerIcon=m_img. Add (ExtractIcon (AfxGetApp () - & gt; M_hInstance, "shell32. DLL", 15));
M_drives_root=AddItem_Core (_T (" my computer "), TVI_ROOT, true,
MycomputerIcon mycomputerIcon);
M_desktop_root=AddItem (m_desktop_path TVI_ROOT, true, desktopIcon, desktopIcon);
}
HTREEITEM CDirTreeCtrl: : AddItem (const TCHAR * path, HTREEITEM parent,
Bool isDir, int iconEx, int iconEx2)
{
SHFILEINFO shinfo shinfo_sel;
SHGetFileInfo (path, NULL,
& Shinfo,
Sizeof (shinfo),
SHGFI_DISPLAYNAME | SHGFI_ICON | SHGFI_SMALLICON);
SHGetFileInfo (path, NULL,
& Shinfo_sel,
Sizeof (shinfo_sel),
SHGFI_DISPLAYNAME | SHGFI_ICON | SHGFI_OPENICON | SHGFI_SMALLICON);
Int icon=iconEx!=1? IconEx: m_img. Add (shinfo. HIcon);
Int iconsel=iconEx2!=1? IconEx2: m_img. Add (shinfo_sel. HIcon);
SetImageList (& amp; M_img LVSIL_NORMAL);
Return AddItem_Core (shinfo szDisplayName, parent, isDir, icon, iconsel);
}
Void CDirTreeCtrl: : DeleteAllChild (HTREEITEM itm, Boolean isEmpty)
{
HTREEITEM child;
The child=GetChildItem (itm);
While (child)
{
Int img1, img2;
If (GetChildItem (child))
DeleteAllChild (GetChildItem (child), isEmpty);
GetItemImage (child, img1, img2);
If (img2!=0 & amp; & Img2!=img1)
{
M_img. Remove (img2);
RefreshTreeImages (m_drives_root, img2);
RefreshTreeImages (m_desktop_root, img2);
}
If (img1!=0)
{
M_img. Remove (img1);
RefreshTreeImages (m_drives_root img1);
RefreshTreeImages (m_desktop_root img1);
}
Called DeleteItem (child);
SetImageList (& amp; M_img TVSIL_NORMAL);
The child=GetChildItem (itm);
}
If (isEmpty) InsertItem (_T (" "), itm);
}
Void CDirTreeCtrl: : ExpandBranch (HTREEITEM parent)
{
Bool ipads=false;
If (the parent==m_drives_root)
{
TCHAR * PTR.
DeleteAllChild (parent, false);
GetLogicalDriveStrings (255, m_drives_name);
PTR=m_drives_name;
While (* PTR)
{
Ipads=true;
AddItem (PTR, m_drives_root, true);
PTR +=_tcslen (PTR) + 1;
}
}
The else
{
Cstrings fullpath pathWildcard;
Fullpath=GetPathFromHere (fullpath, parent);
DeleteAllChild (parent, false);
PathWildcard=fullpath + _T (" *. * ");
CFileFind finder.
BOOL bWorking=finder. FindFile (pathWildcard);
While (bWorking)
{
BWorking=finder. The FindNextFile ();
If (finder. IsDirectory () & amp; & ! Finder. IsDots ())
{
Ipads=true;
HTREEITEM itm=AddItem (fullpath + finder. GetFileName (),
The parent);
InsertItem (_T (" "), itm);
}
}
BWorking=finder. FindFile (pathWildcard);
While (bWorking)
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull