⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tabsheetview.cpp

📁 VC++中Tab View的多种实现方法
💻 CPP
字号:
// MyTabDemoView.cpp : implementation of the CTabSheetView class
//

#include "stdafx.h"
#include "MyTabDemo.h"

#include "TabSheetDoc.h"
#include "TabSheetView.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

/////////////////////////////////////////////////////////////////////////////
// CTabSheetView

IMPLEMENT_DYNCREATE(CTabSheetView, CView)

BEGIN_MESSAGE_MAP(CTabSheetView, CView)
	//{{AFX_MSG_MAP(CTabSheetView)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)

    ON_MESSAGE(WM_TABCHANGED, OnTabChanged)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTabSheetView construction/destruction
void CTabSheetView::OnTabChanged(LPVOID pParam)
{
    m_ucViewNo=(unsigned char)pParam;
	CWnd* pWnd=GetWindow(GW_CHILD);
    for(unsigned char i=1;i<m_ucViewNo;i++)
	{
        pWnd=pWnd->GetWindow(GW_HWNDNEXT); 
	}
    CView * pActiveView=(CView *)pWnd; 
}

CTabSheetView::CTabSheetView()
{
	// TODO: add construction code here
    m_bIsFirstView=TRUE;//判断第一个加入的View
}

CTabSheetView::~CTabSheetView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTabSheetView drawing

void CTabSheetView::OnDraw(CDC* pDC)
{
	CTabSheetDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CTabSheetView diagnostics

#ifdef _DEBUG
void CTabSheetView::AssertValid() const
{
	CView::AssertValid();
}

void CTabSheetView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CTabSheetDoc* CTabSheetView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTabSheetDoc)));
	return (CTabSheetDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTabSheetView message handlers

void CTabSheetView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();

    //将标签窗口附着到主窗口上
    m_Sheet.Attach(this);
 
    CCreateContext Context;
	Context.m_pCurrentDoc=((CFrameWnd*)GetParent())->GetActiveDocument();
    
	AddView("MyView 1",RUNTIME_CLASS(CMyView1),&Context);
	AddView("MyView 2",RUNTIME_CLASS(CMyView2),&Context);
	AddView("MyView 3",RUNTIME_CLASS(CMyView3),&Context);
}

BOOL CTabSheetView::AddView(LPCTSTR lpszLabel, CRuntimeClass *pViewClass, CCreateContext *pContext)
{	

#ifdef _DEBUG
	ASSERT_VALID(this);
	ASSERT(pViewClass != NULL);
	ASSERT(pViewClass->IsDerivedFrom(RUNTIME_CLASS(CWnd)));
	ASSERT(AfxIsValidAddress(pViewClass, sizeof(CRuntimeClass), FALSE));
#endif

	CCreateContext context;
	if (pContext == NULL)
	{
		// if no context specified, generate one from the currently selected
		//  client if possible
		CView* pOldView = NULL;
		if (pOldView != NULL && pOldView->IsKindOf(RUNTIME_CLASS(CView)))
		{
			// set info about last pane
			ASSERT(context.m_pCurrentFrame == NULL);
			context.m_pLastView = pOldView;
			context.m_pCurrentDoc = pOldView->GetDocument();
			if (context.m_pCurrentDoc != NULL)
				context.m_pNewDocTemplate =
				context.m_pCurrentDoc->GetDocTemplate();
		}
		pContext = &context;
	}
	else
	{
        pContext->m_pNewViewClass=pViewClass;
	}

	CWnd* pWnd;
	TRY
	{
		pWnd = (CWnd*)pViewClass->CreateObject();
		if (pWnd == NULL)
			AfxThrowMemoryException();
	}
	CATCH_ALL(e)
	{
		TRACE0("Out of memory creating a view.\n");
		// Note: DELETE_EXCEPTION(e) not required
		return FALSE;
	}
	END_CATCH_ALL
		
    ASSERT_KINDOF(CWnd, pWnd);
	ASSERT(pWnd->m_hWnd == NULL);       // not yet created
	
	DWORD dwStyle = AFX_WS_DEFAULT_VIEW;//(WS_CHILD|WS_VISIBLE|WS_BORDER)
	CRect rect;
	// Create with the right size and position
	if (!pWnd->Create(NULL, NULL, dwStyle, rect, this, 0, pContext))
	{
		TRACE0("Warning: couldn't create client pane for view.\n");
		AfxMessageBox("Warning: couldn't create view");
		// pWnd will be cleaned up by PostNcDestroy
		return FALSE;
	}
	else
	{   //给每个view加窗口标题
		pWnd->SetWindowText(lpszLabel);
	}

	m_pActiveView = (CView*) pWnd;


    if (!m_bIsFirstView)
	{
		pWnd->EnableWindow(FALSE);
		pWnd->ShowWindow(SW_HIDE);
	}
	else
	{
		//将第一个加入View窗口设为Active
        m_bIsFirstView=FALSE;
		((CFrameWnd *)GetParent())->SetActiveView((CView *)m_pActiveView);
    }

	return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -