MFC列表控件ListControl和树控件TreeControl

  • Post author:
  • Post category:其他






列表控件


列表相关的类

:CListCtrl-

父类是


CWnd


,本质是一个控件;

CListView-

父类是


CView


,本质是一个视图。相当于在视图中嵌入了一个


CListCtrl


控件


列表控件的使用

  • 1.1

    显示方式

    :LVS_ICON/LVS_SMALLICON/LVS_LIST/LVS_REPORT
  • 1.2

    图标

    :CListCtrl::SetImageList;CListCtrl::GetImageList
  • 1.3

    设置列

    :CListCtrl::InsertColumn
  • 1.4

    数据的添加

    :CListCtrl::InsertItem-

    插入数据项

    ;CListCtrl::SetItemText-

    设置数据项的文本
  • 1.5

    遍历数据项

    :CListCtrl::GetFirstSelectedItemPosition; CListCtrl::GetNextSelectedItem; CListCtrl::GetSelectedCount
  • 1.6

    数据项的删除

    :CListCtrl::DeleteItem-

    删除指定索引的数据项;

    CListCtrl::DeleteAllItems-

    删除所有项

    ;CListCtrl::DeleteColumn-

    删除指定列
  • 1.7

    查找

    :CListCtrl::FindItem
  • 1.8

    附加数据

    :CListCtrl::SetItemData;CListCtrl::GetItemData

实例:新建MFC基于对话框的工程MFCList;关键代码


  1. void

    CMFCListDlg::DoDataExchange(CDataExchange* pDX)
  2. {
  3. CDialogEx::DoDataExchange(pDX);
  4. DDX_Control(pDX, IDC_COMBO1, m_wndStyle);

    //添加下拉列表框的关联控件变量
  5. DDX_Control(pDX, IDC_LIST1, m_wndList);

    //添加列表控件的关联控件变量
  6. }
  7. BEGIN_MESSAGE_MAP(CMFCListDlg, CDialogEx)
  8. ON_WM_SYSCOMMAND()
  9. ON_WM_PAINT()
  10. ON_WM_QUERYDRAGICON()
  11. ON_BN_CLICKED(IDC_BUTTON_Seclect, &CMFCListDlg::OnBnClickedButtonSeclect)

    //”获取选择项”控件的双击事件
  12. ON_BN_CLICKED(IDC_BUTTON_Delete, &CMFCListDlg::OnBnClickedButtonDelete)

    //”删除选择项”控件 的双击事件
  13. ON_CBN_SELCHANGE(IDC_COMBO1, &CMFCListDlg::OnCbnSelchangeCombo1)

    //下拉列表框的选择项改变事件
  14. ON_NOTIFY(NM_DBLCLK, IDC_LIST1, &CMFCListDlg::OnNMDblclkList1)

    //列表控件的双击项事件
  15. END_MESSAGE_MAP()

  16. //BOOL CMFCListDlg::OnInitDialog()调用下列代码

  17. //初始化列表控件
  18. InitListCtrl();
  19. m_wndStyle.InsertString(0,L

    “图标(ICON)”

    );
  20. m_wndStyle.InsertString(1,L

    “小图标(SMALL ICON)”

    );
  21. m_wndStyle.InsertString(2,L

    “列表(LIST)”

    );
  22. m_wndStyle.InsertString(3,L

    “报表(REPORT)”

    );
  23. m_wndStyle.SetCurSel(0);

  24. //”获取选择项”控件的双击事件

  25. void

    CMFCListDlg::OnBnClickedButtonSeclect()
  26. {
  27. POSITION pos=m_wndList.GetFirstSelectedItemPosition();

  28. while

    (pos)
  29. {

  30. int

    nItem=m_wndList.GetNextSelectedItem(pos);
  31. MessageBox(m_wndList.GetItemText(nItem,0));

    //显示数据项的文本
  32. }
  33. }

  34. //”删除选择项”控件 的双击事件

  35. void

    CMFCListDlg::OnBnClickedButtonDelete()
  36. {
  37. LVFINDINFO info;
  38. info.flags=LVFI_STRING|LVFI_PARTIAL;
  39. info.psz=L

    “stu.data”

    ;

  40. int

    index=0;

  41. if

    ((index=m_wndList.FindItem(&info))!=-1)
  42. {
  43. m_wndList.DeleteItem(index);
  44. }
  45. }

  46. //下拉列表框的选择项改变事件

  47. void

    CMFCListDlg::OnCbnSelchangeCombo1()
  48. {

  49. int

    nSel=m_wndStyle.GetCurSel();

  50. switch

    (nSel)
  51. {

  52. case

    0:

    //图标
  53. m_wndList.ModifyStyle(LVS_SMALLICON|LVS_LIST|LVS_REPORT,LVS_ICON);

  54. break

    ;

  55. case

    1:

    //小图标
  56. m_wndList.ModifyStyle(LVS_ICON|LVS_LIST|LVS_REPORT,LVS_SMALLICON);

  57. break

    ;

  58. case

    2:

    //列表
  59. m_wndList.ModifyStyle(LVS_SMALLICON|LVS_ICON|LVS_REPORT,LVS_LIST);

  60. break

    ;

  61. case

    3:

    //报表
  62. m_wndList.ModifyStyle(LVS_ICON|LVS_LIST|LVS_SMALLICON,LVS_REPORT);

  63. break

    ;
  64. }
  65. }

  66. //列表控件的双击项事件

  67. void

    CMFCListDlg::OnNMDblclkList1(NMHDR *pNMHDR,

    LRESULT

    *pResult)
  68. {
  69. LPNMITEMACTIVATE pNMItemActivate =

    reinterpret_cast

    <LPNMITEMACTIVATE>(pNMHDR);

  70. // TODO: 在此添加控件通知处理程序代码

  71. int

    nSel=pNMItemActivate->iItem;

  72. if

    (-1!=nSel)
  73. {
  74. CString* pPath=(CString*)m_wndList.GetItemData(nSel);

  75. //MessageBox(*pPath);
  76. ShowFile(*pPath+L

    “\\*.*”

    );
  77. }
  78. *pResult = 0;
  79. }

  80. void

    CMFCListDlg::InitListCtrl()
  81. {

  82. //1 设置控件的图标列表
  83. CImageList* pILNormal=

    new

    CImageList;
  84. pILNormal->Create(IDB_NORMAL,32,1,RGB(0,0,0));
  85. m_wndList.SetImageList(pILNormal,LVSIL_NORMAL);

  86. //2 设置控件的小图标
  87. CImageList* pILSmall=

    new

    CImageList;
  88. pILSmall->Create(IDB_SMALL,18,1,RGB(0,0,0));
  89. m_wndList.SetImageList(pILSmall,LVSIL_SMALL);

  90. //3 设置列
  91. m_wndList.InsertColumn(0,L

    “名称”

    ,LVCFMT_LEFT,150);
  92. m_wndList.InsertColumn(1,L

    “大小”

    ,LVCFMT_LEFT,80);
  93. m_wndList.InsertColumn(2,L

    “类型”

    ,LVCFMT_LEFT,80);
  94. m_wndList.InsertColumn(3,L

    “修改日期”

    ,LVCFMT_LEFT,100);

  95. //4 插入测试数据
  96. m_wndList.InsertItem(0,L

    “文件”

    ,1);
  97. m_wndList.SetItemText(0,1,L

    “15”

    );
  98. m_wndList.SetItemText(0,2,L

    “文件”

    );
  99. m_wndList.SetItemText(0,3,L

    “2012-03-06”

    );
  100. m_wndList.InsertItem(1,L

    “文件夹”

    ,0);
  101. m_wndList.SetItemText(1,1,L

    “12”

    );
  102. m_wndList.SetItemText(1,2,L

    “文件夹”

    );
  103. m_wndList.SetItemText(1,3,L

    “2011-03-06”

    );

  104. //5 显示C盘根目录下的所有文件和文件夹

  105. //ShowFile(L”c:\\”);

  106. //6 设置控件的背景色
  107. m_wndList.SetBkColor(RGB(200,200,200));
  108. m_wndList.SetTextColor(RGB(255,0,0));
  109. m_wndList.SetTextBkColor(CLR_NONE);

  110. //7 设置控件的背景图片
  111. m_wndList.SetBkImage(L

    “res\\MFCList.ico”

    );
  112. }

  113. void

    CMFCListDlg::ShowFile(CString strPath)
  114. {

  115. //添加数据前,先清空原有数据
  116. m_wndList.DeleteAllItems();

  117. int

    index=0;

    //添加数据项的索引
  118. CFileFind find;

  119. BOOL

    bRet=find.FindFile(strPath+

    “*.*”

    );

  120. while

    (bRet)
  121. {
  122. bRet=find.FindNextFileW();

  123. if

    (find.IsDirectory())
  124. {
  125. m_wndList.InsertItem(index,find.GetFileName(),0);
  126. m_wndList.SetItemText(index,2,L

    “文件夹”

    );
  127. }

  128. else
  129. {
  130. m_wndList.InsertItem(index,find.GetFileName(),1);

  131. DWORD

    nLen=find.GetLength();
  132. CString strLen;
  133. strLen.Format(L

    “%d”

    ,nLen);
  134. m_wndList.SetItemText(index,1,strLen);
  135. }

  136. //保存路径
  137. CString* pPath=

    new

    CString;
  138. *pPath=find.GetFilePath();
  139. m_wndList.SetItemData(index,(

    DWORD

    )pPath);
  140. index++;
  141. }
  142. find.Close();

    //结束查找
  143. }
void CMFCListDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO1, m_wndStyle);//添加下拉列表框的关联控件变量
	DDX_Control(pDX, IDC_LIST1, m_wndList);//添加列表控件的关联控件变量
}

BEGIN_MESSAGE_MAP(CMFCListDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_Seclect, &CMFCListDlg::OnBnClickedButtonSeclect)//"获取选择项"控件的双击事件
	ON_BN_CLICKED(IDC_BUTTON_Delete, &CMFCListDlg::OnBnClickedButtonDelete)//"删除选择项"控件 的双击事件
	ON_CBN_SELCHANGE(IDC_COMBO1, &CMFCListDlg::OnCbnSelchangeCombo1)//下拉列表框的选择项改变事件
	ON_NOTIFY(NM_DBLCLK, IDC_LIST1, &CMFCListDlg::OnNMDblclkList1)//列表控件的双击项事件
END_MESSAGE_MAP()
//BOOL CMFCListDlg::OnInitDialog()调用下列代码
//初始化列表控件
InitListCtrl();
m_wndStyle.InsertString(0,L"图标(ICON)");
m_wndStyle.InsertString(1,L"小图标(SMALL ICON)");
m_wndStyle.InsertString(2,L"列表(LIST)");
m_wndStyle.InsertString(3,L"报表(REPORT)");
m_wndStyle.SetCurSel(0);

//"获取选择项"控件的双击事件
void CMFCListDlg::OnBnClickedButtonSeclect()
{ 
	POSITION pos=m_wndList.GetFirstSelectedItemPosition();
	while(pos)
	{
		int nItem=m_wndList.GetNextSelectedItem(pos);
		MessageBox(m_wndList.GetItemText(nItem,0));//显示数据项的文本
	}
}
//"删除选择项"控件 的双击事件
void CMFCListDlg::OnBnClickedButtonDelete()
{
	LVFINDINFO info;
	info.flags=LVFI_STRING|LVFI_PARTIAL;
	info.psz=L"stu.data";
	int index=0;
	if ((index=m_wndList.FindItem(&info))!=-1)
	{
		m_wndList.DeleteItem(index);
	}
}

