How to make a CTreeCtrl item centrally displayed?

2019-08-11 06:45发布

问题:

I know that the function EnsureVisible(HTREEITEM hItem) can show hItem by scrolling the bar of the CTreeCtrl control. But sometimes the hItem is displayed either beside the top or beside the bottom of the control, which is so unfriendly for users. I want the item to be shown in the middle of the control. Any good solutions?

回答1:

You would have to scroll the CTreeCtrl manually. Call CTreeCtrl::EnsureVisible() to make the item visible, then call CTreeCtrl::GetItemRect() to retrieve the item's current coordinates, and then call CWnd::ScrollWindow/Ex() specifying the necessary offset to put the item wherever you want it.