当前位置:网站首页>【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列的文本,其他列的没办法编辑。
边栏推荐
猜你喜欢

Gateway-88

Field agricultural irrigation system

How can enterprises and developers take the lead in the outbreak of cloud native landing?

分享2022上半年我读过的7本书

Advanced C language pointer (Part 2)

机动目标跟踪——当前统计模型(CS模型)扩展卡尔曼滤波/无迹卡尔曼滤波 matlab实现

《单片机原理及应用》—定时器、串行通信和中断系统

《单片机原理及应用》-片外拓展

The use of word in graduation thesis

SPL-安装与基本使用(二)
随机推荐
What is the material of 16mo3 steel plate? What is the difference between 16mo3 and Q345R?
Leetcode T39: 组合总和
Guidelines and principles of did
What is 1cr0.5mo (H) material? 1cr0.5mo (H) tensile yield strength
Foundation: 2 The essence of image
TypeError: __ init__ () got an unexpected keyword argument ‘autocompletion‘
IT 技术电子书 收藏
《微机原理》——微处理器内部及外部结构
Provincial election + noi Part VI skills and ideas
Provincial selection + noi Part II string
《单片机原理及应用》-片外拓展
How can enterprises and developers take the lead in the outbreak of cloud native landing?
Huawei machine test questions column subscription Guide
基础:2.图像的本质
[no title] free test questions for constructor municipal direction general foundation (constructor) and theoretical test for constructor municipal direction general foundation (constructor) in 2022
基于Gazebo的无人机管道检测
How to use OKR as the leadership framework of marketing department
[JS reverse] MD5 encryption parameter cracking
factory type_ Id:: create process resolution
Advanced C language pointer (Part 2)