建立分割窗口
http://www.cnblogs.com/Lizheng0201/archive/2008/02/28/1399840.html
1 CMainFrame类添加虚方法OnCreateClient,见下面的代码
2 添加CSplitterWnd类对象m_wndSplitter
3 CDivisionView是自己创建的View类
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
m_wndSplitter.CreateStatic(this, 1, 2);
m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CDivisionView),CSize(200,200), pContext);
m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CDivisionView),CSize(200,200), pContext);
return TRUE;
}
一侧为树形视图,一侧为编辑区
http://blog.csdn.net/xs813/article/details/6462669
1 添加自己的CMyTreeView类(基类CTreeView)
2 在虚方法OnInitialUpdate实现树节点设置,见下面的代码
3 将上面的代码中分割窗口的加载类CDivisionView分别改成CMyTreeView和CEditView
void CMyTreeView::OnInitialUpdate()
{
CTreeView::OnInitialUpdate();
CTreeCtrl *ptheTree = &GetTreeCtrl();
ptheTree->ModifyStyle(0,TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS);
TVINSERTSTRUCT tvInsert;
HTREEITEM hTreeItem;
tvInsert.hInsertAfter = NULL;
tvInsert.hParent = TVI_ROOT;
tvInsert.item.mask = TVIF_TEXT;
tvInsert.item.pszText = "一年级";
hTreeItem = ptheTree->InsertItem(&tvInsert);
tvInsert.hParent = hTreeItem;
tvInsert.item.pszText = "1班";
ptheTree->InsertItem(&tvInsert);
tvInsert.item.pszText = "2班";
ptheTree->InsertItem(&tvInsert);
ptheTree->Expand(hTreeItem,TVE_EXPAND);
tvInsert.hParent = TVI_ROOT;
tvInsert.item.pszText = "二年级";
hTreeItem = ptheTree->InsertItem(&tvInsert);
tvInsert.hParent = hTreeItem;
tvInsert.item.pszText = "1班";
ptheTree->InsertItem(&tvInsert);
tvInsert.item.pszText = "2班";
ptheTree->InsertItem(&tvInsert);
tvInsert.item.pszText = "3班";
ptheTree->InsertItem(&tvInsert);
}
视图切换控制
http://www.pudn.com/downloads289/sourcecode/windows/dialog/detail1302472.html
仿照outlook的风格,左边树视图控制右边是分割窗口还是list视图或者edit视图
1 创建CLeftPaneView(基于CFormView):
对应的IDD增加IDC_TREE
增加成员变量 m_treeCtrl 对应IDC_TREE,用于控制IDC_TREE;
m_ImageList 显示tree视图的图标list
m_hSplitterView m_hListCtrlView m_EditView(HTREEITEM) 保存右边框架视图的对应选项
CRightPaneFrame *m_pRightPaneFrame 控制右边框架的视图分割
增加消息处理函数 OnSize 和 OnSelChangedTree
2 创建CListCtrlView 和 CEditView(基于CFormView),右边框架的两个分割视图
3 创建CSplitterView,用于右边框架创建分割视图。
4 创建CRightPaneFrame,增加成员变量,包括指向各个视图的指针和当前选中视图的ID
5 在CMainFrame::OnCreateClient方法中,对窗口进行左右分割,并保存相关变量
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
m_wndSplitter.CreateStatic(this, 1, 2)
CRect rect;
GetClientRect(&rect);
m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CLeftPaneView), CSize(rect.Width()/3, 0), pContext);
m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CRightPaneFrame), CSize(0, 0), pContext)
CLeftPaneView* pLeftPaneView = (CLeftPaneView*) m_wndSplitter.GetPane(0,0);
pLeftPaneView->m_pRightPaneFrame = (CRightPaneFrame*) m_wndSplitter.GetPane(0,1);
SetActiveView((CView*) m_wndSplitter.GetPane(0, 0));
return TRUE;
}
6 在CRightPaneFrame::OnCreateClient方法中,创建右边三个视图对象
BOOL CRightPaneFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
m_pSplitterView = new CSplitterView;
m_pSplitterView->Create(NULL, NULL, 0L, CFrameWnd::rectDefault, this, VIEW_SPLITTER, pContext);
SetActiveView(m_pSplitterView);
m_pSplitterView->ShowWindow(SW_SHOW);
m_pSplitterView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
m_nCurrentViewID = VIEW_SPLITTER;
m_pListCtrlView = new CListCtrlView;
((CView*) m_pListCtrlView)->Create(NULL, NULL, 0L, CFrameWnd::rectDefault, this, VIEW_LISTCTRL, pContext);
m_pListCtrlView->ShowWindow(SW_HIDE);
m_pListCtrlView->SetDlgCtrlID(VIEW_LISTCTRL);
m_pEditCtrlView = new CEditCtrlView;
((CView*) m_pEditCtrlView)->Create(NULL, NULL, 0L, CFrameWnd::rectDefault, this, VIEW_EDIT, pContext);
m_pEditCtrlView->ShowWindow(SW_HIDE);
m_pEditCtrlView->SetDlgCtrlID(VIEW_EDIT);
RecalcLayout();
return TRUE;
}
7 在CSplitterView::OnCreate方法中对右边框架进行分割
int CSplitterView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
m_wndSplitter.CreateStatic(this, 2, 1);
CCreateContext *pContext = (CCreateContext*) lpCreateStruct->lpCreateParams;
m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CListCtrlView), CSize(150,0), pContext);
m_wndSplitter.CreateView(1,0,RUNTIME_CLASS(CEditCtrlView), CSize(0,0), pContext);
return 0;
}
8 当左边树视图选项发生变化时
void CLeftPaneView::OnSelchangedTree(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
UINT nView = 0;
HTREEITEM hSelectedItem = m_treeCtrl.GetSelectedItem();
if (hSelectedItem == m_hSplitterView)
nView = VIEW_SPLITTER;
else
if (hSelectedItem == m_hListCtrlView)
nView = VIEW_LISTCTRL;
else
if (hSelectedItem == m_hEditView)
nView = VIEW_EDIT;
if (nView) m_pRightPaneFrame->SwitchToView(nView);
*pResult = 0;
}
void CRightPaneFrame::SwitchToView(UINT nView)
{
CView* pOldActiveView = GetActiveView();
CView* pNewActiveView = NULL;
switch (nView)
{
case VIEW_SPLITTER:
pNewActiveView = (CView*) m_pSplitterView;
break;
case VIEW_LISTCTRL:
pNewActiveView = (CView*) m_pListCtrlView;
break;
case VIEW_EDIT:
pNewActiveView = (CView*) m_pEditCtrlView;
break;
}
if (pNewActiveView)
{
if (pOldActiveView == pNewActiveView) return;
SetActiveView(pNewActiveView);
pNewActiveView->ShowWindow(SW_SHOW);
pNewActiveView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
pOldActiveView->ShowWindow(SW_HIDE);
pOldActiveView->SetDlgCtrlID(m_nCurrentViewID);
m_nCurrentViewID = nView;
RecalcLayout();
}
}