CTreeCtrl Expand without scrolling

2019-09-15 01:09发布

In my CTreeCtrl, when I expand a node which is not visible the control automatically repositions the tree to make the expanded node visible. Is there any way to prevent this behaviour?

Use case: I have a tree which is loading items asynchronously from a remote source and may be building the 'bottom' of the tree for quite some time, and currently the tree behaviour of jumping to each node as it is completed is very distracting to the user.

Current workaround:

/******************************************************************************
 Expand an item while retaining the tree position
 ******************************************************************************/
void CFileOpenTreeView::ExpandWithoutJumping(HTREEITEM hItem)
{
    // This still flickers for some reason, but at least it doesn't jump
    LockWindowUpdate();
    HTREEITEM hFirstVisible = GetTreeCtrl().GetFirstVisibleItem();

    GetTreeCtrl().Expand(hItem, TVE_EXPAND);

    GetTreeCtrl().SelectSetFirstVisible(hFirstVisible);
    UnlockWindowUpdate();
    Invalidate();
}

标签: mfc
1条回答
我命由我不由天
2楼-- · 2019-09-15 01:27

To answer your immediate question, you can turn off window refreshing while the control is being populated, using CWnd::LockWindowUpdate() and CWnd::UnlockWindowUpdate().

In general, though, if you can, you may want to rethink your strategy. For example, you can populate sub-nodes in the tree without expanding the parent nodes.

Also, in the use case you've described, you may want to look into dynamically populating the children of a node only when the parent node is expanded by the user.

查看更多
登录 后发表回答