Visual C++ 7.1Visual C++ 7.0Windows 2003Windows 2000Visual C++ 6.0Windows XPMFC中级开发Visual StudioWindowsC++
轻松地在树视图和列表视图之间进行访问






2.18/5 (39投票s)
2003年6月21日
1分钟阅读

113751
在Windows资源管理器风格的应用程序中,从树形视图访问列表视图以及反之的简易方法。
引言
这是在Windows资源管理器风格的MFC应用程序中,从树形视图访问列表视图以及反之的简易方法。(在MFC应用程序向导的第6步的第5点中指定)
假设项目名称为“F”。
需要做什么?
- 创建一个具有Windows资源管理器风格的SDI应用程序,名称为“F”。
- 在
CLeftView
类中添加一个公共成员变量(指向CTreeView
类型的指针),命名为m_treeview
。其形式为CTreeView * m_treeview
。 - 在
CFView
类中(其中F是应用程序的名称),添加一个公共成员变量(指向CListView
类型的指针),命名为m_listview
。其形式为CListView * m_listview
。 - 在
CMainFrame
类中添加一个公共成员函数,类型为void
,命名为GetViews()
- 在函数中编写以下代码
void CMainFrame::GetViews() { //Code beginning //get the right view (the list view) CWnd* pWnd = m_wndSplitter.GetPane(0, 1); //cast the right view to the CFView type where F is //the name of Application and store it in list variable CFView* list= DYNAMIC_DOWNCAST(CFView, pWnd); //get the left view (the tree view) pWnd = m_wndSplitter.GetPane(0, 0); //cast it to CLeftView type and store it in tree variable CLeftView * tree = DYNAMIC_DOWNCAST(CLeftView, pWnd); //store the list view in the m_listview variable //(member of CLeftView class) tree->m_listview=list; //store the tree view in the m_treeview //variable (member of CFView class) list->m_treeview=tree; //end of code }
- 在
CMainFrame::OnCreate()
函数中,在“return true;
”之前添加GetViews();
那么如何使用这段代码?
要从树形视图访问列表视图,
- 在
CLeftView
类中的任何您想要发生的事件(单击、双击、选择)中,添加以下代码m_listview->GetListCtrl().InsertColumn(0,"Test");
要从列表视图访问树形视图,
- 在
CFView
类中的任何您想要发生的事件(单击、双击、选择)中,添加以下代码m_treeview->GetTreeCtrl().InsertItem("Test");
希望我能帮到你。