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

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

问题描述

如何维护CListCtrl的水平滚动条位置?我会定期转储并重新填充列表控件的内容,因此无需显式记住旧位置并将其恢复,滚动只返回到左上角.

How can I maintain the horizontal scroll bar position of a CListCtrl? I periodically dump and repopulate the contents of the list control so without explicitly remembering the old position and restoring it the scroll just goes back to the top left.

我问了一个相关问题, CListCtrl:如何保持滚动位置?,但早些时候,当时我只对垂直滚动位置感兴趣,提供的答案解决了这一问题.但是,现在我想记住并恢复水平滚动位置(以及垂直滚动).

I asked a related question, CListCtrl: How to maintain scroll position?, earlier but at the time I was only interested in vertical scroll position and the answer supplied solved that. However, now I want to remember and restore the horizontal scroll position (as well the vertical scroll).

推荐答案

首先,您可能会觉得更简单.重新填充列表之前和重新填充强制列表控件之后,必须保存位置以更新新内容.

First of all, it is simpler you may think. You have to save position before repopulating list and after repopulating force list control to update new content.

此外,您可能会考虑以下事实:新内容可能具有不同数量的项目,因此您必须相对于最大滚动位置设置位置.

Also, you may take under consideration the fact that new content may have different number of items, hence you will have to set position relative to the max scroll position.

示例代码如下:

    SCROLLINFO sbiBefore = { sizeof(SCROLLINFO) };
    SCROLLINFO sbiAfter = { sizeof(SCROLLINFO) };

    // get scroll info before
    sbiBefore.fMask = SIF_ALL;
    m_List.GetScrollInfo(SB_HORZ, &sbiBefore);

    RenewContents();

    // force control to redraw
    int iCount = m_List.GetItemCount();
    m_List.RedrawItems(0, iCount);

    // get the scroll info after
    sbiAfter.fMask = SIF_ALL;
    m_List.GetScrollInfo(SB_HORZ, &sbiAfter);

    double dRatio = (double)sbiAfter.nMax / sbiBefore.nMax;

    // compute relative new position
    sbiAfter.fMask = SIF_POS;
    sbiAfter.nPos = dRatio * sbiBefore.nPos;

    // set new position
    BOOL bSet = m_List.SetScrollInfo(SB_HORZ, &sbiAfter);

我确信您可以用相同的方式处理垂直滚动. 在您提到的帖子中,因为您有更适当的方式来执行,所以使用EnsureVisible不必要地强制进行更新. 另外,如果最后一个项目已经可见,则不能使用"EnsureVisible".

I am sure that you can handle vertical scroll in the same manner. In the post you mentioned, EnsureVisible is used to force update unnecessarily, since you have more proper way of doing it. Also, using EnsureVisible would not work if last item is visible already.

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

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