博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
再探MFC(五)列表控件
阅读量:4120 次
发布时间:2019-05-25

本文共 3223 字,大约阅读时间需要 10 分钟。

List Control and List View

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

  • Directly, by embedding a  object in a dialog class.
  • Indirectly, by using class .

CListView makes it easy to integrate a listcontrol with the MFC document/view architecture, encapsulating the control muchas encapsulatesan 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  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  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.

 

源文档 <>

 

 

 

使用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  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 (see).
  1. In , set the styles for the CListCtrl. See . This determines the kind of "view" you get in the control, although you can change the view later.

 

源文档 <>

注意列表控件属性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;

}

你可能感兴趣的文章
XML生成(三):JDOM生成
查看>>
Ubuntu Could not open lock file /var/lib/dpkg/lock - open (13:Permission denied)
查看>>
collect2: ld returned 1 exit status
查看>>
C#入门
查看>>
C#中ColorDialog需点两次确定才会退出的问题
查看>>
数据库
查看>>
nginx反代 499 502 bad gateway 和timeout
查看>>
linux虚拟机安装tar.gz版jdk步骤详解
查看>>
python猜拳游戏
查看>>
python实现100以内自然数之和,偶数之和
查看>>
python数字逆序输出及多个print输出在同一行
查看>>
苏宁产品经理面经
查看>>
百度产品经理群面
查看>>
去哪儿一面+平安科技二面+hr面+贝贝一面+二面产品面经
查看>>
element ui 弹窗在IE11中关闭时闪现问题修复
查看>>
vue 遍历对象并动态绑定在下拉列表中
查看>>
Vue动态生成el-checkbox点击无法选中的解决方法
查看>>
python __future__
查看>>
MySQL Tricks1
查看>>
python 变量作用域问题(经典坑)
查看>>