The treeview control does not provide an API for finding a label. You will have to manually move the elements and compare them with your row.
If your tree-like image is at a depth of more than one level, you will need to decide how to move around the objects (first, first in depth, or in width). If there are multiple items with the same label, these strategies can return different items.
An implementation might look something like this:
// Helper function to return the label of a treeview item std::wstring GetItemText( HWND hwndTV, HTREEITEM htItem ) { static const size_t maxLen = 128; WCHAR buffer[ maxLen + 1 ]; TVITEMW tvi = { 0 }; tvi.hItem = htItem; // Treeview item to query tvi.mask = TVIF_TEXT; // Request text only tvi.cchTextMax = maxLen; tvi.pszText = &buffer[ 0 ]; if ( TreeView_GetItem( hwndTV, &tvi ) ) { return std::wstring( tvi.pszText ); } else { return std::wstring(); } }
This is where the actual bypass occurs. The function is called recursively until more items are found or a match is found. This implementation uses a case-sensitive comparison ( wstring::operator==( const wstring& ) ). If you need another predicate, you will have to modify the implementation as you wish.
HTREEITEM FindItemDepthFirstImpl( HWND hwndTV, HTREEITEM htStart, const std::wstring& itemText ) { HTREEITEM htItemMatch = NULL; HTREEITEM htItemCurrent = htStart; // Iterate over items until there are no more items or we found a match while ( htItemCurrent != NULL && htItemMatch == NULL ) { if ( GetItemText( hwndTV, htItemCurrent ) == itemText ) { htItemMatch = htItemCurrent; } else { // Traverse into child items htItemMatch = FindItemDepthFirstImpl( hwndTV, TreeView_GetChild( hwndTV, htItemCurrent ), itemText ); } htItemCurrent = TreeView_GetNextSibling( hwndTV, htItemCurrent ); } return htItemMatch; }
The following function completes the recursion and passes the root element as a starting point. This is the function you call in your code. It will return HTREEITEM if it is found, NULL otherwise.
HTREEITEM FindItem( HWND hwndTV, const std::wstring& itemText ) { HTREEITEM htiRoot = TreeView_GetRoot( hwndTV ); return FindItemDepthFirstImpl( hwndTV, htiRoot, itemText ); }