简体   繁体   English

列表控件需要LVM_SETTOPINDEX

[英]List control LVM_SETTOPINDEX needed

The list-view control has the LVM_GETTOPINDEX message that allows to get the index of the topmost visible item. 列表视图控件具有LVM_GETTOPINDEX消息,该消息允许获取最顶层可见项的索引。

Now I need to set the topmost visible item, but surprisingly there is no LVM_SETTOPINDEX message which would be natural. 现在我需要设置最顶层的可见项,但令人惊讶的是没有LVM_SETTOPINDEX消息是自然的。

Is there an easy clean way to set the topmost item? 是否有一种简单的方法来设置最顶级的项目?

My list-control is always in report mode. 我的列表控件始终处于报告模式。

  1. Use LVM_GETITEMPOSITION or LVM_GETITEMRECT to obtain the items position. 使用LVM_GETITEMPOSITIONLVM_GETITEMRECT获取项目位置。
  2. Use LVM_SCROLL to scroll the list so that your item is the top item. 使用LVM_SCROLL滚动列表,以便您的项目是顶部项目。

First, it may NOT be possible. 首先,它可能是不可能的。 For example, if the list doesn't have enough items after your top index to fill the page. 例如,如果列表在您的顶部索引之后没有足够的项目来填充页面。

As there is no direct way, you can count the number of items on the page, add that count to your index and call EnsureVisible() . 由于没有直接的方法,您可以计算页面上的项目数,将该计数添加到索引并调用EnsureVisible() This will make your sure that your top is above visible page. 这将确保您的顶部位于可见页面之上。 The next EnsureVisible() for your item will bring it into the view, at the top of the page. 您商品的下一个EnsureVisible()会将其带入页面顶部的视图中。 Of course, you would need to block updates to avoid jerking of the screen. 当然,您需要阻止更新以避免屏幕抖动。

Example (updated by Vlad): 示例(由Vlad更新):

void CDlg::SetTopIndex(int top)
{
    int bottom = min(top + m_List.GetCountPerPage(), m_List.GetItemCount() - 1);
    m_List.SetRedraw(FALSE);
    m_List.EnsureVisible(bottom, TRUE);
    m_List.EnsureVisible(top, FALSE);
    m_List.SetRedraw(TRUE);
}

This function does the job: 这个功能完成了这项工作:

void SetTopIndex(CListCtrl & listctrl, int topindex)
{
  int actualtopindex = listctrl.GetTopIndex();
  int horspacing;
  int lineheight;
  listctrl.GetItemSpacing(TRUE, &horspacing, &lineheight);

  CSize scrollsize(0, (topindex - actualtopindex) * lineheight);
  listctrl.Scroll(scrollsize);
}

No parameter sanitation is done here. 这里没有参数卫生。

Thanks to David Heffernan and Remy Lebeau for giving me the idea. 感谢David Heffernan和Remy Lebeau给我的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM