//////////////////////////////////////////////// BOOL CViewExApp::InitInstance() { …………………………… // simple text output view AddDocTemplate(new CMultiDocTemplate(IDR_TEXTTYPE, RUNTIME_CLASS(CMainDoc), RUNTIME_CLASS(CMDIChildWnd), RUNTIME_CLASS(CTextView))); // simple color output view AddDocTemplate(new CMultiDocTemplate(IDR_COLORTYPE, RUNTIME_CLASS(CMainDoc), RUNTIME_CLASS(CMDIChildWnd), RUNTIME_CLASS(CColorView))); // form view with input AddDocTemplate(new CMultiDocTemplate(IDR_INPUTTYPE, RUNTIME_CLASS(CMainDoc), RUNTIME_CLASS(CMDIChildWnd), RUNTIME_CLASS(CInputView))); // splitter frame with both simple text output and form input view AddDocTemplate(new CMultiDocTemplate(IDR_SPLIT2TYPE, RUNTIME_CLASS(CMainDoc), RUNTIME_CLASS(CSplitterFrame), RUNTIME_CLASS(CTextView))); // 3-way splitter frame with form input, text output and color output views AddDocTemplate(new CMultiDocTemplate(IDR_SPLIT3TYPE, RUNTIME_CLASS(CMainDoc), RUNTIME_CLASS(C3WaySplitterFrame), RUNTIME_CLASS(CInputView))); CMDIFrameWnd* pMainFrame = new CMDIFrameWnd; if (!pMainFrame->LoadFrame(IDR_MAINFRAME)) return FALSE; // Now finally show the main menu pMainFrame->ShowWindow(m_nCmdShow); pMainFrame->UpdateWindow(); m_pMainWnd = pMainFrame; OnFileNew(); return TRUE; }
void CTextView::OnDraw(CDC* pDC) { CMainDoc* pDoc = GetDocument(); CRect rect; GetClientRect(rect); pDC->SetTextAlign(TA_BASELINE | TA_CENTER); pDC->SetTextColor(pDoc->m_colorData); pDC->SetBkMode(TRANSPARENT); // center in the window pDC->TextOut(rect.Width() / 2, rect.Height() / 2, pDoc->m_strData, pDoc->m_strData.GetLength()); }
int CTextView::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message) { // side-step CView's implementation since we don't want to activate // this view return CWnd::OnMouseActivate(pDesktopWnd, nHitTest, message); } ////////////////////////// CColorView IMPLEMENT_DYNCREATE(CColorView, CView) BEGIN_MESSAGE_MAP(CColorView, CView) //{{AFX_MSG_MAP(CColorView) ON_WM_MOUSEACTIVATE() //}}AFX_MSG_MAP END_MESSAGE_MAP() // CColorView construction/destruction CColorView::CColorView() {} CColorView::~CColorView() {}
void CColorView::OnDraw(CDC* pDC) { CMainDoc* pDoc = GetDocument(); CRect rect; GetClientRect(rect); // fill the view with the specified color CBrush br(pDoc->m_colorData); pDC->FillRect(rect, &br); }
int CColorView::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message) { // side-step CView's implementation since we don't want to activate // this view return CWnd::OnMouseActivate(pDesktopWnd, nHitTest, message); }
void CColorView::OnActivateView(BOOL, CView*, CView*) { ASSERT(FALSE); // output only view - should never be active } ///////////////////////////////////////// splitter.h文件; // CSplitterFrame frame with splitter/wiper class CSplitterFrame : public CMDIChildWnd { DECLARE_DYNCREATE(CSplitterFrame) protected: CSplitterFrame(); // protected constructor used by dynamic creation // Attributes protected: CSplitterWnd m_wndSplitter; // Implementation public: virtual ~CSplitterFrame(); virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext); // Generated message map functions //{{AFX_MSG(CSplitterFrame) //}}AFX_MSG DECLARE_MESSAGE_MAP() };
class CViewExSplitWnd : public CSplitterWnd { DECLARE_DYNAMIC(CViewExSplitWnd) // Implementation public: CViewExSplitWnd(); ~CViewExSplitWnd(); CWnd* GetActivePane(int* pRow = NULL, int* pCol = NULL); }; class C3WaySplitterFrame : public CMDIChildWnd { DECLARE_DYNCREATE(C3WaySplitterFrame) protected: C3WaySplitterFrame(); // protected constructor used by dynamic creation // Attributes protected: CViewExSplitWnd m_wndSplitter; CViewExSplitWnd m_wndSplitter2; // embedded in the first // Implementation public: virtual ~C3WaySplitterFrame(); virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext); // Generated message map functions //{{AFX_MSG(C3WaySplitterFrame) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; /////////////////////////////splitter.cpp文件; #include "stdafx.h" #include "viewex.h" #include "splitter.h" #ifdef _DEBUG #undef THIS_FILE static char BASED_CODE THIS_FILE[] = __FILE__; #endif // CSplitterFrame // Create a splitter window which splits an output text view and an input view // | // TEXT VIEW (CTextView) | INPUT VIEW (CInputView) // | IMPLEMENT_DYNCREATE(CSplitterFrame, CMDIChildWnd) CSplitterFrame::CSplitterFrame() {}
CSplitterFrame::~CSplitterFrame() {} BOOL CSplitterFrame::OnCreateClient(LPCREATESTRUCT, CCreateContext* pContext) { // create a splitter with 1 row, 2 columns if (!m_wndSplitter.CreateStatic(this, 1, 2)) { TRACE0("Failed to CreateStaticSplitter\n"); return FALSE; } // add the first splitter pane - the default view in column 0 if (!m_wndSplitter.CreateView(0, 0,pContext->m_pNewViewClass, CSize(130, 50), pContext)) { TRACE0("Failed to create first pane\n"); return FALSE; } // add the second splitter pane - an input view in column 1 if (!m_wndSplitter.CreateView(0, 1,RUNTIME_CLASS(CInputView), CSize(0, 0), pContext)) { TRACE0("Failed to create second pane\n"); return FALSE; } // activate the input view SetActiveView((CView*)m_wndSplitter.GetPane(0,1)); return TRUE; } BEGIN_MESSAGE_MAP(CSplitterFrame, CMDIChildWnd) //{{AFX_MSG_MAP(CSplitterFrame) //}}AFX_MSG_MAP END_MESSAGE_MAP() // C3WaySplitterFrame - just like CSplitterFrame except the input view is // the first pane, and there are two output views
// | Text View (CTextView) // INPUT VIEW (CInputView) |------------------------ // | Color View (CColorView) IMPLEMENT_DYNCREATE(C3WaySplitterFrame, CMDIChildWnd) C3WaySplitterFrame::C3WaySplitterFrame() {}
C3WaySplitterFrame::~C3WaySplitterFrame() {}
BOOL C3WaySplitterFrame::OnCreateClient(LPCREATESTRUCT lpcs,CCreateContext* pContext) { // create a splitter with 1 row, 2 columns if (!m_wndSplitter.CreateStatic(this, 1, 2)) { TRACE0("Failed to CreateStaticSplitter\n"); return FALSE; } // add the first splitter pane - the default view in column 0 if (!m_wndSplitter.CreateView(0, 0,pContext->m_pNewViewClass, CSize(200, 50), pContext)) { TRACE0("Failed to create first pane\n"); return FALSE; } // add the second splitter pane - which is a nested splitter with 2 rows if (!m_wndSplitter2.CreateStatic(&m_wndSplitter, // our parent window is the first splitter 2, 1, // the new splitter is 2 rows, 1 column WS_CHILD | WS_VISIBLE | WS_BORDER, // style, WS_BORDER is needed m_wndSplitter.IdFromRowCol(0, 1) )) // new splitter is in the first row, 2nd column of first splitter { TRACE0("Failed to create nested splitter\n"); return FALSE; } // now create the two views inside the nested splitter int cyText = max(lpcs->cy - 70, 20); // height of text pane if (!m_wndSplitter2.CreateView(0, 0,RUNTIME_CLASS(CTextView), CSize(0, cyText), pContext)) { TRACE0("Failed to create second pane\n"); return FALSE; } if (!m_wndSplitter2.CreateView(1, 0,RUNTIME_CLASS(CColorView), CSize(0, 0), pContext)) { TRACE0("Failed to create third pane\n"); return FALSE; } // it all worked, we now have two splitter windows which contain // three different views return TRUE; } BEGIN_MESSAGE_MAP(C3WaySplitterFrame, CMDIChildWnd) //{{AFX_MSG_MAP(C3WaySplitterFrame) //}}AFX_MSG_MAP END_MESSAGE_MAP() IMPLEMENT_DYNAMIC(CViewExSplitWnd, CSplitterWnd) CViewExSplitWnd::CViewExSplitWnd() {}
CViewExSplitWnd::~CViewExSplitWnd() {}
CWnd* CViewExSplitWnd::GetActivePane(int* pRow, int* pCol) { ASSERT_VALID(this); // attempt to use active view of frame window CWnd* pView = NULL; CFrameWnd* pFrameWnd = GetParentFrame(); ASSERT_VALID(pFrameWnd); pView = pFrameWnd->GetActiveView(); // failing that, use the current focus if (pView == NULL) pView = GetFocus(); return pView; }