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();
}
To answer your immediate question, you can turn off window refreshing while the control is being populated, using
CWnd::LockWindowUpdate()
andCWnd::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.