//下拉列表框的选择项改变事件
void CMFCListDlg::OnCbnSelchangeCombo1()
{ 
	int nSel=m_wndStyle.GetCurSel();
	switch (nSel)
	{
	case 0://图标
		m_wndList.ModifyStyle(LVS_SMALLICON|LVS_LIST|LVS_REPORT,LVS_ICON);
		break;
	case 1://小图标
		m_wndList.ModifyStyle(LVS_ICON|LVS_LIST|LVS_REPORT,LVS_SMALLICON);
		break;
	case 2://列表
		m_wndList.ModifyStyle(LVS_SMALLICON|LVS_ICON|LVS_REPORT,LVS_LIST);
		break;
	case 3://报表
		m_wndList.ModifyStyle(LVS_ICON|LVS_LIST|LVS_SMALLICON,LVS_REPORT);
		break;
	}
} 
//列表控件的双击项事件
void CMFCListDlg::OnNMDblclkList1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	int nSel=pNMItemActivate->iItem;
	if (-1!=nSel)
	{
		CString* pPath=(CString*)m_wndList.GetItemData(nSel);
		//MessageBox(*pPath);
		ShowFile(*pPath+L"\\*.*");
	}
	*pResult = 0;
}

void CMFCListDlg::InitListCtrl()
{
	//1 设置控件的图标列表
	CImageList* pILNormal=new CImageList;
	pILNormal->Create(IDB_NORMAL,32,1,RGB(0,0,0));
	m_wndList.SetImageList(pILNormal,LVSIL_NORMAL);
	//2 设置控件的小图标
	CImageList* pILSmall=new CImageList;
	pILSmall->Create(IDB_SMALL,18,1,RGB(0,0,0));
	m_wndList.SetImageList(pILSmall,LVSIL_SMALL);
	//3 设置列
	m_wndList.InsertColumn(0,L"名称",LVCFMT_LEFT,150);
	m_wndList.InsertColumn(1,L"大小",LVCFMT_LEFT,80);
	m_wndList.InsertColumn(2,L"类型",LVCFMT_LEFT,80);
	m_wndList.InsertColumn(3,L"修改日期",LVCFMT_LEFT,100);
	//4 插入测试数据
	m_wndList.InsertItem(0,L"文件",1);
	m_wndList.SetItemText(0,1,L"15");
	m_wndList.SetItemText(0,2,L"文件");
	m_wndList.SetItemText(0,3,L"2012-03-06");

	m_wndList.InsertItem(1,L"文件夹",0);
	m_wndList.SetItemText(1,1,L"12");
	m_wndList.SetItemText(1,2,L"文件夹");
	m_wndList.SetItemText(1,3,L"2011-03-06");
	//5 显示C盘根目录下的所有文件和文件夹
	//ShowFile(L"c:\\");
	//6 设置控件的背景色
	m_wndList.SetBkColor(RGB(200,200,200));
	m_wndList.SetTextColor(RGB(255,0,0));
	m_wndList.SetTextBkColor(CLR_NONE);
	//7 设置控件的背景图片
	m_wndList.SetBkImage(L"res\\MFCList.ico");
}

void CMFCListDlg::ShowFile(CString strPath)
{
	//添加数据前,先清空原有数据
	m_wndList.DeleteAllItems();
	int index=0;//添加数据项的索引
	CFileFind find;
	BOOL bRet=find.FindFile(strPath+"*.*");
	while (bRet)
	{
		bRet=find.FindNextFileW();
		if (find.IsDirectory())
		{
			m_wndList.InsertItem(index,find.GetFileName(),0);
			m_wndList.SetItemText(index,2,L"文件夹");
		}
		else
		{
			m_wndList.InsertItem(index,find.GetFileName(),1);
			DWORD nLen=find.GetLength();
			CString strLen;
			strLen.Format(L"%d",nLen);
			m_wndList.SetItemText(index,1,strLen);
		}
		//保存路径
		CString* pPath=new CString;
		*pPath=find.GetFilePath();
		m_wndList.SetItemData(index,(DWORD)pPath);
		index++;
	}
	find.Close();//结束查找
}

