Search code examples
winapimfccomctl32

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.

Now I need to set the topmost visible item, but surprisingly there is no LVM_SETTOPINDEX message which would be natural.

Is there an easy clean way to set the topmost item?

My list-control is always in report mode.


Solution

  • 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.