折叠树视图中的所有节点,最后一个扩展节点除外 [英] Collapse all nodes in treeview, except the last expanded one

查看:75
本文介绍了折叠树视图中的所有节点,最后一个扩展节点除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

介绍及相关信息:



我需要实施以下方案:



1.用户扩展节点,例如节点1 ;



2.用户扩展另一个节点,例如节点2 ;



3.折叠上一个节点(节点1 );



为了直观地解释我的意思,我们将使用以下示例图片 [ ^ ]。



现在当用户点击 Assembly 1 节点或其子节点组件时,我需要折叠每个其他节点。我的意思的例子在这里 [ ^ ]。



我努力实现这一行为:



通过互联网浏览,稍微考虑一下我自己,我能够编写帮助函数折叠节点及其子节点:



  void  CollapseNode(HWND hTree,HTREEITEM hti)
{
< span class =code-keyword> if (TreeView_GetChild(hTree,hti)!= NULL)
{
TreeView_Expand(hTree,hti,TVE_COLLAPSE);
hti = TreeView_GetChild(hTree,hti);
do
{
CollapseNode(hTree,hti);
}
while ((hti = TreeView_GetNextSibling(hTree,hti))!= NULL);
}
}





通过 MSDN 文档阅读我发现 TVN_ITEMEXPANDING TVN_ITEMEXPANDED 可能有用的消息。



我也有发现 NM_CLICK 通知似乎很有趣,因为我可以使用 TVM_HITTEST 消息来测试点击是否在 +/- 按钮。



我还找到 TVN_KEYDOWN 消息,这将帮助我在用户按下时进行扩展左箭头右箭头键。



问题:



我想不出使用上述消息来解决我的任务的算法。



问题:



你能否建议我处理上述消息的算法,以便我可以打电话我的 CollapseNode(..)功能?



这样的东西:



Hittest in NM_CLICK 然后调用您的函数将最后一个展开的项目存储在变量中并折叠它以响应 TVN_ITEMEXPANDED 一开始会很好。



谢谢。

解决方案

< blockquote>

很久以前使用Tree控件所以我不知道我要告诉你的是什么,已经实现了一些方法。这也许不是最好的方法。但它可能运行良好。

在树遍历中,路径的唯一性非常重要。

实现OnTvnSelchangedTree事件。

在此事件中获取用户选择项目的句柄。

现在使用句柄,您可以获取用户点击项目的文本。启动一个字符串向量并将该字符串存储在该向量中(您将在此处创建唯一的路径,以后会派上用场)。

同时使用句柄获取此选定项父句的句柄以及何时你有父母的句柄,在父文件上获取文本。在字符串向量中存储它。

继续这样做直到你到达树的根。所以,最后你将有一个向量它具有从根到用户选择的节点的唯一路径。

例如,向量将具有< test assembly =component =3 =>如果用户选择了组件3,则为唯一路径。



现在调用折叠函数折叠整个树。

实现一个函数来扩展树节点。(你实现了一个崩溃,所以这对你来说应该不是很麻烦;))

调用你的扩展函数。

对崩溃和扩展函数的调用将来自OnTvnSelchangedTree事件本身。

现在在每个节点的扩展函数中获取节点上的文本并检查它对你所拥有的字符串向量。也就是说,展开树的根。

对应于根的字符串是向量的第一个元素。现在获取root的所有子节点,但只调用与向量中第二个元素具有相同文本的扩展函数。然后得到它的孩子,只展开与向量的第三个元素具有相同文本的那个...依此类推。

我希望你能得到全局。基本上保持路径是唯一关键的部分。



我不确定是否要保持唯一路径,而不是将文本存储在项目上,我们可以只存储项目本身的句柄。你可以尝试一下。

使用MSDN和谷歌。已经提供了GetSelectedItem,GetItemText和GetParentItem等。

祝你好运......请让我知道它是怎么回事;)


INTRODUCTION AND RELEVANT INFORMATION:

I need to implement the following scenario:

1. User expands a node, e.g. Node 1;

2. User expands another node, e.g. Node 2;

3. Collapse previous node ( Node 1 );

To visually explain what I mean, we shall use the following example image[^].

Now when user clicks on Assembly 1 node or its child node Components, I need to collapse every other node. Example of what I mean is illustrated here[^].

MY EFFORTS TO IMPLEMENT THIS BEHAVIOR:

Browsing through Internet, and with a little thinking of my own, I was able to write helper function that collapses node and its children :

void CollapseNode( HWND hTree, HTREEITEM hti )
{
    if( TreeView_GetChild( hTree, hti ) != NULL )
    {
        TreeView_Expand( hTree, hti, TVE_COLLAPSE );
        hti = TreeView_GetChild( hTree, hti );
        do
        {
            CollapseNode( hTree, hti );
        }
        while( ( hti = TreeView_GetNextSibling( hTree, hti ) ) != NULL  );
    }
}



Reading through MSDN documentation I found TVN_ITEMEXPANDING and TVN_ITEMEXPANDED messages that might be useful.

I have also found NM_CLICK notification that seems interesting since I can use TVM_HITTEST message to test if click was on +/- button.

Also I have found TVN_KEYDOWN message that will help me with expanding when user presses left arrow or right arrow keys.

PROBLEM:

I can not think of an algorithm for using the above messages to solve my task.

QUESTION:

Can you please suggest me an algorithm for handling the above messages so I can call my CollapseNode(..) function ?

Something like this:

Hittest in NM_CLICK and then call your function or Store the last expanded item in a variable and collapse it in response to TVN_ITEMEXPANDED would be good for a start.

Thank you.

解决方案

hi,
Used Tree control long long ago so i don't know if what i am going to tell you is already implemented some how. Also this might not be the best approach. But it might work well.
In tree traversal the uniquness of the path is important.
Implement a OnTvnSelchangedTree event.
In this event get a handle to the item selected by user .
Now using the handle, you can get the text of the item user clicked on . Start a vector of strings and store this string in that vector(You are making the unique path here which will come in handy later).
Also using the handle get a handle to this selected items parent and when you have the handle to the parent, get the text on parent.Store this in the vector of strings .
Keep doing this until you reach the root of the tree .So, finally you will have a vector which has the unique path right from root up to the node the user selected.
For example the vector will have <test assembly="" component="" 3=""> as unique path if user selected Component 3.

Now call your collapse function to collapse the whole tree.
Implement a function to expand the tree nodes.(You implemented one for collapse so this should not be much trouble for you ;))
Call your expand function.
The call to both collapse and expand functions will be from inside the OnTvnSelchangedTree event itself.
Now in this expand function at each node get the text on the node and check it against the vector of strings that you have. That is to say, expand the root of the tree.
the string corresponding to the root was first element of the vector. Now get all children of the root but call the expand function only for the one which has the same text as the second element in your vector. Then get its children and expand only the one which has the same text as the third element of vector ... and so on.
I hope you do get the big picture. Basically maintaining the path is the only crucial part.

I am not sure if for maintaining the unique path, instead of storing the text on the items, we could just store the handles to the items themselves. You can give it a try.
Make use of MSDN and Google. GetSelectedItem, GetItemText and GetParentItem etc are already provided.
Good luck ...and please let me know how it goes ;)


这篇关于折叠树视图中的所有节点,最后一个扩展节点除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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