MFC高级控件之列表视图控件(CListCtrl)

MFC高级控件之列表视图控件(CListCtrl),第1张

CListCtrl类封装了列表视图控件的功能,该控件显示一组项目,每个项目由一个图标和一个标签组成。列表视图控件可以以4种不同的方式显示其内容,分别是:图标视图、小图标视图、列表视图、报表视图。

图标视图:每个项目显示为一个全尺寸图标,32 x 32像素,下面有一个标签。用户可以将项目拖动到列表视图窗口中的任何位置。

小图标视图:每个项目显示为一个小尺寸图标,16 x 16像素,标签在其右侧。用户可以将项目拖动到列表视图窗口中的任何位置。

列表视图:每个项目都显示为一个小图标,右侧有一个标签。项目按列排列,不能拖动到列表视图窗口中的任何位置。

报表视图:每个项目都显示在自己的行中,其他信息按列排列在右侧。最左边的列包含小图标和标签,后续列包含应用程序指定的子项。嵌入式标题控件CHeaderCtrl类实现了这些列。

CListCtrl的成员函数如下:

CListCtrl Class Members

Construction
Attributes
Operations
Overridables

Construction

CListCtrlConstructs a CListCtrl object.
CreateCreates a list control and attaches it to a CListCtrl object.

Attributes

GetBkColorRetrieves the background color of a list view control.
SetBkColorSets the background color of the list view control.
GetImageListRetrieves the handle of an image list used for drawing list view items.
SetImageListAssigns an image list to a list view control.
GetItemCountRetrieves the number of items in a list view control.
GetItemRetrieves a list view item’s attributes.
SetItemSets some or all of a list view item’s attributes.
GetCallbackMaskRetrieves the callback mask for a list view control.
SetCallbackMaskSets the callback mask for a list view control.
GetNextItemSearches for a list view item with specified properties and with specified relationship to a given item.
GetFirstSelectedItemPositionRetrieves the position of the first selected list view item in a list view control.
GetNextSelectedItemRetrieves the next selected list view item for iterating.
GetItemRectRetrieves the bounding rectangle for an item.
SetItemPositionMoves an item to a specified position in a list view control.
GetItemPositionRetrieves the position of a list view item.
GetStringWidthDetermines the minimum column width necessary to display all of a given string.
GetEditControlRetrieves the handle of the edit control used to edit an item’s text.
GetColumnRetrieves the attributes of a control’s column.
SetColumnSets the attributes of a list view column.
GetColumnWidthRetrieves the width of a column in report view or list view.
SetColumnWidthChanges the width of a column in report view or list view.
GetCheckRetrieves the current display status of the state image associated with an item.
SetCheckSets the the current display status of the state image associated with an item.
GetViewRectRetrieves the bounding rectangle of all items in the list view control.
GetTextColorRetrieves the text color of a list view control.
SetTextColorSets the text color of a list view control.
GetTextBkColorRetrieves the text background color of a list view control.
SetTextBkColorSets the background color of text in a list view control.
GetTopIndexRetrieves the index of the topmost visible item.
GetCountPerPageCalculates the number of items that can fit vertically in a list view control.
GetOriginRetrieves the current view origin for a list view control.
SetItemStateChanges the state of an item in a list view control.
GetItemStateRetrieves the state of a list view item.
GetItemTextRetrieves the text of a list view item or subitem.
SetItemTextChanges the text of a list view item or subitem.
SetItemCountPrepares a list view control for adding a large number of items.
SetItemDataSets the item’s application-specific value.
GetItemDataRetrieves the application-specific value associated with an item.
GetSelectedCountRetrieves the number of selected items in the list view control.
SetColumnOrderArraySets the column order (left to right) of a list view control.
GetColumnOrderArrayRetrieves the column order (left to right) of a list view control.
SetIconSpacingSets the spacing between icons in a list view control.
GetHeaderCtrlRetrieves the header control of a list view control.
GetHotCursorRetrieves the cursor used when hot tracking is enabled for a list view control.
SetHotCursorSets the cursor used when hot tracking is enabled for a list view control.
GetSubItemRectRetrieves the bounding rectangle of an item in a list view control.
GetHotItemRetrieves the list view item currently under the cursor.
SetHotItemSets the current hot item of a list view control.
GetSelectionMarkRetrieves the selection mark of a list view control.
SetSelectionMarkSets the selection mark of a list view control.
GetExtendedStyleRetrieves the current extended styles of a list view control.
SetExtendedStyleSets the current extended styles of a list view control.
SubItemHitTestDetermines which list view item, if any, is at a given position.
GetWorkAreasRetrieves the current working areas of a list view control.
GetNumberOfWorkAreasRetrieves the current number of working areas for a list view control.
SetItemCountExSets the item count for a virtual list view control.
SetWorkAreasSets the area where icons can be displayed in a list view control.
ApproximateViewRectDetermines the width and height required to display the items of a list view control.
GetBkImageRetreives the current background image of a list view control.
SetBkImageSets the current background image of a list view control.
GetHoverTimeRetrieves the current hover time of a list view control.
SetHoverTimeSets the current hover time of a list view control.

