热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

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

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 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 DeleteItem(0);}
}

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

 选中左边ID:1000,如下:

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

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

 

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

 


推荐阅读
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • SmartRefreshLayout自定义头部刷新和底部加载
    1.添加依赖implementation‘com.scwang.smartrefresh:SmartRefreshLayout:1.0.3’implementation‘com.s ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • Android中怎么利用TextView显示部分文字高亮
    这篇文章将为大家详细讲解有关Android中怎么利用TextView显示部分文字高亮,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇 ... [详细]
  • 记录一些 Latex 的技巧
    Latex一些技巧:1.如何创建不浮动的的figure和table\makeatletter\newcommand{\figcaption}{\def\captyp ... [详细]
author-avatar
年庚瑶
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有