当前位置:网站首页>【MFC开发(17)】高级列表控件List Control
【MFC开发(17)】高级列表控件List Control
2022-07-01 08:33:00 【Demo.demo】
1.介绍
ListCtrl 高级列表控件也是我们平时编程过程中很常用的一个控件,一般涉及到报表展示、记录展示之类的,都需要ListCtrl 高级列表控件。例如:任务管理器啊,文件列表啊,等等都是ListCtrl 高级列表控件来实现的。


常用属性介绍:
Edit Lables 是否可编辑节点

View 用来设置高级列表控件的风格,有以下四种风格

Icon:为每个item显示大图标

Small Icon:为每个item显示小图标

List:显示一列带有小图标的item

Report:显示item详细资料

2.常用方法
(1)首先给控件添加变量

(2)设置扩展风格
//获得扩展风格
DWORD style = m_listctrl.GetExtendedStyle();
//添加网格线风格以及整行选中的风格以及前面加有选择框
style |= LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT|LVS_EX_CHECKBOXES;
//设置扩展风格
m_listctrl.SetExtendedStyle(style);(3)添加列
//添加列,0---序号,第二个参数显示内容,
//第三个参数显示对齐方式,第四个参数表头显示的宽度
m_listctrl.InsertColumn(0, _T("第一列"), LVCFMT_CENTER, 50);
m_listctrl.InsertColumn(1, _T("第二列"), LVCFMT_CENTER, 50);
m_listctrl.InsertColumn(2, _T("第三列"), LVCFMT_CENTER, 50);(4)添加行以及行内容
//插入行内容
//尾部添加行
m_listctrl.InsertItem(m_listctrl.GetItemCount(),_T("第一行"));
//可以设置某一行某一列的内容,参数依次为行,列,文本内容
m_listctrl.SetItemText(0,0, _T("111"));
m_listctrl.SetItemText(0,1, _T("222"));
m_listctrl.SetItemText(0,2, _T("333"));
(5)设置选中某一行以及取消选中
选中
void CMFC_Test1Dlg::OnBnClickedButton21()
{
// TODO: 在此添加控件通知处理程序代码
//设置高亮
m_listctrl.SetFocus();
//设置选中某一行
m_listctrl.SetItemState(1, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
}取消选中刚才选中的
void CMFC_Test1Dlg::OnBnClickedButton24()
{
// TODO: 在此添加控件通知处理程序代码
m_listctrl.SetItemState(1,0, LVIS_SELECTED | LVIS_FOCUSED);
}
(6)遍历控件中的每一行
void CMFC_Test1Dlg::OnBnClickedButton25()
{
// TODO: 在此添加控件通知处理程序代码
//首先获取有多少行
int ncount = m_listctrl.GetItemCount();
//遍历每一行
for (int i = 0; i < ncount; i++){
//一次获取i+1行的第一列、第二列、第三列
CString str1 = m_listctrl.GetItemText(i, 0);
CString str2 = m_listctrl.GetItemText(i, 1);
CString str3 = m_listctrl.GetItemText(i, 2);
}
}(7)获取选中的行
//获得当前选中的行
//首先获得位置
POSITION pos = m_listctrl.GetFirstSelectedItemPosition();
while (pos){
//根据位置获得选中行的索引
int row_idx = m_listctrl.GetNextSelectedItem(pos);
CString str_idx;
str_idx.Format(_T("%d"), str_idx);
MessageBox(str_idx);
}(8)获得多选框选中的行
void CMFC_Test1Dlg::OnBnClickedButton26()
{
// TODO: 在此添加控件通知处理程序代码
//首先获取有多少行
int ncount = m_listctrl.GetItemCount();
//遍历每一行
for (int i = 0; i < ncount; i++){
BOOL bcheck = m_listctrl.GetCheck(i);
if (bcheck){
CString check_idx;
check_idx.Format(_T("第%d行被勾选了"), i);
MessageBox(check_idx);
}
}
}
(9)删除行
void CMFC_Test1Dlg::OnBnClickedButton27()
{
// TODO: 在此添加控件通知处理程序代码
//删除某一行
m_listctrl.DeleteItem(0);
//删除所有行
m_listctrl.DeleteAllItems();
}需要注意的是删除一行之后,剩余所有行的索引会改变,比如删除了0行,则删除之后1行变成了0行
(10)编辑指定的行文本

需要注意的是要使可编辑,需要设置控件属性钟的Edit Labels为True
首先添加两个事件,开始编辑以及结束编辑


void CMFC_Test1Dlg::OnLvnBeginlabeleditList2(NMHDR *pNMHDR, LRESULT *pResult)
{
NMLVDISPINFO *pDispInfo = reinterpret_cast<NMLVDISPINFO*>(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
*pResult = 0;
}
void CMFC_Test1Dlg::OnLvnEndlabeleditList2(NMHDR *pNMHDR, LRESULT *pResult)
{
NMLVDISPINFO *pDispInfo = reinterpret_cast<NMLVDISPINFO*>(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
//三个参数依次为行,列,输入的内容
m_listctrl.SetItemText(pDispInfo->item.iItem, pDispInfo->item.iSubItem, pDispInfo->item.pszText);
*pResult = 0;
}需要注意的是只能编辑(索引为0的)第1列的文本,其他列的没办法编辑。
边栏推荐
- [deep analysis of C language] - data storage in memory
- IT 技术电子书 收藏
- 目标检测的yolov3、4、5、6总结
- Audio-AudioRecord create(一)
- 01 numpy introduction
- TypeError: __init__() got an unexpected keyword argument ‘autocompletion‘
- Gateway-88
- [JS reverse] MD5 encryption parameter cracking
- Stack implementation calculator
- The data analyst will be ruined without project experience. These 8 project resources will not be taken away
猜你喜欢

Principle and application of single chip microcomputer - off chip development

《微机原理》-绪论

MATLAB小技巧(16)矩阵特征向量特征值求解一致性验证--层次分析

C basic knowledge review (Part 4 of 4)

Brief introduction to AES

CPU design practice - Chapter 4 practical tasks - simple CPU reference design and debugging

Qt的模型与视图

Glitch Free时钟切换技术

基础:2.图像的本质

机动目标跟踪——当前统计模型(CS模型)扩展卡尔曼滤波/无迹卡尔曼滤波 matlab实现
随机推荐
你了解数据是如何存储的吗?(C整型和浮点型两类)
How to recruit Taobao anchor suitable for your own store
Leetcode t39: combined sum
电视机尺寸与观看距离
Intelligent water conservancy solution
5mo3 UHI HII HII 17mn4 19Mn6 executive standard
"Analysis of 43 cases of MATLAB neural network": Chapter 30 design of combined classifier based on random forest idea - breast cancer diagnosis
Foundation: 3 Opencv getting started images and videos
长安链同步节点配置与启动
19Mn6 German standard pressure vessel steel plate 19Mn6 Wugang fixed binding 19Mn6 chemical composition
[Yu Yue education] Shandong Vocational College talking about railway reference materials
2022 Chinese cook (technician) simulation test and Chinese cook (technician) practice test video
leetcode T31:下一排列
CPU design practice - Chapter 4 practical tasks - simple CPU reference design and debugging
Leetcode T29: 两数相除
Provincial election + noi Part VII computational geometry
Stack implementation calculator
Introduction to R language
Codeworks round 803 (Div. 2) VP supplement
MD文档中插入数学公式,Typora中插入数学公式