1. 程式人生 > >再探MFC(五)列表控制元件

再探MFC(五)列表控制元件

List Control and List View

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

  • Directly, by embedding aCListCtrl 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.)

ACListView 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 theCListCtrl 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 theAdd 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. InOnInitDialog, set the styles for theCListCtrl. See Changing List Control Styles. 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;

}