www.gusucode.com > VC++的窗体TabView标签切换实现-源码程序 > VC++的窗体TabView标签切换实现-源码程序/code/MainFrm.cpp

    // MainFrm.cpp : implementation of the CMainFrame class
// Download by http://www.codefans.net

#include "stdafx.h"
#include "Tab2.h"

#include "MainFrm.h"

#include "MyView1.h"
#include "MyView2.h"
#include "MyView3.h "

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
    ON_MESSAGE( WM_MYMESSAGE, OnExample )
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	if (!m_wndToolBar.Create(this) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Remove this if you don't want tool tips or a resizeable toolbar
	m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
		CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFrameWnd::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
{
    if(!m_TabView.CreateStatic(this))
	{
        TRACE0("failed to createpropertysheet\n");
        return FALSE;
	}  
    CRuntimeClass* pMyViewClass = RUNTIME_CLASS(CMyView1);
    m_TabView.SetTab(0);

    CSize size(160, 180);

    if (!m_TabView.CreateView(pMyViewClass, size, pContext))
	{
        TRACE0("failed to create first pane\n");
        return FALSE;
	}

    SetActiveView(m_TabView.GetActiveView());
	
    return TRUE;
}

void CMainFrame::OnExample(UINT nCmdID)
{
    if (nCmdID == m_nCurrentExample)
    return; // already selected

    // set the child window id of the active view to afx_idw_pane_first.
    // this is necessary so that cframewnd::recalclayout will allocate
    // this "first pane" to that portion of the frame window's client
    // area not allocated to control bars. set the child id of
    // the previously active view to some other id; we will use the
    // command id as the child id.

    CView* pOldActiveView = GetActiveView();
    ::SetWindowLong(pOldActiveView->m_hWnd, GWL_ID, m_nCurrentExample);

    CRuntimeClass* CMyView2;
    switch (nCmdID)
    {
        case ID_VIEW1:
            CMyView2 = RUNTIME_CLASS(CMyView1);
            break;

        case ID_VIEW2:
            CMyView2 = RUNTIME_CLASS(CMyView2);
            break;

        case ID_VIEW3:
            CMyView2 = RUNTIME_CLASS(CMyView3);
            break;

        default:
           ASSERT(0);
           return;
    }

    // create the new view
    CCreateContext Context;
    Context.m_pNewViewClass = CMyView2;
    Context.m_pCurrentDoc = GetActiveDocument();

    // new code below
    CView* pNewView = m_TabView.CreateView(CMyView2, CSize(100,100), &Context/*相关上下文*/);

    if (pNewView != NULL)
	{
        // the new view is there, but invisible and not active...
        pNewView->ShowWindow(SW_SHOW);
        pNewView->OnInitialUpdate();
        SetActiveView(pNewView);
        m_TabView.RecalcLayout(); //<-- new code
        //RecalcLayout();
        m_nCurrentExample = nCmdID;
    
        // finally destroy the old view...
        pOldActiveView->DestroyWindow();
	}
}