List Control and List View
For convenience, MFCencapsulates the list control in two ways. You canuse list controls:
CListView makes it easy to integrate a listcontrol with the MFC document/view architecture, encapsulating the control muchas CEditViewencapsulatesan edit control: the control fills the entire surface area of an MFC view. (The view is the control, cast to CListView.)
A CListView object inherits from CCtrlView andits base classes and adds a member function to retrieve the underlying listcontrol. Use view members to work with the view as a view. Usethe GetListCtrl memberfunction to gain access to the list control‘s member functions. Use thesemembers to:
To obtain a reference to the CListCtrl underlying a CListView, call GetListCtrl fromyour list view class:
C++
CListCtrl& listCtrl = GetListCtrl();
This topic describes both ways to use the listcontrol.
源文档 <https://msdn.microsoft.com/zh-cn/library/vstudio/x9w88sx9(v=vs.110).aspx>
使用CListCtrl
To use CListCtrl directly in a dialog box
源文档 <https://msdn.microsoft.com/zh-cn/library/vstudio/tfzs968s(v=vs.110).aspx>
注意列表控件属性View选择Report.
添加列
在初始化列表视图时,先要调用InsertColumn()插入各个列
CRectrect;
m_contacts.GetClientRect(&rect);
m_contacts.InsertColumn(0,_T("姓名"), LVCFMT_LEFT, rect.Width() * 3 / 5);
m_contacts.InsertColumn(1,_T("电话"), LVCFMT_LEFT, rect.Width() *5);
添加项
表项插入与删除,通过InsertItem插入行,通过SetItemText设置行各列项
CStringdata[2] = { _T("张三"), _T("1234") };
LV_ITEMlvi;
lvi.mask= LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
lvi.iSubItem= 0;
lvi.pszText= data[0].GetBuffer(0);
lvi.iImage= 0;
lvi.iItem= 0;
m_contacts.InsertItem(&lvi);
for(int i = 0; i<2; i++) m_contacts.SetItemText(0, i, data[i]);
自定义参数
自定义参数,通过SetItemData设置,通过GetItemData取得
原文:http://blog.csdn.net/soliddream66/article/details/44680117