SDI中使用多个视
OutlookMultiViews

App中InitInstance()
文档模板创建后 ((CMainFrame*)m_pMainWnd)->InitViews ();
建立多个视类CView1、CView2、CView3和自身的COutlookMultiViewsView
CMainFrame类
#define NUMVIEWS 4
CBCGPOutlookBar m_wndShortcutsBar;
CBCGPOutlookBarPane m_wndShortcutsPane1;
OnCreate()
if (!CreateShortcutsBar ())
{
TRACE0("Failed to create shortcuts bar\n");
return -1; // fail to create
}
//----------------------------------------------------
// Outlook bar is created and docking on the left side
// should be allowed.
//----------------------------------------------------
EnableDocking (CBRS_ALIGN_LEFT);
if (!m_wndCaptionBar.Create (WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, this,
ID_VIEW_CAPTIONBAR, -1))
{
TRACE0("Failed to create caption bar\n");
return -1; // fail to create
}
m_wndCaptionBar.SetFlatBorder ();
m_wndCaptionBar.SetText (_T("View 1"));
m_bmpCaption.LoadBitmap (IDB_CAPTION);
m_wndCaptionBar.SetBitmap (m_bmpCaption, RGB (255, 0, 255));
void CMainFrame::OnViewOutlookBar()
{
ShowControlBar (&m_wndShortcutsBar,
!(m_wndShortcutsBar.IsVisible ()),
FALSE, TRUE);
RecalcLayout ();
}
void CMainFrame::OnUpdateViewOutlookBar(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck (m_wndShortcutsBar.IsVisible ());
}
void CMainFrame::OnOutlookBarShortcut(UINT id)
{
const int nIndex = id - ID_SHORTCUT_1;
ASSERT( nIndex >=0 && nIndex < NUMVIEWS );
CView* pNewView = m_pViews[nIndex];
CView* pActiveView =GetActiveView();
if ( !pActiveView ) // No currently active view
return;
if ( pNewView == pActiveView ) // Already there
return;
m_nCurView = nIndex; // Store the new current view's index
// exchange view window ID's so RecalcLayout() works
UINT temp = ::GetWindowLong(pActiveView->m_hWnd, GWL_ID);
::SetWindowLong(pActiveView->m_hWnd, GWL_ID,
::GetWindowLong(pNewView->m_hWnd, GWL_ID));
::SetWindowLong(pNewView->m_hWnd, GWL_ID, temp);
// Display and update the new current view - hide the old one
pActiveView->ShowWindow(SW_HIDE);
pNewView->ShowWindow(SW_SHOW);
SetActiveView(pNewView);
RecalcLayout();
pNewView->Invalidate();
CString strCaption;
strCaption.Format (_T("View %d"), nIndex + 1);
m_wndCaptionBar.SetText (strCaption);
m_wndCaptionBar.RedrawWindow ();
}
void CMainFrame::OnUpdateOutlookBarShortcut(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck (pCmdUI->m_nID == ID_SHORTCUT_1 + m_nCurView);
}
void CMainFrame::OnViewCaptionBar()
{
ShowControlBar (&m_wndCaptionBar,
!(m_wndCaptionBar.IsVisible ()),
FALSE, TRUE);
RecalcLayout ();
}
void CMainFrame::OnUpdateViewCaptionBar(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck (m_wndCaptionBar.IsVisible ());
}
BOOL CMainFrame::CreateShortcutsBar ()
{
CBCGPOutlookWnd::EnableAnimation ();
const int nInitialWidth = 150;
const CString strCaption = _T("Shortcuts");
if (!m_wndShortcutsBar.Create (strCaption, this,
CRect (0, 0, nInitialWidth, nInitialWidth),
ID_VIEW_OUTLOOKBAR, WS_CHILD | WS_VISIBLE | CBRS_LEFT))
{
TRACE0("Failed to create outlook bar\n");
return FALSE; // fail to create
}
CBCGPOutlookWnd* pShortcutsBarContainer = DYNAMIC_DOWNCAST (CBCGPOutlookWnd,
m_wndShortcutsBar.GetUnderlinedWindow ());
if (pShortcutsBarContainer == NULL)
{
TRACE0("Cannot get outlook bar container\n");
return FALSE;
}
CImageList images;
images.Create (IDB_SHORTCUTS, 32, 0, RGB (255, 0, 255));
// Create first page:
m_wndShortcutsPane1.Create (&m_wndShortcutsBar, dwDefaultToolbarStyle, ID_SHORTCUTS_PANE_1);
m_wndShortcutsPane1.SetOwner (this);
m_wndShortcutsPane1.EnableTextLabels ();
m_wndShortcutsPane1.EnableDocking (CBRS_ALIGN_ANY);
m_wndShortcutsPane1.AddButton (images.ExtractIcon (0), _T("View 1"), ID_SHORTCUT_1);
m_wndShortcutsPane1.AddButton (images.ExtractIcon (1), _T("View 2"), ID_SHORTCUT_2);
m_wndShortcutsPane1.AddButton (images.ExtractIcon (2), _T("View 3"), ID_SHORTCUT_3);
m_wndShortcutsPane1.AddButton (images.ExtractIcon (3), _T("View 4"), ID_SHORTCUT_4);
pShortcutsBarContainer->AddTab (&m_wndShortcutsPane1, _T("Views"), (UINT)-1, FALSE);
return TRUE;
}
void CMainFrame::InitViews ()
{
m_nCurView = 0; // Save index of the currently active view class
CView* pActiveView = GetActiveView();
m_pViews[0] = pActiveView;
m_pViews[1] = (CView*) new CView1;
m_pViews[2] = (CView*) new CView2;
m_pViews[3] = (CView*) new CView3;
CDocument* pCurrentDoc = GetActiveDocument();
// Initialize a CCreateContext to point to the active document.
// With this context, the new view is added to the document
// when the view is created in CView::OnCreate().
CCreateContext newContext;
newContext.m_pNewViewClass = NULL;
newContext.m_pNewDocTemplate = NULL;
newContext.m_pLastView = NULL;
newContext.m_pCurrentFrame = NULL;
newContext.m_pCurrentDoc = pCurrentDoc;
CRect rect(0, 0, 0, 0); // gets resized later
for (int nView = 1; nView < NUMVIEWS; nView++)
{
// Create the new view. In this example, the view persists for
// the life of the application. The application automatically
// deletes the view when the application is closed.
m_pViews[nView]->Create(NULL, NULL,
(AFX_WS_DEFAULT_VIEW & ~WS_VISIBLE),
// views are created with the style of AFX_WS_DEFAULT_VIEW
// In MFC 4.0, this is (WS_BORDER | WS_VISIBLE | WS_CHILD)
rect, this,
AFX_IDW_PANE_FIRST + nView, &newContext);
// When a document template creates a view, the WM_INITIALUPDATE
// message is sent automatically. However, this code must
// explicitly send the message, as follows.
m_pViews [nView]->OnInitialUpdate();
}
}