10

我有一个 CListCtrl (报告样式),我在其中清除列表并在某些时候重新填充它。我想在执行此操作时保持垂直滚动位置。我看到有几种方法看起来很有希望:

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

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

更新

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

更新 2

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

4

2 回答 2

19

我过去曾这样做过。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
于 2011-09-30T14:44:58.687 回答
0

另一种方法是这样的:

CRect r;
m_lcList.GetItemRect(0, r, LVIR_BOUNDS);
int scrollPos = m_lcList.GetTopIndex() * r.Height();
RenewContents();
m_lcList.Scroll(CSize(0, scrollPos));
于 2020-03-26T03:36:21.207 回答