如何找到标签等于字符串变量的树视图节点? [英] How can i find treeview node by which label equals string variable?

查看:24
本文介绍了如何找到标签等于字符串变量的树视图节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想首先感谢所有花一些时间查看此主题并尝试提供帮助的人.

I would like to start by saying thanks to everyone who takes some time to view this thread and try to help.

我在网上搜索过,没有找到选择标签文本与字符串变量文本相同的树视图节点的示例.

I have searched the Internet, and couldn't find an example of selecting tree view node that has label text as same as text of a string variable.

在 MSDN 上我找到了 TVM_GETISEARCHSTRING 消息,但我不知道它是否可以用来解决我的问题.即使可以,我还是不知道怎么用.

On MSDN I have found message TVM_GETISEARCHSTRING but I don't know if it can be used to solve my problem. Even if it can, I still don't know how to use it.

我有一个字符串变量,用于保存数据库中的文本.当程序加载时,树视图应该有一个选择了相同文本的节点.

I have a string variable that holds text from database. When program loads, tree view should have a node with the same text selected.

请帮忙提供一些说明或代码片段,因为我什至不知道如何开始编码.

Please help with some instructions or code snippets, since I have no clue how to even start coding this.

我在 MS Visual Studio Express 2008、Windows XP、C++ 中工作,使用纯 WIN32 API.

I work in MS Visual Studio Express 2008, on Windows XP, in C++, using pure WIN32 API.

仅此而已,再次感谢所有试图提供帮助的人.非常感谢!

That would be all, again I thank everyone who tries to help.Thank you very very much!

两个答案都适合我,但我不知道如何标记它们,在这个网站上似乎只能接受一个答案.

Both answers are good for me, but I don't know how to mark them both, it seems that on this site only one answer can be accepted.

我不能忽视他们两人为帮助我所做的所有工作,所以我写这篇文章是为了至少通过正式声明他的解决方案对我来说也是可以接受的来回报乔纳森,这是只是 Tim 的解决方案更适合我的编码风格.我也会赞成这两个答案.

I couldn't have just neglected all the work both of them invested to help me, so I write this in order to try to repay to the Jonathan at least by officially stating that his solution is acceptable for me too, it is just that Tim's solution suits my coding style better.I will also upvote both answers.

推荐答案

treeview 控件不提供用于搜索标签的 API.您必须手动遍历这些项目并将它们与您的字符串进行比较.

The treeview control does not provide an API to search for a label. You will have to manually traverse the items and compare them to your string.

如果您的树视图深度超过一层,您将不得不决定如何遍历项目(深度优先或广度优先).如果有多个具有相同标签的项目,这些策略可能会返回不同的项目.

If your treeview is more than one level deep you will have to decide how to traverse the items (either depth first or breadth first). In case there are multiple items with the same label these strategies may 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();
    }
}

这是实际遍历发生的地方.递归调用该函数,直到无法搜索更多项目或找到匹配项.此实现使用区分大小写的比较 (wstring::operator==( const wstring& )).如果您需要不同的谓词,则必须根据需要修改实现.

This is where the actual traversal takes place. The function is called recursively until no more items can be searched or a match has been found. This implementation uses a case-sensitive comparison (wstring::operator==( const wstring& )). If you need a different predicate you will have to modify the implementation as you see fit.

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;
}

以下函数将递归包装起来,并以根元素为起点.这是您将在代码中调用的函数.如果找到,它将返回 HTREEITEM,否则返回 NULL.

The following function wraps the recursion and passes the root element as the starting point. This is the function you would call in your code. It will return an HTREEITEM if one is found, NULL otherwise.

HTREEITEM FindItem( HWND hwndTV, const std::wstring& itemText )
{
    HTREEITEM htiRoot = TreeView_GetRoot( hwndTV );
    return FindItemDepthFirstImpl( hwndTV, htiRoot, itemText );
}

这篇关于如何找到标签等于字符串变量的树视图节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