📄 tabbed_views2.shtml
字号:
That's about it, you should now have a version of the MFC collect sample
which uses my CTabCtrlView Class. Now for a few words on the
class. It consists of two main parts. A wrapper window which gets
set as the main frame's client and the TabCtrl itself. It is the
job of the wrapper window to resize the TabCtrl and the view to keep everything
in its proper place. The window also handles the initialization and handling
of the Tabs for the TabCtrl. The code for this class follows:
<BR>
<PRE><TT><FONT COLOR="#990000">
#if !defined(AFX_CTabCtrlView_H__8E652EC1_5159_11D1_96A0_0E6B8A000000__INCLUDED_)
#define AFX_CTabCtrlView_H__8E652EC1_5159_11D1_96A0_0E6B8A000000__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
// CTabCtrlView.h : header file
//
#include "afxcmn.h"
class CTabCtrlView;
class CViewTabCtl : public CTabCtrl
{
// Construction
public:
CViewTabCtl();
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CViewTabCtl)
//}}AFX_VIRTUAL
// Implementation
public:
void SetView(CTabCtrlView* pView);
virtual void RecalcLayout(CRect& rect, CWnd* wnd);
virtual ~CViewTabCtl();
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
// Generated message map functions
protected:
CTabCtrlView* m_pView;
CString m_sGrayFont;
CString m_sSelFont;
CDC m_dc;
CFont m_selFont;
virtual BOOL HandleTabs(int sel);
//{{AFX_MSG(CViewTabCtl)
afx_msg void OnSelchange(NMHDR* pNMHDR, LRESULT* pResult);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// CCTabCtrlView window
// The actual tab control
// Wrapper Window Handles Interfacing to MFC doc View Model
// and resizing of the Tab Ctrl
class CTabCtrlView : public CWnd
{
// Construction
public:
CTabCtrlView();
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCTabCtrlView)
//}}AFX_VIRTUAL
// Implementation
public:
virtual BOOL HandleTabs(int sel);
void SetTab(int Tab);
virtual void SetView();
void RecalcLayout();
CView* GetActiveView();
BOOL CreateStatic(CWnd* pParentWnd, DWORD dwStyle = WS_CHILD | WS_VISIBLE, UINT nID = AFX_IDW_PANE_FIRST);
virtual CView* CreateView(CRuntimeClass* pViewClass, SIZE sizeInit, CCreateContext* pContext );
virtual ~CTabCtrlView();
// Generated message map functions
protected:
virtual void InitTabs(CTabCtrlView* pView);
CViewTabCtl m_TabCtl;
CView* m_ActiveView;
BOOL CreateCommon(CWnd* pParentWnd, SIZE sizeMin, DWORD dwStyle, UINT nID);
//{{AFX_MSG(CCTabCtrlView)
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnPaint();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// CViewTabCtl window
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_CTabCtrlView_H__8E652EC1_5159_11D1_96A0_0E6B8A000000__INCLUDED_)
/////////////////////////////////////////////////////////////////////////////
C++ Code file
/////////////////////////////////////////////////////////////////////////////
// CTabCtrlView.cpp : implementation file
//
// core headers
#include "afx.h"
#include "afxplex_.h"
#include "afxcoll.h"
#include "afxcmn.h"
#include "stdafx.h"
#include "TabCtrlView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define CX_BORDER 1
#define CY_BORDER 1
#define MAX_COLORS 10
// The following colors give a Tab Ctrl an OS/2 look
COLORREF colorRef[MAX_COLORS] =
{
RGB(0,225,255),
RGB(0,240,190),
RGB(128,128,255),
RGB(240,200,175),
RGB(240,240,150),
RGB(175,130,175),
RGB(240,140,0),
RGB(255,200,0),
RGB(255,160,120),
RGB(255,200, 175)
};
/////////////////////////////////////////////////////////////////////////////
// CTabCtrlView
CTabCtrlView::CTabCtrlView()
{
}
CTabCtrlView::~CTabCtrlView()
{
}
BEGIN_MESSAGE_MAP(CTabCtrlView, CWnd)
//{{AFX_MSG_MAP(CTabCtrlView)
ON_WM_SIZE()
ON_WM_ERASEBKGND()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTabCtrlView message handlers
BOOL CTabCtrlView::CreateStatic(CWnd * pParentWnd, DWORD dwStyle, UINT nID)
{
ASSERT(pParentWnd != NULL);
ASSERT(dwStyle & WS_CHILD);
ASSERT(!(dwStyle & SPLS_DYNAMIC_SPLIT));
// create with zero minimum pane size
if (!CreateCommon(pParentWnd, CSize(0, 0), dwStyle, nID))
return FALSE;
// all panes must be created with explicit calls to CreateView
return TRUE;
}
BOOL CTabCtrlView::CreateCommon(CWnd * pParentWnd, SIZE sizeMin, DWORD dwStyle, UINT nID)
{
ASSERT(pParentWnd != NULL);
ASSERT(sizeMin.cx >= 0 && sizeMin.cy >= 0);
ASSERT(dwStyle & WS_CHILD);
ASSERT(nID != 0);
// create with the same wnd-class as MDI-Frame (no erase bkgnd)
if (!CreateEx(0, NULL, NULL, dwStyle, 0, 0, 0, 0,
pParentWnd->m_hWnd, (HMENU)nID, NULL))
return FALSE; // create invisible
//Create the Tab Control
CRect rect;
GetClientRect(rect);
CImageList pImageList;
m_TabCtl.Create(WS_VISIBLE | WS_CHILD | TCS_OWNERDRAWFIXED, rect, this, nID);
//Overide this function to provide your Tabs
InitTabs(this);
return TRUE;
}
CView* CTabCtrlView::CreateView(CRuntimeClass * pViewClass, SIZE sizeInit, CCreateContext * pContext)
{
#ifdef _DEBUG
ASSERT_VALID(this);
ASSERT(pViewClass != NULL);
ASSERT(pViewClass->IsDerivedFrom(RUNTIME_CLASS(CWnd)));
ASSERT(AfxIsValidAddress(pViewClass, sizeof(CRuntimeClass), FALSE));
#endif
BOOL bSendInitialUpdate = FALSE;
CCreateContext contextT;
if (pContext == NULL)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -