目录
1、引用TabSheet.h和TabSheet.cpp文件:
2、主窗口添加标签页:
(1)、标签页的创建和属性更改
(2)、添加俩个标签页的类
(3)、主窗口添加成员变量
(4)、代码实现部分
1、引用TabSheet.h和TabSheet.cpp文件:
MFC引入选项卡控件的类(TabSheet.h和TabSheet.cpp)的目的:
在MFC中,标签页通常通过CTabCtrl类来实现。你可以将TabSheet类作为CTabCtrl的一个子类,以便更方便地管理和操作标签页。
TabSheet.h
#if !defined(AFX_TABSHEET_H__42EE262D_D15F_46D5_8F26_28FD049E99F4__INCLUDED_)
#define AFX_TABSHEET_H__42EE262D_D15F_46D5_8F26_28FD049E99F4__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// TabSheet.h : header file
///
// CTabSheet window
#define MAXPAGE 16class CTabSheet : public CTabCtrl
{// Construction
public:CTabSheet();// Attributes
public:// Operations
public:// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CTabSheet)//}}AFX_VIRTUAL// Implementation
public:int GetCurSel();int SetCurSel(int nItem);void Show();void SetRect();BOOL AddPage(LPCTSTR title, CDialog *pDialog, UINT ID);virtual ~CTabSheet();// Generated message map functions
protected:LPCTSTR m_Title[MAXPAGE];UINT m_IDD[MAXPAGE];CDialog* m_pPages[MAXPAGE];int m_nNumOfPages;int m_nCurrentPage;//{{AFX_MSG(CTabSheet)afx_msg void OnLButtonDown(UINT nFlags, CPoint point);//}}AFX_MSGDECLARE_MESSAGE_MAP()
};///{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_TABSHEET_H__42EE262D_D15F_46D5_8F26_28FD049E99F4__INCLUDED_)
TabSheet.cpp
// TabSheet.cpp : implementation file
//#include "stdafx.h"
//#include "MyPos.h"
#include "TabSheet.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/
// CTabSheetCTabSheet::CTabSheet()
{m_nNumOfPages = 0;m_nCurrentPage = 0;
}CTabSheet::~CTabSheet()
{
}BEGIN_MESSAGE_MAP(CTabSheet, CTabCtrl)//{{AFX_MSG_MAP(CTabSheet)ON_WM_LBUTTONDOWN()//}}AFX_MSG_MAP
END_MESSAGE_MAP()/
// CTabSheet message handlersBOOL CTabSheet::AddPage(LPCTSTR title, CDialog *pDialog, UINT ID)
{if (MAXPAGE == m_nNumOfPages)return FALSE;m_nNumOfPages++;m_pPages[m_nNumOfPages - 1] = pDialog;m_IDD[m_nNumOfPages - 1] = ID;m_Title[m_nNumOfPages - 1] = title;return TRUE;
}void CTabSheet::SetRect()
{CRect tabRect, itemRect;int nX, nY, nXc, nYc;GetClientRect(&tabRect);GetItemRect(0, &itemRect);nX = itemRect.left;nY = itemRect.bottom + 1;nXc = tabRect.right - itemRect.left - 2;nYc = tabRect.bottom - nY - 2;m_pPages[0]->SetWindowPos(&wndTop, nX, nY, nXc, nYc, SWP_SHOWWINDOW);for (int nCount = 1; nCount < m_nNumOfPages; nCount++)m_pPages[nCount]->SetWindowPos(&wndTop, nX, nY, nXc, nYc, SWP_HIDEWINDOW);}void CTabSheet::Show()
{for (int i = 0; i < m_nNumOfPages; i++){m_pPages[i]->Create(m_IDD[i], this);InsertItem(i, m_Title[i]);}m_pPages[0]->ShowWindow(SW_SHOW);for (int i = 1; i < m_nNumOfPages; i++)m_pPages[i]->ShowWindow(SW_HIDE);SetRect();}void CTabSheet::OnLButtonDown(UINT nFlags, CPoint point)
{CTabCtrl::OnLButtonDown(nFlags, point);if (m_nCurrentPage != GetCurFocus()){m_pPages[m_nCurrentPage]->ShowWindow(SW_HIDE);m_nCurrentPage = GetCurFocus();m_pPages[m_nCurrentPage]->ShowWindow(SW_SHOW);// m_pPages[m_nCurrentPage]->SetFocus();}
}int CTabSheet::SetCurSel(int nItem)
{if (nItem < 0 || nItem >= m_nNumOfPages)return -1;int ret = m_nCurrentPage;if (m_nCurrentPage != nItem){m_pPages[m_nCurrentPage]->ShowWindow(SW_HIDE);m_nCurrentPage = nItem;m_pPages[m_nCurrentPage]->ShowWindow(SW_SHOW);// m_pPages[m_nCurrentPage]->SetFocus();CTabCtrl::SetCurSel(nItem);}return ret;
}int CTabSheet::GetCurSel()
{return CTabCtrl::GetCurSel();
}
CTabsheet作为他的子类会提供对选项卡控件的操作和管理
2、主窗口添加标签页:
下面就是我们要显示的标签页最终UI
(1)、标签页的创建和属性更改
创建我们直接添加Dialog即可
属性更改的目的为了让他不再是一个窗口框架样式,变成一个窗口中标签
1、boder None :去掉窗口边框
2、style Child:变成子窗口相当于一个窗口的标签
第二个标签创建和第一个相同
(2)、添加俩个标签页的类
此时有UI文件但是没有.h和.cpp文件,所以我们添加类。