本文简单地介绍下VS的treeview使用:包括属性设置、单击事件、双击事件、右键弹出菜单等。
使用工具:VS2008
使用语言:C++
开发步骤:
void CTreeViewDemoView::OnInitialUpdate() { CTreeView::OnInitialUpdate(); //属性 CTreeCtrl& tree = GetTreeCtrl(); DWORD dwStyle = GetWindowLong(tree,GWL_STYLE); dwStyle |= TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT | TVS_SHOWSELALWAYS; SetWindowLong(tree,GWL_STYLE,dwStyle); //菜单 m_menu.LoadMenu(IDR_MENU1); tree.SetMenu(&m_menu); //图标 m_ImageList.Create(IDB_BITMAP1,10,10,RGB(255,255,255)); tree.SetImageList(&m_ImageList,LVSIL_NORMAL); //字体 CFont font; font.CreateFont( 15, 0, 0, 0, FW_NORMAL, FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, _T("宋体")); tree.SetFont(&font); //更新界面 UpdateTree(); }
void CTreeViewDemoView::UpdateTree(void) { CTreeCtrl& tree = GetTreeCtrl(); tree.DeleteAllItems(); CString str = _T(""); int i=0, cnt = 0; HTREEITEM root,sub; str = _T("View1"); root = tree.InsertItem(str,0,0,TVI_ROOT); tree.SetItemData(root,VIEW1_ROOT); m_TreeItem1 = root; cnt = 2; for(i=0; i<cnt; i++) { str.Format(_T("View1 %d"),i); sub = tree.InsertItem(str,1,1,m_TreeItem1); tree.SetItemData(sub,VIEW1_SUB); } tree.Expand(m_TreeItem1,TVE_EXPAND); str = _T("View2"); root = tree.InsertItem(str,2,2,TVI_ROOT); tree.SetItemData(root,VIEW2_ROOT); m_TreeItem2 = root; cnt = 3; for(i=0; i<cnt; i++) { str.Format(_T("View2 %d"),i); sub = tree.InsertItem(str,3,3,m_TreeItem2); tree.SetItemData(sub,VIEW2_SUB); } tree.Expand(m_TreeItem2,TVE_EXPAND); }
void CTreeViewDemoView::OnNMClick(NMHDR *pNMHDR, LRESULT *pResult) { // TODO: 在此添加控件通知处理程序代码 CTreeCtrl& tree = GetTreeCtrl(); CPoint CursorPoint; if(!GetCursorPos(&CursorPoint)) { return; } tree.ScreenToClient(&CursorPoint); UINT uFlag=0; HTREEITEM select_item = tree.HitTest(CursorPoint,&uFlag); if((NULL != select_item) && (TVHT_ONITEM&uFlag)) { tree.Select(select_item,TVGN_CARET); } else { return; } ShowView(select_item); *pResult = 0; } void CTreeViewDemoView::ShowView(HTREEITEM select_item) { CTreeCtrl& tree = GetTreeCtrl(); DWORD dwData = tree.GetItemData(select_item); CString sText = tree.GetItemText(select_item); CString str = _T(""); switch(dwData) { case VIEW1_ROOT: str = _T("您点击了:") + sText; AfxMessageBox(str); break; case VIEW1_SUB: str = _T("您点击了:") + sText; AfxMessageBox(str); break; case VIEW2_ROOT: str = _T("您点击了:") + sText; AfxMessageBox(str); break; case VIEW2_SUB: str = _T("您点击了:") + sText; AfxMessageBox(str); break; default: break; } }
void CTreeViewDemoView::OnNMDblclk(NMHDR *pNMHDR, LRESULT *pResult) { // TODO: 在此添加控件通知处理程序代码 CTreeCtrl& tree = GetTreeCtrl(); CPoint CursorPoint; if(!GetCursorPos(&CursorPoint)) { return; } tree.ScreenToClient(&CursorPoint); UINT uFlag=0; HTREEITEM select_item = tree.HitTest(CursorPoint,&uFlag); if((NULL != select_item) && (TVHT_ONITEM&uFlag)) { tree.Select(select_item,TVGN_CARET); } else { return; } ShowDlg(select_item); *pResult = 0; } void CTreeViewDemoView::ShowDlg(HTREEITEM select_item) { CTreeCtrl& tree = GetTreeCtrl(); DWORD dwData = tree.GetItemData(select_item); CString sText = tree.GetItemText(select_item); CString str = _T(""); switch(dwData) { case VIEW1_ROOT: str = _T("您双击了双击了:") + sText; AfxMessageBox(str); break; case VIEW1_SUB: str = _T("您双击了双击了:") + sText; AfxMessageBox(str); break; case VIEW2_ROOT: str = _T("您双击了双击了:") + sText; AfxMessageBox(str); break; case VIEW2_SUB: str = _T("您双击了双击了:") + sText; AfxMessageBox(str); break; default: break; } }
void CTreeViewDemoView::OnNMRClick(NMHDR *pNMHDR, LRESULT *pResult) { // TODO: 在此添加控件通知处理程序代码 CTreeCtrl& tree = GetTreeCtrl(); CPoint CursorPoint; if(!GetCursorPos(&CursorPoint)) { return; } tree.ScreenToClient(&CursorPoint); UINT uFlag=0; HTREEITEM select_item = tree.HitTest(CursorPoint,&uFlag); if((NULL != select_item) && (TVHT_ONITEM&uFlag)) { tree.Select(select_item,TVGN_CARET); } else { return; } ShowMenu(select_item); *pResult = 0; } void CTreeViewDemoView::ShowMenu(HTREEITEM select_item) { CTreeCtrl& tree = GetTreeCtrl(); DWORD dwData = tree.GetItemData(select_item); CString sText = tree.GetItemText(select_item); CMenu* pSubMenu = NULL; switch(dwData) { case VIEW1_ROOT: pSubMenu = m_menu.GetSubMenu(0); break; case VIEW1_SUB: pSubMenu = m_menu.GetSubMenu(0); break; case VIEW2_ROOT: pSubMenu = m_menu.GetSubMenu(1); break; case VIEW2_SUB: pSubMenu = m_menu.GetSubMenu(1); break; default: break; } if(pSubMenu) { CPoint CurSorPoint; GetCursorPos(&CurSorPoint); pSubMenu->TrackPopupMenu(TPM_LEFTALIGN,CurSorPoint.x,CurSorPoint.y,this); } } void CTreeViewDemoView::OnTest11() { // TODO: 在此添加命令处理程序代码 CString str = _T("菜单第一项第一个"); AfxMessageBox(str); } void CTreeViewDemoView::OnTest12() { // TODO: 在此添加命令处理程序代码 CString str = _T("菜单第一项第二个"); AfxMessageBox(str); } void CTreeViewDemoView::OnTest21() { // TODO: 在此添加命令处理程序代码 CString str = _T("菜单第二项第一个"); AfxMessageBox(str); } void CTreeViewDemoView::OnTest22() { // TODO: 在此添加命令处理程序代码 CString str = _T("菜单第二项第二个"); AfxMessageBox(str); }
TreeViewDemoView.h
// TreeViewDemoView.h : CTreeViewDemoView 类的接口 // #pragma once //View1 #define VIEW1_ROOT 0x10 #define VIEW1_SUB 0x11 //View2 #define VIEW2_ROOT 0x20 #define VIEW2_SUB 0x21 class CTreeViewDemoView : public CTreeView { protected: // 仅从序列化创建 CTreeViewDemoView(); DECLARE_DYNCREATE(CTreeViewDemoView) // 属性 public: CTreeViewDemoDoc* GetDocument() const; // 操作 public: // 重写 public: virtual BOOL PreCreateWindow(CREATESTRUCT& cs); protected: virtual void OnInitialUpdate(); // 构造后第一次调用 // 实现 public: virtual ~CTreeViewDemoView(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: // 生成的消息映射函数 protected: DECLARE_MESSAGE_MAP() public: void UpdateTree(void); private: CImageList m_ImageList;//图标 CMenu m_menu;//菜单 HTREEITEM m_TreeItem1; HTREEITEM m_TreeItem2; public: afx_msg void OnNMClick(NMHDR *pNMHDR, LRESULT *pResult); void ShowView(HTREEITEM select_item); afx_msg void OnNMDblclk(NMHDR *pNMHDR, LRESULT *pResult); void ShowDlg(HTREEITEM select_item); afx_msg void OnNMRClick(NMHDR *pNMHDR, LRESULT *pResult); void ShowMenu(HTREEITEM select_item); afx_msg void OnTest11(); afx_msg void OnTest12(); afx_msg void OnTest21(); afx_msg void OnTest22(); }; #ifndef _DEBUG // TreeViewDemoView.cpp 中的调试版本 inline CTreeViewDemoDoc* CTreeViewDemoView::GetDocument() const { return reinterpret_cast<CTreeViewDemoDoc*>(m_pDocument); } #endifTreeViewDemoView.cpp
// TreeViewDemoView.cpp : CTreeViewDemoView 类的实现 // #include "stdafx.h" #include "TreeViewDemo.h" #include "TreeViewDemoDoc.h" #include "TreeViewDemoView.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // CTreeViewDemoView IMPLEMENT_DYNCREATE(CTreeViewDemoView, CTreeView) BEGIN_MESSAGE_MAP(CTreeViewDemoView, CTreeView) ON_NOTIFY_REFLECT(NM_CLICK, &CTreeViewDemoView::OnNMClick) ON_NOTIFY_REFLECT(NM_DBLCLK, &CTreeViewDemoView::OnNMDblclk) ON_NOTIFY_REFLECT(NM_RCLICK, &CTreeViewDemoView::OnNMRClick) ON_COMMAND(ID_TEST11, &CTreeViewDemoView::OnTest11) ON_COMMAND(ID_TEST12, &CTreeViewDemoView::OnTest12) ON_COMMAND(ID_TEST21, &CTreeViewDemoView::OnTest21) ON_COMMAND(ID_TEST22, &CTreeViewDemoView::OnTest22) END_MESSAGE_MAP() // CTreeViewDemoView 构造/析构 CTreeViewDemoView::CTreeViewDemoView() { // TODO: 在此处添加构造代码 } CTreeViewDemoView::~CTreeViewDemoView() { } BOOL CTreeViewDemoView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: 在此处通过修改 // CREATESTRUCT cs 来修改窗口类或样式 return CTreeView::PreCreateWindow(cs); } void CTreeViewDemoView::OnInitialUpdate() { CTreeView::OnInitialUpdate(); //属性 CTreeCtrl& tree = GetTreeCtrl(); DWORD dwStyle = GetWindowLong(tree,GWL_STYLE); dwStyle |= TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT | TVS_SHOWSELALWAYS; SetWindowLong(tree,GWL_STYLE,dwStyle); //菜单 m_menu.LoadMenu(IDR_MENU1); tree.SetMenu(&m_menu); //图标 m_ImageList.Create(IDB_BITMAP1,10,10,RGB(255,255,255)); tree.SetImageList(&m_ImageList,LVSIL_NORMAL); //字体 CFont font; font.CreateFont( 15, 0, 0, 0, FW_NORMAL, FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, _T("宋体")); tree.SetFont(&font); //更新界面 UpdateTree(); } // CTreeViewDemoView 诊断 #ifdef _DEBUG void CTreeViewDemoView::AssertValid() const { CTreeView::AssertValid(); } void CTreeViewDemoView::Dump(CDumpContext& dc) const { CTreeView::Dump(dc); } CTreeViewDemoDoc* CTreeViewDemoView::GetDocument() const // 非调试版本是内联的 { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTreeViewDemoDoc))); return (CTreeViewDemoDoc*)m_pDocument; } #endif //_DEBUG // CTreeViewDemoView 消息处理程序 void CTreeViewDemoView::UpdateTree(void) { CTreeCtrl& tree = GetTreeCtrl(); tree.DeleteAllItems(); CString str = _T(""); int i=0, cnt = 0; HTREEITEM root,sub; str = _T("View1"); root = tree.InsertItem(str,0,0,TVI_ROOT); tree.SetItemData(root,VIEW1_ROOT); m_TreeItem1 = root; cnt = 2; for(i=0; i<cnt; i++) { str.Format(_T("View1 %d"),i); sub = tree.InsertItem(str,1,1,m_TreeItem1); tree.SetItemData(sub,VIEW1_SUB); } tree.Expand(m_TreeItem1,TVE_EXPAND); str = _T("View2"); root = tree.InsertItem(str,2,2,TVI_ROOT); tree.SetItemData(root,VIEW2_ROOT); m_TreeItem2 = root; cnt = 3; for(i=0; i<cnt; i++) { str.Format(_T("View2 %d"),i); sub = tree.InsertItem(str,3,3,m_TreeItem2); tree.SetItemData(sub,VIEW2_SUB); } tree.Expand(m_TreeItem2,TVE_EXPAND); } void CTreeViewDemoView::OnNMClick(NMHDR *pNMHDR, LRESULT *pResult) { // TODO: 在此添加控件通知处理程序代码 CTreeCtrl& tree = GetTreeCtrl(); CPoint CursorPoint; if(!GetCursorPos(&CursorPoint)) { return; } tree.ScreenToClient(&CursorPoint); UINT uFlag=0; HTREEITEM select_item = tree.HitTest(CursorPoint,&uFlag); if((NULL != select_item) && (TVHT_ONITEM&uFlag)) { tree.Select(select_item,TVGN_CARET); } else { return; } ShowView(select_item); *pResult = 0; } void CTreeViewDemoView::ShowView(HTREEITEM select_item) { CTreeCtrl& tree = GetTreeCtrl(); DWORD dwData = tree.GetItemData(select_item); CString sText = tree.GetItemText(select_item); CString str = _T(""); switch(dwData) { case VIEW1_ROOT: str = _T("您点击了:") + sText; AfxMessageBox(str); break; case VIEW1_SUB: str = _T("您点击了:") + sText; AfxMessageBox(str); break; case VIEW2_ROOT: str = _T("您点击了:") + sText; AfxMessageBox(str); break; case VIEW2_SUB: str = _T("您点击了:") + sText; AfxMessageBox(str); break; default: break; } } void CTreeViewDemoView::OnNMDblclk(NMHDR *pNMHDR, LRESULT *pResult) { // TODO: 在此添加控件通知处理程序代码 CTreeCtrl& tree = GetTreeCtrl(); CPoint CursorPoint; if(!GetCursorPos(&CursorPoint)) { return; } tree.ScreenToClient(&CursorPoint); UINT uFlag=0; HTREEITEM select_item = tree.HitTest(CursorPoint,&uFlag); if((NULL != select_item) && (TVHT_ONITEM&uFlag)) { tree.Select(select_item,TVGN_CARET); } else { return; } ShowDlg(select_item); *pResult = 0; } void CTreeViewDemoView::ShowDlg(HTREEITEM select_item) { CTreeCtrl& tree = GetTreeCtrl(); DWORD dwData = tree.GetItemData(select_item); CString sText = tree.GetItemText(select_item); CString str = _T(""); switch(dwData) { case VIEW1_ROOT: str = _T("您双击了双击了:") + sText; AfxMessageBox(str); break; case VIEW1_SUB: str = _T("您双击了双击了:") + sText; AfxMessageBox(str); break; case VIEW2_ROOT: str = _T("您双击了双击了:") + sText; AfxMessageBox(str); break; case VIEW2_SUB: str = _T("您双击了双击了:") + sText; AfxMessageBox(str); break; default: break; } } void CTreeViewDemoView::OnNMRClick(NMHDR *pNMHDR, LRESULT *pResult) { // TODO: 在此添加控件通知处理程序代码 CTreeCtrl& tree = GetTreeCtrl(); CPoint CursorPoint; if(!GetCursorPos(&CursorPoint)) { return; } tree.ScreenToClient(&CursorPoint); UINT uFlag=0; HTREEITEM select_item = tree.HitTest(CursorPoint,&uFlag); if((NULL != select_item) && (TVHT_ONITEM&uFlag)) { tree.Select(select_item,TVGN_CARET); } else { return; } ShowMenu(select_item); *pResult = 0; } void CTreeViewDemoView::ShowMenu(HTREEITEM select_item) { CTreeCtrl& tree = GetTreeCtrl(); DWORD dwData = tree.GetItemData(select_item); CString sText = tree.GetItemText(select_item); CMenu* pSubMenu = NULL; switch(dwData) { case VIEW1_ROOT: pSubMenu = m_menu.GetSubMenu(0); break; case VIEW1_SUB: pSubMenu = m_menu.GetSubMenu(0); break; case VIEW2_ROOT: pSubMenu = m_menu.GetSubMenu(1); break; case VIEW2_SUB: pSubMenu = m_menu.GetSubMenu(1); break; default: break; } if(pSubMenu) { CPoint CurSorPoint; GetCursorPos(&CurSorPoint); pSubMenu->TrackPopupMenu(TPM_LEFTALIGN,CurSorPoint.x,CurSorPoint.y,this); } } void CTreeViewDemoView::OnTest11() { // TODO: 在此添加命令处理程序代码 CString str = _T("菜单第一项第一个"); AfxMessageBox(str); } void CTreeViewDemoView::OnTest12() { // TODO: 在此添加命令处理程序代码 CString str = _T("菜单第一项第二个"); AfxMessageBox(str); } void CTreeViewDemoView::OnTest21() { // TODO: 在此添加命令处理程序代码 CString str = _T("菜单第二项第一个"); AfxMessageBox(str); } void CTreeViewDemoView::OnTest22() { // TODO: 在此添加命令处理程序代码 CString str = _T("菜单第二项第二个"); AfxMessageBox(str); }
好了,关于VS的treeview使用就到这了。
源码下载