andong777
andong777

Reputation: 98

How to get file type of selected item in CTreeCtrl? The file name extensions may be hidden, so GetItemText is not helpful

I'm taking over an old project built with MFC. I'm not familiar with MFC at all, but I need to add a few new features to this project.

There is a tree list in the program, showing files on your computer. Double-click an item and the corresponding file will be imported into the project. At first, I used the following code to get the selected file name:

HTREEITEM hSelectItem = m_wndTree.GetSelectedItem();
CString filename = m_wndTree.GetItemText(hSelectItem);

But later, the method failed on others' computers, because they may check hide file name extensions so a file named "Arial.ttf" will return "Arial".

What is the best way for me to the full path of a selected item? inherit CTreeCtrl and override some functions may be a choice, but not familiar with MFC can cost me a lot of time and will get hands dirty. Are there any APIs or third-party libraries I can use?

Besides, The actual class I'm using is CMFCShellTreeCtrl, which has a public method GetItemPath, but there is a bug in it so I can't use this method to get the full path.

Thanks!

Upvotes: 0

Views: 643

Answers (2)

ScottMcP-MVP
ScottMcP-MVP

Reputation: 10415

The control has SetItemData and GetItemData methods that it inherits from CTreeCtrl. So when the control is being populated you can put the full file name or path into a heap-allocated string (CString) and save the address of the string with SetItemData. Then you can get it back when an item is clicked.

Upvotes: 1

Werner Henze
Werner Henze

Reputation: 16726

If you are talking about this bug, it was fixed back in 2010, so it should be resolved starting with MSVC 2012, I guess. If you need to work with MSVC 2010, then you might consider overriding the original CMFCShellTreeCtrl and replacing the buggy GetItemPath with the current implementation which you can easily take from a current compiler version.

Upvotes: 2

Related Questions