1

CMyListCtrl处于虚拟数据模式和所有者抽奖。当LVN_GETDISPINFO控件需要数据时会发送通知。

下面的代码工作正常,除了它多次显示每一行。

文档说,如果我设置项目的掩码LVIF_DI_SETITEM标志,它就不会这样做。文档还说pItem->iGroupId必须在InsertItem我之前设置,但是控件仍然为每个插入的行显示许多行。

void CMyListCtrl::OnLvnGetdispinfo(NMHDR *pNMHDR, LRESULT *pResult)
{
  NMLVDISPINFO *pDispInfo = reinterpret_cast<NMLVDISPINFO*>(pNMHDR);

  //Create a pointer to the item
  LV_ITEM* pItem= &(pDispInfo)->item;
  CString text, strippedText;

  //Does the control need text information?
  if( pItem->mask & LVIF_TEXT )
  {
    if(pItem->iSubItem == 0) // only first column used
    {
      text.Format( L"%s", cacheNotifyVect[ cacheNdx ] );

      //Copy the text to the LV_ITEM structure
      lstrcpyn(pItem->pszText, text, pItem->cchTextMax);
      pItem->mask |= LVIF_DI_SETITEM; // documentation says to set this so the list-view control will store the requested data and will not ask for it again. The application must set the iGroupid member of the LVITEM structure.
    }
  }

  *pResult = 0;
}

void CMyListCtrl::AddNotifyString(const CString & outListStr)
{
  cacheNotifyVect[ cacheNdx % CACHE_CAPACITY] = outListStr; // RT:130908: make cache round robin for notify
  LVITEM item;
  item.mask = LVIF_TEXT;
  item.iItem = cacheNdx++;
  item.iSubItem = 0;
  item.iGroupId = I_GROUPIDNONE; // so control will store data internally
  item.pszText = (LPTSTR)(LPCTSTR)( outListStr );
  outputWnd->outputNotify.InsertItem( &item );
4

1 回答 1

0

您已将 item.mask 设置为 LVIF_TEXT,因此只观察 iItem 成员而忽略 iGroupId 成员。将 item.mask 设置为 LVIF_TEXT|LVIF_GROUPID。

于 2014-02-24T11:36:47.943 回答