c++ - CListCtrl 多次显示插入的项目

标签 c++ mfc clistctrl

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

最佳答案

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

关于c++ - CListCtrl 多次显示插入的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19232859/

相关文章:

c++ - 使用 QStackedWidget 的分层 QTreeView 映射

c++ - 在CListCtrl中搜索项目

visual-studio - CListCtrl 设置字体样式为粗体

c++ - CListCtrl 类重写和 OnTimer

C++ 缺少类型说明符 : syntax error

c++ - C/C++ 开发人员是否应该了解 IGMP 和 BGP 协议(protocol)以使用多播?

c++ - lambda 函数/表达式是否支持 constexpr?

visual-c++ - VS CShellManager未声明

c++ - 分层窗口、UpdateLayeredWindow 和性能

mfc - 是否可以更改 CMFCEditBrowseCtrl 控件的背景颜色?