首页 > 编程语言 > 详细

再探MFC(四)列表控件

时间:2015-03-27 20:03:15      阅读:290      评论:0      收藏:0      [点我收藏+]

List Control and List View

For convenience, MFCencapsulates the list control in two ways. You canuse list controls:

  • Directly, by embedding a CListCtrl object in a dialog class.

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:

  • Add, delete, or manipulate "items" in the list.
  • Set or get list control attributes.

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

  1. In the dialog editor, add a List Control to your dialog template resource. Specify its control ID.
  1. Use the Add Member Variable Wizard to add a member variable of type CListCtrl with the Control property. You can use this member to call CListCtrl member functions.
  1. Use the Properties window to map handler functions in the dialog class for any list control notification messages you need to handle (seeMapping Messages to Functions).
  1. In OnInitDialog, set the styles for the CListCtrl. See Changing List Control Styles. This determines the kind of "view" you get in the control, although you can change the view later.

 

源文档 <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取得

技术分享



再探MFC(四)列表控件

原文:http://blog.csdn.net/soliddream66/article/details/44680117

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!