【发布时间】: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 );
【问题讨论】: