2014-06-10 21:28
3926人閱讀
評論(0)
收藏
舉報

分類:
最后界面
1、創(chuàng)建單文檔exe,支持切分窗口。

2、新建對話框資源

ID為IDD_TREEVIEW,Style=CHILD,BORDER=NONE,
刪掉按鈕OK和CANCEL,添加Tree控件IDC_TREE,占滿整個對話框
 
導(dǎo)入位圖資源,ID為IDB_BITMAP

新建列表對話框IDD_LISTCTRLVIEW,Style=CHILD,BORDER=NONE,
添加LISTCONTROL控件IDC_LIST,占滿藍色邊框,
 
和編輯視圖的對話框資源IDD_EDITVIEW,Style=CHILD,BORDER=NONE,刪掉上面的按鈕。
添加EditBox控件IDC_EDIT,占滿藍色邊界線
 
3、新建視圖類并添加控件型變量和初始化函數(shù)
建立2 個View 的類,這里我們讓這2個View 的類繼承于FormView,
CListControlView 繼承于FormView 關(guān)聯(lián)對話框 IDD_LISTVIEW,為了后面可以new 將構(gòu)造函數(shù)改為publlic屬性(默認為protected)
-
class CListControlView : public CFormView
-
{
-
public:
-
CListControlView(); // protected constructor used by dynamic creation
-
DECLARE_DYNCREATE(CListControlView)
為列邊框添加控件類型變量m_listCtrl

為對話框添加OnSize消息,使列表框與對話框等大
-
void CListControlView::OnSize(UINT nType, int cx, int cy)
-
{
-
CFormView::OnSize(nType, cx, cy);
-
-
// TODO: Add your message handler code here
-
CFormView::ShowScrollBar(SB_VERT,FALSE);
-
CFormView::ShowScrollBar(SB_HORZ,FALSE);
-
if (GetSafeHwnd())
-
{
-
if (m_listCtrl.GetSafeHwnd())
-
{
-
CRect rect(0,0,cx,cy);
-
m_listCtrl.MoveWindow(&rect);
-
}
-
}
-
}
添加虛函數(shù),初始化列表框
-
void CListControlView::OnInitialUpdate()
-
{
-
CFormView::OnInitialUpdate();
-
-
// TODO: Add your specialized code here and/or call the base class
-
CRect rect;
-
m_listCtrl.GetClientRect(&rect);
-
-
m_listCtrl.InsertColumn(0, "From", LVCFMT_LEFT, rect.Width()/4);
-
m_listCtrl.InsertColumn(1, "Subject", LVCFMT_LEFT, rect.Width()/4);
-
m_listCtrl.InsertColumn(2, "Date", LVCFMT_LEFT, rect.Width()/4);
-
m_listCtrl.InsertColumn(3, "Size", LVCFMT_LEFT, rect.Width()/4);
-
}
CEditControlView 繼承于FormView 關(guān)聯(lián)對話框IDD_EDITVIEW,為了后面可以new 將構(gòu)造函數(shù)改為publlic屬性(默認為protected)
-
class CEditControlView : public CFormView
-
{
-
public:
-
CEditControlView(); // protected constructor used by dynamic creation
為CEditControlView上的編輯控件添加控件型變量m_editCtrl

為對話框添加OnSize消息,使編輯框與對話框等大
-
void CEditControlView::OnSize(UINT nType, int cx, int cy)
-
{
-
CFormView::OnSize(nType, cx, cy);
-
-
// TODO: Add your message handler code here
-
CFormView::ShowScrollBar(SB_VERT,FALSE);
-
CFormView::ShowScrollBar(SB_HORZ,FALSE);
-
//編輯框與窗口大小一樣
-
if (GetSafeHwnd())
-
{
-
if (m_editCtrl.GetSafeHwnd())
-
{
-
CRect rect(0,0,cx,cy);
-
m_editCtrl.MoveWindow(&rect);
-
}
-
}
-
}
現(xiàn)在創(chuàng)建關(guān)聯(lián)樹控件的視圖類
CLeftPaneView : public CFormView關(guān)聯(lián)對話框 IDD_TREEVIEW。
為CLeftPaneView樹控件關(guān)聯(lián)一個控件類型的變量m_treeCtrl

為對話框添加OnSize消息,使樹控件與對話框等大
-
void CLeftPaneView::OnSize(UINT nType, int cx, int cy)
-
{
-
CFormView::OnSize(nType, cx, cy);
-
-
// TODO: Add your message handler code here
-
if (GetSafeHwnd())
-
{
-
CRect rect;
-
GetClientRect(&rect);
-
if (m_treeCtrl.GetSafeHwnd())
-
m_treeCtrl.MoveWindow(&rect);
-
}
-
}
添加OnInitialUpdate()虛函數(shù),初始化樹控件
-
void CLeftPaneView::OnInitialUpdate()
-
{
-
CFormView::OnInitialUpdate();
-
-
// TODO: Add your specialized code here and/or call the base class
-
m_ImageList.Create(IDB_BITMAP, 16, 1, RGB(255, 0, 255));
-
m_treeCtrl.SetImageList(&m_ImageList, LVSIL_NORMAL);
-
-
m_hSplitterView = m_treeCtrl.InsertItem("Splitter View", 0, 0);
-
m_hListCtrlView = m_treeCtrl.InsertItem("ListCtrl View", 1, 1);
-
m_hEditView = m_treeCtrl.InsertItem("EditCtrl View", 2, 2);
-
}
-
4、創(chuàng)建一個切分窗口類
其實質(zhì)是在切分窗口添加編輯視圖和列表視圖對象。
源文件中添加
-
#include "ListControlView.h"
-
#include "EditControlView.h"
為了后面可以new CSplitterView將構(gòu)造函數(shù)改為publlic屬性(默認為protected)
-
class CSplitterView : public CView
-
{
-
public: // changed from protected
-
CSplitterView(); // protected constructor used by dynamic creation
-
//
-
//
-
};
-
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(CListControlView), CSize(150,0), pContext);
-
m_wndSplitter.CreateView(1,0,RUNTIME_CLASS(CEditControlView), CSize(0,0), pContext);
-
-
return 0;
-
}
添加成員變量
-
// Attributes
-
public:
-
CSplitterWnd m_wndSplitter;
-
添加WM_CREATE、WM_SIZE消息響應(yīng)
-
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;
-
}
-
-
void CSplitterView::OnSize(UINT nType, int cx, int cy)
-
{
-
CView::OnSize(nType, cx, cy);
-
-
// TODO: Add your message handler code here
-
m_wndSplitter.MoveWindow(-2,-2,cx+4,cy+4);
-
m_wndSplitter.SetRowInfo(0, cy-(cy/4), 0);
-
m_wndSplitter.SetRowInfo(1, cy/4, 20);
-
m_wndSplitter.RecalcLayout();
-
-
}
-
5、創(chuàng)建右側(cè)切分窗口的框架類
因為所有視圖都要放在框架窗口中,所以,右面板需要一個框架窗口。
新建框架窗口類CRightPaneFrame,繼承自CFrameWnd。
為了在框架中調(diào)用不同視圖類,添加成員變量,并定義視圖ID
-
#include "SplitterView.h"
-
#include "ListControlView.h"
-
#include "EditControlView.h"
-
-
#define VIEW_SPLITTER 1
-
#define VIEW_LISTCTRL 2
-
#define VIEW_EDIT 3
-
public:
-
CSplitterView* m_pSplitterView;
-
CListControlView* m_pListCtrlView;
-
CEditControlView* m_pEditCtrlView;
-
UINT m_nCurrentViewID;
添加虛函數(shù)
-
BOOL CRightPaneFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
-
{
-
// TODO: Add your specialized code here and/or call the base class
-
// TODO: Add your specialized code here and/or call the base class
-
-
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 CListControlView;
-
((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 CEditControlView;
-
((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;
-
// return CFrameWnd::OnCreateClient(lpcs, pContext);
-
}
添加切換視圖的函數(shù)
public:
void SwitchToView(UINT nView);
-
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)
-
{
-
// don't switch when views are the same
-
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();//調(diào)整框架窗口
-
}
-
}
-
-
注意:
pNewActiveView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
實際上AFX_IDW_PANE_FIRST是為了解決多個VIEW的情況下消息轉(zhuǎn)發(fā)的問題,這是MFC內(nèi)部使用的一個固定的值,所有當(dāng)前的活動視圖都會切換到AFX_IDW_PANE_FIRST這個ID,主窗口收到的一些消息(比如命令、通知等等消息)會轉(zhuǎn)發(fā)給活動視圖來處理,框架通過這個ID來定位活動視圖。
當(dāng)在某個SDI應(yīng)用程序中使用多個視圖并支持視圖切換時,很容易忽略這點,造成某些消息得不到正確的響應(yīng),
因此當(dāng)激活某個視圖時也要把這個視圖的ID改成AFX_IDW_PANE_FIRST。
當(dāng)主框架調(diào)整窗口布局時,即調(diào)用RecalcLayout()這個函數(shù)時,會將ID為AFX_IDW_PANE_FIRST的窗口/視(必須可見)作為最后一個視圖進行拉伸以填充剩余的區(qū)域。
6.左面版添加視圖類成員變量
在左面版也即樹視中添加成員變量,包含右面板和樹項
-
// Attributes
-
public:
-
CImageList m_ImageList;
-
HTREEITEM m_hSplitterView;
-
HTREEITEM m_hListCtrlView;
-
HTREEITEM m_hEditView;
-
-
CRightPaneFrame* m_pRightPaneFrame;
頭文件class CLeftPaneView : public CFormView前加前置聲明,以便使用VIEW_SPLITTER等宏定義
在源文件加
-
#include "RightPaneFrame.h"
添加當(dāng)樹形控件選項改變時消息響應(yīng),右邊視圖變化

-
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;
-
}
7、修改MainFrame,創(chuàng)建靜態(tài)切分窗口
在源文件中添加
-
#include "LeftPaneView.h"
-
#include "RightPaneFrame.h"
-
修改OnCreateClient函數(shù)
-
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
-
{
-
// TODO: Add your specialized code here and/or call the base class
-
if (!m_wndSplitter.CreateStatic(this, 1, 2))
-
{
-
TRACE0("Failed to create splitter window\n");
-
return FALSE;
-
}
-
-
// Get the client rect first for calc left pane size
-
CRect rect;
-
GetClientRect(&rect);
-
-
// create the left tree view first.
-
if (!m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CLeftPaneView), CSize(rect.Width()/3, 0), pContext))
-
{
-
TRACE0("Failed to create left pane view\n");
-
return FALSE;
-
}
-
-
// The right pane is a frame which and contain several different views.
-
// The is can be set to active or non-active
-
if (!m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CRightPaneFrame), CSize(0, 0), pContext))
-
{
-
TRACE0("Failed to create right pane frame\n");
-
return FALSE;
-
}
-
-
CLeftPaneView* pLeftPaneView = (CLeftPaneView*) m_wndSplitter.GetPane(0,0);
-
pLeftPaneView->m_pRightPaneFrame = (CRightPaneFrame*) m_wndSplitter.GetPane(0,1);
-
-
// Set the left pane as the active view
-
SetActiveView((CView*) m_wndSplitter.GetPane(0, 0));
-
-
return TRUE;
-
}
-
|