效果如图:
上位机MFC实现树形菜单资源管理器源代码
关键代码为
- void CDirTreeView::AddNode(const char * path, HTREEITEM node, int type, int mode)
- {
- WIN32_FIND_DATA fd;
- HANDLE hFind;
- char buff [_MAX_PATH]; // temporary storage
- HTREEITEM newNode = node; // May be used to build pathname without
- int image1, image2; // adding any new nodes.
- CString dirPath;
- // If a node name was passed to us then we will add the node.
- // Otherwise, we need to get a directory name first and then recurse.
- //
- if (path)
- {
- // if the mode indicates that we are adding a drive node
- // then we need to:
- // add the drive label in parens.
- // reset the mode to tmShort (actually, on fast machines this
- // doesn't really speed things up).
- if (type >= DRIVE_REMOVABLE)
- {
- char VolName[24];
- char RootName[10];
- DWORD dwCompLen, dwFlags;
- memset (&fd, '\0', sizeof (WIN32_FIND_DATA));
- fd.dwFileAttributes = FILE_ATTRIBUTE_SYSTEM;
- sprintf (RootName, "%s\", path);
- memset (VolName, '\0', sizeof (VolName));
- strcpy (fd.cFileName, RootName);
- GetVolumeInformation (RootName, VolName, 24, NULL, &dwCompLen, &dwFlags, NULL, 0);
- if (strlen (VolName))
- {
- wsprintf(buff, "%s (%s)", path, VolName);
- }
- else
- wsprintf (buff, "%s", path);
- image1 = image2 = GetIconIndex (fd);
- newNode = InsertChild (node, buff, image1, image2, TVI_LAST);
- mode = CDirTreeView::tmShort;
- }
- // Otherwise just use the node name passed
- else
- {
- strcpy(buff, path);
- // Add the node as a child to the current node using the folder images.
- // Use the sort flag to sort the tree as we go.so the list is sorted
- // as we go.
- memset (&fd, '\0', sizeof (WIN32_FIND_DATA));
- strcpy (fd.cFileName, buff);
- fd.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
- image1 = GetIconIndex (fd);
- fd.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY | SHGFI_OPENICON;
- image2 = GetIconIndex (fd);
- newNode = InsertChild (node, buff, image1, image2, TVI_SORT);
- }
- }
- // Build a path name based on the node.
- BuildPath(dirPath, newNode);
- // Add wildcards
- dirPath += "*.*";
- //
- // Look for the first match. Return if none.
- if ((hFind = FindFirstFile(dirPath, &fd)) == INVALID_HANDLE_VALUE)
- {
- return;
- }
- // add one to the level we are on
- m_nLevel++;
- do
- {
- if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
- && (!(fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)))
- {
- if (!strcmp (fd.cFileName, "."))
- {
- ; // Current directory. Do nothing.
- }
- else if (!strcmp (fd.cFileName, ".."))
- {
- ; // Parent directory. Do nothing.
- }
- else
- {
- // If the are building the intial tree structure (tmShort) or we are
- // expanding a level (tmDetail), add the node by recursion.
- if (((mode == CDirTreeView::tmShort)
- && (m_nLevel < 2))
- || (mode == CDirTreeView::tmDetail))
- AddNode(fd.cFileName, newNode, 0, mode); // type, 0); // mode);
- // If we're building the initial structure, we want to add only one
- // subnode to make the plus symbold appear.
- if (mode == CDirTreeView::tmShort)
- break;
- // In the detail mode we need to fill this branch completely
- // but only one sub-node per node under this branch. Again,
- // we have to do this so the + sign shows up next to the node.
- if (mode == CDirTreeView::tmDetail && m_nLevel > 1)
- {
- break;
- }
- }
- }
- } while (::FindNextFile (hFind, &fd)); // Look for the next match
- FindClose (hFind);
- // decrement the level counter
- m_nLevel--;
- }
复制代码
|