TabCtrlView.cpp
资源名称:TabSample.rar [点击查看]
上传用户:sz86035077
上传日期:2013-02-27
资源大小:40k
文件大小:4k
源码类别:
Tab控件
开发平台:
Visual C++
- // TabCtrlView.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Tab.h"
- #include "TabCtrlView.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CTabCtrlView
- CTabCtrlView::CTabCtrlView()
- {
- }
- CTabCtrlView::~CTabCtrlView()
- {
- }
- BEGIN_MESSAGE_MAP(CTabCtrlView, CWnd)
- //{{AFX_MSG_MAP(CTabCtrlView)
- ON_WM_SIZE()
- //}}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;
- 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);
- //去掉TCS_OWNERDRAWFIXED类型,就可以屏蔽CViewTabCtrl类中的DrawItem函数。sunxin
- m_tabctl.Create(WS_VISIBLE | WS_CHILD | TCS_OWNERDRAWFIXED, rect, this, nid);
- //m_tabctl.Create(WS_VISIBLE | WS_CHILD | TCS_TABS | TCM_SETITEMSIZE, rect, this, nid);
- //overide this function to provide your tabs
- InitTabs(this);
- return TRUE;
- }
- BOOL CTabCtrlView::HandleTabs(int sel)
- {
- ASSERT(FALSE); //此处的设置相当于限定了在子类中必须重载HandleTabs函数,
- //否则,就会引发断言。
- return FALSE;
- }
- void CTabCtrlView::InitTabs(CTabCtrlView *pView)
- {
- m_tabctl.SetView(pView); //实际上传递的仍然是this。sunxin
- }
- 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
- CWnd* pWnd;
- TRY
- {
- pWnd = (CWnd*)pViewClass->CreateObject();
- if (pWnd == NULL)
- AfxThrowMemoryException();
- }
- CATCH_ALL(e)
- {
- TRACE0("out of memory creating a splitter pane.n");
- // note: delete_exception(e) not required
- return (CView*) NULL;
- }
- END_CATCH_ALL
- ASSERT_KINDOF(CWnd, pWnd);
- ASSERT(pWnd->m_hWnd == NULL); // not yet created
- DWORD dwstyle = AFX_WS_DEFAULT_VIEW;
- // create with the right size (wrong position)
- CRect rect(CPoint(0,0), sizeInit);
- if (!pWnd->Create(NULL, NULL, dwstyle,
- rect, this, 0, pContext))
- {
- TRACE0("warning: couldn't create client pane for splitter.n");
- // pwnd will be cleaned up by postncdestroy
- return (CView*)NULL;
- }
- m_activeView = (CView*) pWnd;
- return m_activeView;
- }
- void CTabCtrlView::OnSize(UINT nType, int cx, int cy)
- {
- CWnd::OnSize(nType, cx, cy);
- // TODO: Add your message handler code here
- if (nType != SIZE_MINIMIZED && cx > 0 && cy > 0)
- RecalcLayout();
- }
- void CTabCtrlView::RecalcLayout()
- {
- CWnd* pWnd = (CWnd*)GetActiveView();
- CRect rect;
- GetClientRect(&rect);
- m_tabctl.RecalcLayout(rect, pWnd);
- }
- CView* CTabCtrlView::GetActiveView()
- {
- return m_activeView;
- }
- void CTabCtrlView::SetTab(int tab)
- {
- m_tabctl.SetCurSel(tab);
- }