当前位置:网站首页>【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列的文本,其他列的没办法编辑。
边栏推荐
- 截图小妙招
- 《单片机原理与应用》——并行IO口原理
- Maneuvering target tracking -- current statistical model (CS model) extended Kalman filter / unscented Kalman filter matlab implementation
- Brief introduction to AES
- Tita OKR: a dashboard to master the big picture
- Matlab tips (16) consistency verification of matrix eigenvector eigenvalue solution -- analytic hierarchy process
- IT 技术电子书 收藏
- Centos7 shell脚本一键安装jdk、mongo、kafka、ftp、postgresql、postgis、pgrouting
- MATLAB【函数和图像】
- Guidelines and principles of did
猜你喜欢
随机推荐
MATLAB小技巧(23)矩阵分析--模拟退火
factory type_id::create过程解析
What is the material of 16mo3 steel plate? What is the difference between 16mo3 and Q345R?
factory type_ Id:: create process resolution
Share 7 books I read in the first half of 2022
Model and view of QT
Foundation: 3 Opencv getting started images and videos
如何招到适合自己店铺的淘宝主播
内存大小端
为什么LTD独立站就是Web3.0网站!
Serial port to WiFi module communication
SPL Introduction (I)
Conception et mise en service du processeur - chapitre 4 tâches pratiques
【面试必刷101】链表
In depth learning training sample amplification and tag name modification
DID的使用指南,原理
分享2022上半年我读过的7本书
win7 pyinstaller打包exe 后报错 DLL load failed while importing _socket:参数错误
Brief introduction to AES
MATLAB【函数求导】



![Matlab [function derivation]](/img/ba/9fb9da8a458d0c74b29b21a17328fc.png)





