CListCtrl:如何保持滚动位置? [英] CListCtrl: How to maintain scroll position?

查看:206
本文介绍了CListCtrl:如何保持滚动位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CListCtrl(报告样式),在其中清除列表并在特定时间重新填充它.在执行此操作时,我想保持垂直滚动位置.我发现有两种方法看起来很有希望:

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()

我正在尝试GetScrollPos(),然后是SetScrollPos(),但它似乎无法正常工作.保存滚动位置然后再恢复滚动位置的简单正确方法是什么?

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?

更新

实际上,我似乎可以保存滚动位置GetScrollPos(),然后保存SetScrollPos()来恢复它,但是实际上它似乎只是设置了滚动条的位置,实际上并没有滚动CListCtrl的项目.

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.

更新2

Scroll()方法似乎可以正确滚动滚动条.但是,它将CSize对象作为参数.因此,问题在于如何在CSize和GetTopIndex或GetScrollInfo/Pos的输出之间进行转换.

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.

推荐答案

我以前已经做到了. IIRC,诀窍在于:

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

这篇关于CListCtrl:如何保持滚动位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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