CListCtrl:如何保持滚动位置?

时间:2011-09-29 22:51:42

标签: mfc scroll clistctrl

我有一个CListCtrl(报告样式),我清除列表并在特定时间重新填充它。这样做时我想保持垂直滚动位置。我看到有几种方法看起来很有希望:

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

我正在尝试GetScrollPos()然后尝试SetScrollPos()但它似乎没有工作。保存滚动位置然后将其恢复的简单正确方法是什么?

更新

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

更新2

Scroll()方法似乎正确滚动滚动条内容。然而,它需要一个CSize对象作为它的参数。所以问题是如何在CSize和GetTopIndex或GetScrollInfo / Pos的输出之间进行转换。

2 个答案:

答案 0 :(得分:17)

我过去做过。 IIRC,诀窍包括:

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

答案 1 :(得分:0)

另一种方式是这样的:

CRect r;
m_lcList.GetItemRect(0, r, LVIR_BOUNDS);
int scrollPos = m_lcList.GetTopIndex() * r.Height();
RenewContents();
m_lcList.Scroll(CSize(0, scrollPos));
相关问题