新手请教:如何在CListView中添加数据?

yongship 2002-06-01 10:25:14
我在学习CListView的使用中创建一个基于CListView的SDI工程,可在将一个CArray中的数据添加到CList控件(Report view)时总是不成功,特来求助,请教正确的代码应如何写?谢谢!
...全文
53 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
scthb 2002-06-10
  • 打赏
  • 举报
回复
刚好,我也正在编写这样的程序。下面是我的实现方法。主要来自于Microsoft Press《Programming Windows with MFC(Second Edition)》的示例。

int CMyFriendsView::AddItem()
{
LV_ITEM lvi;
lvi.mask=LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM;
lvi.iItem=GetListCtrl().GetItemCount() ;
lvi.iSubItem=0;
if(GetDocument()->list.GetTail()->m_strSex.CompareNoCase(_T("男"))==0 )
lvi.iImage=0;
else
lvi.iImage=1;
lvi.pszText=LPSTR_TEXTCALLBACK;
lvi.lParam=(LPARAM)GetDocument()->list.GetTail();

if(GetListCtrl().InsertItem(&lvi)==-1)
return -1;

GetListCtrl().SetCheck(GetListCtrl().GetItemCount()-2,FALSE);
GetListCtrl().SetCheck(GetListCtrl().GetItemCount()-1,TRUE);
SETSTATUSLISTITEM ssli;
ssli.nAllTotal=GetDocument()->list.GetCount();
ssli.nTotal=GetListCtrl().GetItemCount();
ssli.nIndex =ssli.nTotal-1;
ssli.strItemName=GetListCtrl().GetItemText(ssli.nIndex ,0);
ssli.nSelCount=GetListCtrl().GetSelectedCount();

AfxGetApp()->GetMainWnd()->SendMessage(WM_USER_SETSTATUSTEXT_LISTITEM,0,(LPARAM)&ssli);

return lvi.iItem;
}
Yokishiro 2002-06-01
  • 打赏
  • 举报
回复
m_TcpList->InsertItem(i,local);
m_TcpList->SetItemText(i,1,remote);
m_TcpList->SetItemText(i,2,TCPStatus(pTcpTable->table[i].dwState));
m_TcpList是指向CListCtrl的指针

下面是关于这两个函数的一些说明其实就是MSDN中的
Inserts an item into the list view control.

int InsertItem(
const LVITEM* pItem
);
int InsertItem(
int nItem,
LPCTSTR lpszItem
);
int InsertItem(
int nItem,
LPCTSTR lpszItem,
int nImage
);
int InsertItem(
UINT nMask,
int nItem,
LPCTSTR lpszItem,
UINT nState,
UINT nStateMask,
int nImage,
LPARAM lParam
);
Parameters
pItem
Pointer to an LVITEM structure that specifies the item's attributes, as described in the Platform SDK.
nItem
Index of the item to be inserted.
lpszItem
Address of a string containing the item's label, or LPSTR_TEXTCALLBACK if the item is a callback item. For information on callback items, see CListCtrl::GetCallbackMask.
nImage
Index of the item's image, or I_IMAGECALLBACK if the item is a callback item. For information on callback items, see CListCtrl::GetCallbackMask.
nMask
The nMask parameter specifies which item attributes passed as parameters are valid. It can be one or more of the mask values described in LVITEM structure in the Platform SDK. The valid values can be combined with the bitwise OR operator.
nState
Indicates the item's state, state image, and overlay image. See the Platform SDK topics LVITEM for more information and List View Item States for a list of valid flags.
nStateMask
Indicates which bits of the state member will be retrieved or modified. See LVITEM in the Platform SDK for more information.
nImage
Index of the item's image within the image list.
lParam
A 32-bit application-specific value associated with the item. If this parameter is specified, you must set the nMask attribute LVIF_PARAM.

Changes the text of a list view item or subitem.

BOOL SetItemText(
int nItem,
int nSubItem,
LPCTSTR lpszText
);
Parameters
nItem
Index of the item whose text is to be set.
nSubItem
Index of the subitem, or zero to set the item label.
lpszText
Pointer to a string that contains the new item text.
Return Value
Nonzero if successful; otherwise zero.

Example
See the example for CListCtrl::InsertItem.

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