CListCtrl: How to maintain scroll position?

旧街凉风 提交于 2019-12-19 05:33:13

问题


I have a CListCtrl (report style) where I clear the list and repopulate it at certain times. I'd like to maintain the vertical scroll position when doing this. I see there are a couple methods that look promising:

EnsureVisible()
GetScrollPos()
SetScrollPos()
GetScrollInfo()
GetTopIndex()
Scroll()

I'm trying GetScrollPos() and then SetScrollPos() but it doesn't appear to be working. What is the simple correct way to save a scroll position and then later restore it?

UPDATE

Actually it seems I can get to save the scroll position GetScrollPos() and then SetScrollPos() to restore it, however it literally just seems to set the scroll bar position and does not actually scroll the items of my CListCtrl.

UPDATE 2

The Scroll() method seems to correctly scroll the scrollbars and the contents. However it takes a CSize object as it's argument. So the question would be how to translate between the CSize and the output of either GetTopIndex or GetScrollInfo/Pos.


回答1:


I've done that in the past. IIRC, the trick consisted in:

int topIndex= m_List.GetTopIndex();
RenewContents();
m_List.EnsureVisible(m_List.GetItemCount() - 1); // Scroll down to the bottom
m_List.EnsureVisible(topIndex);// scroll back up just enough to show said item on top


来源:https://stackoverflow.com/questions/7604267/clistctrl-how-to-maintain-scroll-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!