有时候,我们需要在列表框ListBox中插入带图标的文字项,这就需要自己派生一个类出来了,网上的一个CIconListBox类还不错,网站http://www.codeguru.com/Cpp/controls/listbox/article.php/c4749/
这里介绍下使用方法,源代码我有改动,环境Visual Studio 2008 SP1:
1.基于对话框的工程,名称为IconListBoxTest;
2.拖动控件到对话框上,按下图所示排列:
3.导入IconListBox.cpp和IconListBox.h两个文件到工程,在对话框头文件包含
#include "IconListBox.h"
4.为ListBox控件,改变其属性“Owner Draw”为“Fixed”,其“Has Strings”为“TRUE”添加变量:
CIconListBox m_ListBox;
添加一个图片列表变量:
CImageList m_imgNormal;
导入图片资源,修改其ID号为IDB_LB_IMG,在对话框的初始化函数里面添加如下代码(可自己根据修改):
CBitmap bmp;
m_imgNormal.Create(16,
16,
ILC_COLOR32 | ILC_MASK,
10, // 初始化图片数量
10);
ASSERT(m_imgNormal.m_hImageList);
bmp.LoadBitmap(IDB_LB_IMG);
m_imgNormal.Add( &bmp, -1);
bmp.DeleteObject();
m_ListBox.SetImageList(&m_imgNormal);
for(int i = 0; i < 10 ; i++ )
m_ListBox.AddString( _T("Hello World") , i );
m_imgNormal.Create(16,
16,
ILC_COLOR32 | ILC_MASK,
10, // 初始化图片数量
10);
ASSERT(m_imgNormal.m_hImageList);
bmp.LoadBitmap(IDB_LB_IMG);
m_imgNormal.Add( &bmp, -1);
bmp.DeleteObject();
m_ListBox.SetImageList(&m_imgNormal);
for(int i = 0; i < 10 ; i++ )
m_ListBox.AddString( _T("Hello World") , i );
5.右边按钮的事件如下:
void CIconListBoxTestDlg::OnBnClickedAdd()
{
if(UpdateData())
{
if(!m_Str.IsEmpty())
m_ListBox.AddString(m_Str,m_Img);
else
AfxMessageBox(_T("输入文本字符串!"));
}
}
void CIconListBoxTestDlg::OnBnClickedInsert()
{
if(UpdateData())
{
if( m_Str.IsEmpty() )
{
AfxMessageBox(_T("输入文本字符串!"));
return;
}
int iSel = m_ListBox.GetCurSel();
if( iSel != LB_ERR )
m_ListBox.InsertString(iSel, m_Str , m_Img);
else
AfxMessageBox(_T("选择左边列表其中一项!"));
}
}
void CIconListBoxTestDlg::OnBnClickedDelete()
{
int iSel = m_ListBox.GetCurSel();
if( iSel != LB_ERR )
m_ListBox.DeleteString( iSel );
else
AfxMessageBox(_T("选择左边列表其中一项!"));
}
void CIconListBoxTestDlg::OnBnClickedChangeicon()
{
if(UpdateData())
{
int iSel = m_ListBox.GetCurSel();
if(iSel != LB_ERR )
m_ListBox.SetItemImage(iSel,m_Img);
else
AfxMessageBox(_T("选择左边列表其中一项!"));
}
}
void CIconListBoxTestDlg::OnBnClickedImgList()
{
if(UpdateData())
{
if( m_Check )
m_ListBox.SetImageList(&m_imgNormal);
else
m_ListBox.SetImageList();
m_ListBox.RedrawWindow();
}
else
{
((CButton *)GetDlgItem(IDC_IMG_LIST))->SetCheck(m_Check);
}
}
{
if(UpdateData())
{
if(!m_Str.IsEmpty())
m_ListBox.AddString(m_Str,m_Img);
else
AfxMessageBox(_T("输入文本字符串!"));
}
}
void CIconListBoxTestDlg::OnBnClickedInsert()
{
if(UpdateData())
{
if( m_Str.IsEmpty() )
{
AfxMessageBox(_T("输入文本字符串!"));
return;
}
int iSel = m_ListBox.GetCurSel();
if( iSel != LB_ERR )
m_ListBox.InsertString(iSel, m_Str , m_Img);
else
AfxMessageBox(_T("选择左边列表其中一项!"));
}
}
void CIconListBoxTestDlg::OnBnClickedDelete()
{
int iSel = m_ListBox.GetCurSel();
if( iSel != LB_ERR )
m_ListBox.DeleteString( iSel );
else
AfxMessageBox(_T("选择左边列表其中一项!"));
}
void CIconListBoxTestDlg::OnBnClickedChangeicon()
{
if(UpdateData())
{
int iSel = m_ListBox.GetCurSel();
if(iSel != LB_ERR )
m_ListBox.SetItemImage(iSel,m_Img);
else
AfxMessageBox(_T("选择左边列表其中一项!"));
}
}
void CIconListBoxTestDlg::OnBnClickedImgList()
{
if(UpdateData())
{
if( m_Check )
m_ListBox.SetImageList(&m_imgNormal);
else
m_ListBox.SetImageList();
m_ListBox.RedrawWindow();
}
else
{
((CButton *)GetDlgItem(IDC_IMG_LIST))->SetCheck(m_Check);
}
}
6.编译完成。效果如下:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2012-12-24 在VC中定制Doxygen注释宏
2012-12-24 Doxygen + Graphviz + Htmlhelp, 成为文档好手
2012-12-24 C++开发常用工具“群英会”