Operations

InsertItemInserts a new item in a list view control.
DeleteItemDeletes an item from the control.
DeleteAllItemsDeletes all items from the control.
FindItemSearches for a list view item having specified characteristics.
SortItemsSorts list view items using an application-defined comparison function.
HitTestDetermines which list view item is at a specified position.
EnsureVisibleEnsures that an item is visible.
ScrollScrolls the content of a list view control.
RedrawItemsForces a list view control to repaint a range of items.
UpdateForces the control to repaint a specified item.
ArrangeAligns items on a grid.
EditLabelBegins in-place editing of an item’s text.
InsertColumnInserts a new column in a list view control.
DeleteColumnDeletes a column from the list view control.
CreateDragImageCreates a drag image list for a specified item.

Overridables

DrawItemCalled when a visual aspect of an owner-draw control changes.

下面以一个实例来演示其使用。

1. 新建一个对话框程序,在对话框上添加两个List Control,如下:

并将第二个CListCtrl控件外观的View向设为Report,如下:

2. 将下面几个图标导入到资源中。

 导入后如下:

3. 在对话框类中声明一个变量,如下:

 再声明一个变量,如下:

3. 在OnInitDialog()函数中“// TODO: 在此添加额外的初始化代码”后面添加代码如下:

BOOL CCtrlListTestDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时,框架将自动
	//  执行此 *** 作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	mImageList.Create(32, 32, ILC_COLOR24 | ILC_MASK, 1, 0);
	for (int i = 0; i < 7; i++)
	{
		mImageList.Add(AfxGetApp()->LoadIconW(IDI_ICON1+i));
	}
	pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST1);
	pListCtrl->SetImageList(&mImageList,LVSIL_NORMAL);
	pListCtrl->SetExtendedStyle(LVS_ICON);
	pListCtrl -> InsertItem(0,_T("ID: 1000"),0);
	pListCtrl->InsertItem(1, _T("ID: 1001"), 1);
	pListCtrl->InsertItem(2, _T("ID: 1002"), 2);
	pListCtrl->InsertItem(3, _T("ID: 1003"), 3);
	pListCtrl->InsertItem(4, _T("ID: 1004"), 4);
	pListCtrl->InsertItem(5, _T("ID: 1005"), 5);
	pListCtrl->InsertItem(6, _T("ID: 1006"),6);

	pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST2);
	pListCtrl->SetExtendedStyle(LVS_EX_FLATSB|LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP|LVS_EX_GRIDLINES);
	pListCtrl->InsertColumn(0, _T("ID号"), LVCFMT_LEFT,80, 0);
	pListCtrl->InsertColumn(1, _T("姓名"), LVCFMT_LEFT, 80, 1);
	pListCtrl->InsertColumn(2, _T("班级"), LVCFMT_LEFT, 80, 2);
	pListCtrl->InsertColumn(3, _T("入会时间"), LVCFMT_LEFT, 80, 3);
	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

