【问题标题】:CListCtrl: How to maintain scroll position?CListCtrl:如何保持滚动位置?
【发布时间】:2011-11-28 02:09:30
【问题描述】:

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

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

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

更新

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

更新 2

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

【问题讨论】:

    标签: mfc scroll clistctrl


    【解决方案1】:

    我过去也这样做过。 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
    

    【讨论】:

    • 这很好。我更喜欢使用 Scroll() 的解决方案,但这可以完成工作。你知道如果在更新列表的内容之后你的项目少于 nTopIndex 会发生什么吗?
    • 我猜 EnsureVisible(nTopIndex) 不会有任何效果。您最终会得到一个向下滚动到底部的列表。
    • 为什么 M$ 不费心提供 SetTopIndex 方法? :-(
    【解决方案2】:

    另一种方法是这样的:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      • 1970-01-01
      • 1970-01-01
      • 2010-10-03
      • 2011-07-15
      • 1970-01-01
      相关资源
      最近更新 更多