再探MFC(五)列表控件

9 篇文章 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;

intnRow = m_contacts.InsertItem(&lvi);

for(int i = 0; i<2; i++) m_contacts.SetItemText(nRow, i, data[i]);

 

排序

列表控件属性sort选择升序或降序

 

自定义参数

自定义参数,通过SetItemData设置,通过GetItemData取得

 

处理鼠标双击事件消息

选中列表控件,在属性视图的控件事件列表中选择NM_DBLCLK,点击下拉按钮选择add.

 

在生成的消息处理函数中添加如下代码

voidCFirstPage::OnNMDblclkListContacts(NMHDR *pNMHDR, LRESULT *pResult)

{

LPNMITEMACTIVATEpNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);

CDialogAddContactdlg;

intnIndex =pNMItemActivate->iItem;        

dlg.m_strName= m_contacts.GetItemText(nIndex, 0);

dlg.m_strPhone= m_contacts.GetItemText(nIndex, 1);

TRACE(_T("修改%d联系人.姓名:%s,电话:%s"),nIndex, dlg.m_strName, dlg.m_strPhone);

 

INT_PTRnResponse = dlg.DoModal();

if(nResponse == IDOK)

{

CStringstrName = dlg.m_strName;

CStringstrPhone = dlg.m_strPhone;

if(!strName.IsEmpty() && !strPhone.IsEmpty())

{

//删除再添加

m_contacts.DeleteItem(nIndex);

LV_ITEMlvi;

lvi.mask= LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;

lvi.iSubItem= 0;

lvi.pszText= strName.GetBuffer(0);

lvi.iImage= 0;

lvi.iItem= 0;

intnRow = m_contacts.InsertItem(&lvi);

m_contacts.SetItemText(nRow,0, strName);

m_contacts.SetItemText(nRow,1,strPhone);                        

}

 

}

elseif (nResponse == IDCANCEL)

{

 

}

 

*pResult= 0;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值