此时试运行,结果如下:

 4.在对话框中新增加“查询” “清空列表“按钮。如下:

在为两个按钮添加事件处理程序前,我们来改变一下第一个CListCtrl控件的外观项的View选项,首先由Icon变为Small Icon,如下:

试运行,结果如下:

奇怪图标没了,注意我装载的图标是32X32。再将外观项的View选项改为List,如下:

 试运行,结果如下:

 与上次的结果相同,再改回Icon。

5.为对话框声明一个Cstring变量,来记录选择项文字,如下:

6.为对话框类添加第一个CListCtrl的HNM_CLICK事件响应函数。如下:

 在函数中加入如下代码:

void CCtrlListTestDlg::OnNMClickList1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST1);
	int iCount = pListCtrl->GetItemCount();
	int iFirst = pListCtrl->GetTopIndex();
	for (int i = 0; i < iCount; i++)
	{
		if(pListCtrl->GetItemState(iFirst + i, LVIS_SELECTED) == LVIS_SELECTED)
		{
			mstr = pListCtrl->GetItemText(iFirst + i, 0);
			break;
		}
	}
	*pResult = 0;
}

7.为"查询"按钮添加事件处理函数,并在函数中添加如下代码:

void CCtrlListTestDlg::OnBnClickedInquiry()
{
	// TODO: 在此添加控件通知处理程序代码
	CString mstr1 = mstr.Right(4);
	int id = _ttoi(mstr1);
	pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST2);
	pListCtrl->InsertItem(0, mstr1, 0);
	switch (id)
	{
		case 1000:
			
			pListCtrl->SetItemText(0, 1, L"李琪");
			pListCtrl->SetItemText(0, 2, L"高2三班");
			pListCtrl->SetItemText(0,3, L"20200906");
			break;
		case 1001:
			pListCtrl->SetItemText(0,1, L"李雯");
			pListCtrl->SetItemText(0,2, L"高2三班");
			pListCtrl->SetItemText(0, 3, L"20200906");
			break;
		case 1002:
			pListCtrl->SetItemText(0, 1,L"蔡明");
			pListCtrl->SetItemText(0, 2, L"高2二班");
			pListCtrl->SetItemText(0, 3, L"20210906");
			break;
		case 1003:
			pListCtrl->SetItemText(0, 1, L"王伟");
			pListCtrl->SetItemText(0, 2, L"高2二班");
			pListCtrl->SetItemText(0, 3, L"20210906");
			break;
		case 1004:
			pListCtrl->SetItemText(0, 1, L"王红");
			pListCtrl->SetItemText(0, 2, L"高2四班");
			pListCtrl->SetItemText(0, 3, L"20210906");
			break;
		case 1005:
			pListCtrl->SetItemText(0, 1, L"李艳");
			pListCtrl->SetItemText(0, 2, L"高3四班");
			pListCtrl->SetItemText(0, 3, L"20200906");
			break;
		case 1006:
			pListCtrl->SetItemText(0, 1, L"刘彬");
			pListCtrl->SetItemText(0, 2, L"高3四班");
			pListCtrl->SetItemText(0, 3, L"20200906");
			break;
	}
}

8. 为“清空列表”按钮添加事件处理函数,并在函数中添加如下代码:

void CCtrlListTestDlg::OnBnClickedClearTable()
{
	// TODO: 在此添加控件通知处理程序代码
	pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST2);
	int count = pListCtrl->GetItemCount();
	for (int i = 0; i < count; i++)
	{
		pListCtrl->DeleteItem(0);
	}
}

试运行,点击第一个控件项目,结果如下:

 选中左边ID:1000,如下:

 再点击查询按钮,结果如下:

重复以上 *** 作,但选取不同的内容,结果如下:

 

 点击清空列表按钮,结果如下:

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1296053.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存