运行结果:



树控件


树控件相关类

:CTreeCtrl-

父类是


CWnd


,本质上是一个控件

;CTreeView –

父类是


CView


,本质是一个视图。


CTreeView


是在


CView


中嵌入了一个


CTreeCtrl


控件

树控件的

使用

  • 2.1

    设置图标

    :CTreeCtrl::SetImageList;CTreeCtrl::GetImageList
  • 2.2

    增加节点

    :CTreeCtrl::InserItem

    增加节点,并返回节点句柄
  • 2.3

    获取节点:

    CTreeCtrl::GetSelectedItem-

    获取选择的节点;

    CTreeCtrl::GetParentItem-

    获取节点的父节点

    ;CTreeCtrl::GetRootItem-

    获取根节点

  • 2.4

    判断

    :CTreeCtrl::ItemHasChildren-

    判断节点是否有子节点
  • 2.5

    删除节点

    :CTreeCtrl::DeleteItem-

    删除指定节点

    ;CTreeCtrl::DeleteAllItems-

    删除所有节点
  • 2.6

    附加数据

    :CTreeCtrl::SetItemData;CTreeCtrl::GetItemData

实例:新建MFC基于对话框的工程MFCTree;关键代码


  1. void

    CMFCTreeDlg::DoDataExchange(CDataExchange* pDX)
  2. {
  3. CDialogEx::DoDataExchange(pDX);
  4. DDX_Control(pDX, IDC_TREE1, m_wndTree);

    //增加树控件的关联控件变量
  5. }
  6. BEGIN_MESSAGE_MAP(CMFCTreeDlg, CDialogEx)
  7. ON_WM_SYSCOMMAND()
  8. ON_WM_PAINT()
  9. ON_WM_QUERYDRAGICON()
  10. ON_NOTIFY(NM_DBLCLK, IDC_TREE1, &CMFCTreeDlg::OnNMDblclkTree1)

    //双击树控件项的事件
  11. END_MESSAGE_MAP()

  12. //初始化树控件

  13. void

    CMFCTreeDlg::InitTreeControl()
  14. {

  15. //1.设置图标列表
  16. CImageList* pilNormal=

    new

    CImageList;

  17. BOOL

    bs=pilNormal->Create(IDB_NORMAL,16,1,RGB(1,0,0));

    //IDB_NORMAL是 bitmap资源 64 x 16

  18. if

    (bs)
  19. {
  20. m_wndTree.SetImageList(pilNormal,TVSIL_NORMAL);
  21. }

  22. else
  23. {
  24. AfxMessageBox(L

    “adsfads”

    );
  25. }
  26. m_wndTree.SetItemHeight(20);

    //设置行高
  27. HTREEITEM hRoot=m_wndTree.InsertItem(L

    “C:\\”

    ,0,2);

    //插入根目录
  28. ShowFile(L

    “c:\\”

    ,hRoot);
  29. }

  30. void

    CMFCTreeDlg::ShowFile(CString strPath,HTREEITEM hParent)
  31. {

  32. if

    (m_wndTree.ItemHasChildren(hParent))
  33. {

  34. return

    ;
  35. }
  36. CFileFind find;

  37. BOOL

    bRet=find.FindFile(strPath+

    “\\*.*”

    );

  38. while

    (bRet)
  39. {
  40. HTREEITEM hItem;
  41. bRet=find.FindNextFileW();
  42. CString strName=find.GetFileName();

  43. if

    (!find.IsDots())
  44. {

  45. if

    (find.IsDirectory())
  46. {
  47. hItem=m_wndTree.InsertItem(strName,0,2,hParent);
  48. }

  49. else
  50. {
  51. hItem=m_wndTree.InsertItem(strName,1,3,hParent);
  52. }

  53. //保存路径
  54. CString *pPath=

    new

    CString;
  55. *pPath=find.GetFilePath();
  56. m_wndTree.SetItemData(hItem,(

    DWORD

    )pPath);
  57. }
  58. }
  59. find.Close();

    //结束查找
  60. m_wndTree.Expand(hParent,TVE_EXPAND);

    //展开根节点
  61. }

  62. //树控件项的双击事件

  63. void

    CMFCTreeDlg::OnNMDblclkTree1(NMHDR *pNMHDR,

    LRESULT

    *pResult)
  64. {
  65. HTREEITEM hCurItem=m_wndTree.GetSelectedItem();

  66. if

    (hCurItem==NULL || hCurItem==m_wndTree.GetRootItem())
  67. {

  68. return

    ;
  69. }
  70. CString* pPath=(CString*)m_wndTree.GetItemData(hCurItem);

  71. //AfxMessageBox(*pPath);
  72. ShowFile(*pPath,hCurItem);
  73. *pResult = 0;
  74. }
void CMFCTreeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_TREE1, m_wndTree);//增加树控件的关联控件变量
}

BEGIN_MESSAGE_MAP(CMFCTreeDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()  
	ON_NOTIFY(NM_DBLCLK, IDC_TREE1, &CMFCTreeDlg::OnNMDblclkTree1)//双击树控件项的事件
END_MESSAGE_MAP()

//初始化树控件
void CMFCTreeDlg::InitTreeControl()
{
	//1.设置图标列表
	CImageList* pilNormal=new CImageList;
	BOOL bs=pilNormal->Create(IDB_NORMAL,16,1,RGB(1,0,0)); //IDB_NORMAL是 bitmap资源 64 x 16
	if (bs)
	{
		m_wndTree.SetImageList(pilNormal,TVSIL_NORMAL);
	}
	else
	{
		AfxMessageBox(L"adsfads");
	}
	m_wndTree.SetItemHeight(20);//设置行高
	HTREEITEM hRoot=m_wndTree.InsertItem(L"C:\\",0,2);//插入根目录
	ShowFile(L"c:\\",hRoot);
}
void CMFCTreeDlg::ShowFile(CString strPath,HTREEITEM hParent)
{
	if (m_wndTree.ItemHasChildren(hParent))
	{
		return;
	}
	CFileFind find;
	BOOL bRet=find.FindFile(strPath+"\\*.*");
	while(bRet)
	{
		HTREEITEM hItem;
		bRet=find.FindNextFileW();
		CString strName=find.GetFileName();
		if (!find.IsDots())
		{
			if (find.IsDirectory())
			{
				hItem=m_wndTree.InsertItem(strName,0,2,hParent);
			}
			else
			{
				hItem=m_wndTree.InsertItem(strName,1,3,hParent);
			}
			//保存路径
			CString *pPath=new CString;
			*pPath=find.GetFilePath();
			m_wndTree.SetItemData(hItem,(DWORD)pPath);
		}
	}
	find.Close();//结束查找
	m_wndTree.Expand(hParent,TVE_EXPAND);//展开根节点
}	 
//树控件项的双击事件
void CMFCTreeDlg::OnNMDblclkTree1(NMHDR *pNMHDR, LRESULT *pResult)
{ 
	HTREEITEM hCurItem=m_wndTree.GetSelectedItem();	
	if (hCurItem==NULL || hCurItem==m_wndTree.GetRootItem())
	{
		return;
	}
	CString* pPath=(CString*)m_wndTree.GetItemData(hCurItem);
	//AfxMessageBox(*pPath);
	ShowFile(*pPath,hCurItem);
	*pResult = 0;
}

运行结果: