列表控件
列表相关的类: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;关键代码
- 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()
-
-
- 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(pNMHDR);
-
- int nSel=pNMItemActivate->iItem;
- if (-1!=nSel)
- {
- CString* pPath=(CString*)m_wndList.GetItemData(nSel);
-
- ShowFile(*pPath+L”\\*.*”);
- }
- *pResult = 0;
- }
-
- void CMFCListDlg::InitListCtrl()
- {
-
- CImageList* pILNormal=new CImageList;
- pILNormal->Create(IDB_NORMAL,32,1,RGB(0,0,0));
- m_wndList.SetImageList(pILNormal,LVSIL_NORMAL);
-
- CImageList* pILSmall=new CImageList;
- pILSmall->Create(IDB_SMALL,18,1,RGB(0,0,0));
- m_wndList.SetImageList(pILSmall,LVSIL_SMALL);
-
- 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);
-
- 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”);
-
-
-
- m_wndList.SetBkColor(RGB(200,200,200));
- m_wndList.SetTextColor(RGB(255,0,0));
- m_wndList.SetTextBkColor(CLR_NONE);
-
- 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();
- }
- 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()
-
-
- 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(pNMHDR);
-
- int nSel=pNMItemActivate->iItem;
- if (-1!=nSel)
- {
- CString* pPath=(CString*)m_wndList.GetItemData(nSel);
-
- ShowFile(*pPath+L”\\*.*”);
- }
- *pResult = 0;
- }
-
- void CMFCListDlg::InitListCtrl()
- {
-
- CImageList* pILNormal=new CImageList;
- pILNormal->Create(IDB_NORMAL,32,1,RGB(0,0,0));
- m_wndList.SetImageList(pILNormal,LVSIL_NORMAL);
-
- CImageList* pILSmall=new CImageList;
- pILSmall->Create(IDB_SMALL,18,1,RGB(0,0,0));
- m_wndList.SetImageList(pILSmall,LVSIL_SMALL);
-
- 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);
-
- 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”);
-
-
-
- m_wndList.SetBkColor(RGB(200,200,200));
- m_wndList.SetTextColor(RGB(255,0,0));
- m_wndList.SetTextBkColor(CLR_NONE);
-
- 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();
- }
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(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;关键代码
- 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()
- {
-
- CImageList* pilNormal=new CImageList;
- BOOL bs=pilNormal->Create(IDB_NORMAL,16,1,RGB(1,0,0));
- 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);
-
- ShowFile(*pPath,hCurItem);
- *pResult = 0;
- }
- 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()
- {
-
- CImageList* pilNormal=new CImageList;
- BOOL bs=pilNormal->Create(IDB_NORMAL,16,1,RGB(1,0,0));
- 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);
-
- ShowFile(*pPath,hCurItem);
- *pResult = 0;
- }
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;
}
运行结果: