【问题标题】:CListCtrl displays inserted item multiple timesCListCtrl 多次显示插入的项目
【发布时间】:2013-10-14 11:59:49
【问题描述】:

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 );

【问题讨论】:

    标签: c++ mfc clistctrl


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复和洞察力。
    • 原来我没有正确实现滚动按钮。因此,当我向上或向下滚动时,屏幕上会“重复”行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 2013-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    相关资源
    最近更新 更多