CMFCShellTreeCtrl Setting Initial Folder

wdolson 1 Reputation point
2023-03-28T04:00:46.59+00:00

I'm trying to make use of CMFCShellTreeCtrl in an MFC project. The documentation is very thin. I would think SelectPath() would set the control to the given path. However, I can't get it to set. I give it a valid path in OnInitDialog() but the shell comes up with nothing selected. After calling SelectPath() I call GetItemPath with thehtreeItem argument set to NULL and it gives me the root path of the tree.

So it appears SelectPath() does not actually select anything in the tree. How do I programatically make a selection in a CMFCShellTreeCtrl control?


C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,688 questions
{count} votes

4 answers

Sort by: Most helpful
  1. RLWA32 44,951 Reputation points
    2023-03-28T08:49:01.4166667+00:00

    This worked for me in OnInitDialog -

        // TODO: Add extra initialization here
        m_Tree.SelectPath(_T("C:\\Users\\RLWA32\\Documents"));
        CString strSelected;
        m_Tree.GetItemPath(strSelected, m_Tree.GetSelectedItem());
        m_Edit.SetWindowText(strSelected);
    

    Result -

    SelectPath

    0 comments No comments

  2. wdolson 1 Reputation point
    2023-03-29T03:35:31.2866667+00:00

    Duplicate posted

    0 comments No comments

  3. wdolson 1 Reputation point
    2023-03-29T03:46:11.5033333+00:00

    My first attempt took a while to show so I thought it disappeared...

    I think I found something. I am using a class derived from CMFCShellTreeCtrl described here

    https://www.codeproject.com/Articles/1073213/Extending-MFC-shell-controls-functionality

    I went back to the sample and tried selecting a directory that was under the new root. I tried both a relative path and an absolute path. SelectPath couldn't find it for some reason.

    I guess I'll need to write my own version of SelectPath.


  4. wdolson 1 Reputation point
    2023-03-30T05:12:57.6366667+00:00

    I did get it to work writing my own SelectPath(). I started with the functions in the Microsoft code. They do it a bit inefficiently. They parse the pidls at each level to get the display name and compare it to the pidl for that level at the given path. Instead of calling SHGetFileInfo once for each level for the path given and then compare to the results of the call for each pidl of the directory, it makes redundant calls to that function each time it checks a level.

    Parsing directories can take a lot of time. You want to eliminate any redundancies if you can.

    I'm going down this road because the old tree I was using could take up to a couple of minutes to parse a complete drive tree and we know everything will always be below a set data path given to the program. My customer wanted to limit the tree to just the contents below the given data path. ie set the root to the data path in the program.

    Anyway I was able to get programatically selecting the path to work by limiting the search to just below the data path given. To debug the given path converted to pidl and then parsed back, I captured all the strings for each level in an array to investigate. I found the parsed pidl path was in this format:

    "Computer C:\foo\foosub1\foosub2\etc"

    "Computer" was an extra layer not in the given path.

    I'd select my post above as the solution, but I can't select one of my own posts as the solution.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.