📄 tabbarctrl.cpp
字号:
// TabBarCtrl.cpp : implementation file
//
#include "stdafx.h"
#include <afxpriv.h>
#include "TabBarCtrl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define NC_FUDGE 3
#define CLIENT_FUDGE 10
#define FIRST_TAB_VIEW_ID AFX_IDW_PANE_FIRST + 1
#define TCIF_ALL_BAR (TCIF_TEXT | TCIF_IMAGE | TCIF_RTLREADING | TCIF_PARAM | TCIF_STATE)
//private to implementation
typedef struct _BARTCITEMA
{
_BARTCITEMA()
: lParam(NULL)
, uiID(0)
, pWnd(NULL)
{
ZeroMemory(&hdr, sizeof(TCITEMHEADERA));
}
_BARTCITEMA(TCITEMA& tcItem)
{
hdr.cchTextMax = tcItem.cchTextMax;
hdr.iImage = tcItem.iImage;
hdr.lpReserved1 = tcItem.dwState;
hdr.lpReserved2 = tcItem.dwStateMask;
hdr.mask = tcItem.mask;
hdr.pszText = tcItem.pszText;
lParam = tcItem.lParam;
}
_BARTCITEMA(_BARTCITEMA& tcBarItem)
{
*this = tcBarItem;
}
_BARTCITEMA& operator =(const _BARTCITEMA& tcBarItem)
{
hdr.cchTextMax = tcBarItem.hdr.cchTextMax;
hdr.iImage = tcBarItem.hdr.iImage;
hdr.lpReserved1 = tcBarItem.hdr.lpReserved1;
hdr.lpReserved2 = tcBarItem.hdr.lpReserved2;
hdr.mask = tcBarItem.hdr.mask;
hdr.pszText = tcBarItem.hdr.pszText;
lParam = tcBarItem.lParam;
uiID = tcBarItem.uiID;
pWnd = tcBarItem.pWnd;
return *this;
}
TCITEMHEADERA hdr;
LPARAM lParam;
int uiID;
CWnd* pWnd;
} BARTCITEMA, FAR *LPBARTCITEMA;
typedef struct _BARTCITEMW
{
_BARTCITEMW()
: lParam(NULL)
, uiID(0)
, pWnd(NULL)
{
ZeroMemory(&hdr, sizeof(TCITEMHEADERW));
}
_BARTCITEMW(TCITEMW& tcItem)
{
hdr.cchTextMax = tcItem.cchTextMax;
hdr.iImage = tcItem.iImage;
hdr.lpReserved1 = tcItem.dwState;
hdr.lpReserved2 = tcItem.dwStateMask;
hdr.mask = tcItem.mask;
hdr.pszText = tcItem.pszText;
lParam = tcItem.lParam;
}
_BARTCITEMW(_BARTCITEMW& tcBarItem)
{
*this = tcBarItem;
}
_BARTCITEMW& operator =(const _BARTCITEMW& tcBarItem)
{
hdr.cchTextMax = tcBarItem.hdr.cchTextMax;
hdr.iImage = tcBarItem.hdr.iImage;
hdr.lpReserved1 = tcBarItem.hdr.lpReserved1;
hdr.lpReserved2 = tcBarItem.hdr.lpReserved2;
hdr.mask = tcBarItem.hdr.mask;
hdr.pszText = tcBarItem.hdr.pszText;
lParam = tcBarItem.lParam;
uiID = tcBarItem.uiID;
pWnd = tcBarItem.pWnd;
return *this;
}
TCITEMHEADERW hdr;
LPARAM lParam;
int uiID;
CWnd* pWnd;
} BARTCITEMW, FAR *LPBARTCITEMW;
#ifdef UNICODE
#define BARTCITEM BARTCITEMW
#define LPBARTCITEM LPBARTCITEMW
#else
#define BARTCITEM BARTCITEMA
#define LPBARTCITEM LPBARLPTCITEMA
#endif
/////////////////////////////////////////////////////////////////////////////
// CTabBarCtrl
IMPLEMENT_DYNAMIC(CTabBarCtrl, CControlBar)
CTabBarCtrl::CTabBarCtrl()
: m_pParentFrame(NULL)
, m_bSendInitialUpdate(FALSE)
, m_cyDefault(0)
{
}
CTabBarCtrl::~CTabBarCtrl()
{
DestroyWindow();
}
BEGIN_MESSAGE_MAP(CTabBarCtrl, CControlBar)
//{{AFX_MSG_MAP(CTabBarCtrl)
ON_WM_NCCALCSIZE()
ON_WM_NCPAINT()
ON_WM_DESTROY()
ON_WM_PAINT()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_SIZEPARENT, OnSizeParent)
ON_MESSAGE(WM_INITIALUPDATE, OnInitialUpdate)
ON_NOTIFY_REFLECT(TCN_SELCHANGE, OnSelchange)
ON_MESSAGE(TCM_GETROWCOUNT, OnDoNothing)
ON_MESSAGE(TCM_SETEXTENDEDSTYLE, OnDoNothing)
ON_MESSAGE(TCM_GETEXTENDEDSTYLE, OnDoNothing)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTabBarCtrl message handlers
LRESULT CTabBarCtrl::OnInitialUpdate(WPARAM wParam, LPARAM lParam)
{
HFONT hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
SetFont(CFont::FromHandle(hFont));
ASSERT(m_pParentFrame);
ASSERT(IsWindow(*m_pParentFrame));
int iSel = GetCurSel();
ASSERT(iSel > -1);
CWnd* pView = GetViewFromItem(iSel);
pView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
m_pParentFrame->RecalcLayout();
m_pParentFrame->SetActiveView((CView*)pView);
pView = m_pParentFrame->GetActiveView();
m_bSendInitialUpdate = TRUE;
CRect rectItem;
GetItemRect(0, rectItem);
m_cyDefault = rectItem.Height() + CLIENT_FUDGE;
return 0;
}
LRESULT CTabBarCtrl::OnSizeParent(WPARAM wParam, LPARAM lParam)
{
AFX_SIZEPARENTPARAMS* lpLayout = (AFX_SIZEPARENTPARAMS*)lParam;
CRect rectItem, rectClient(lpLayout->rect);
GetItemRect(0, rectItem);
rectClient.bottom = rectClient.top + rectItem.Height() + CLIENT_FUDGE;
lpLayout->rect.top = rectClient.bottom;
lpLayout->sizeTotal = rectClient.Size();
if (lpLayout->hDWP != NULL)
AfxRepositionWindow(lpLayout, m_hWnd, &rectClient);
return 0;
}
void CTabBarCtrl::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp)
{
lpncsp->rgrc[0].right += 5;
lpncsp->rgrc[0].left -= 1;
CControlBar::OnNcCalcSize(bCalcValidRects, lpncsp);
}
void CTabBarCtrl::OnNcPaint()
{
CRect rectCLient, rectNC;
GetWindowRect(rectNC);
rectNC.OffsetRect(-rectNC.left, -rectNC.top);
CWindowDC dc(this);
CPen pen;
DWORD dwSysGray = GetSysColor(COLOR_BTNFACE);
dc.DrawEdge(rectNC, EDGE_ETCHED, BF_TOP);
rectNC.DeflateRect(0, 2, 0, 0);
CBrush brush(dwSysGray);
dc.FrameRect(rectNC, &brush);
for(int iIndx = 0; iIndx < 2; iIndx++)
{
rectNC.DeflateRect(1, 1, 1, 1);
dc.FrameRect(rectNC, &brush);
}
rectNC.DeflateRect(0, 1, 0, 2);
pen.DeleteObject();
pen.CreatePen(PS_GEOMETRIC | PS_SOLID, 1, dwSysGray);
dc.SelectObject(&pen);
dc.MoveTo(rectNC.left, rectNC.top);
dc.LineTo(rectNC.right, rectNC.top);
}
void CTabBarCtrl::OnDestroy()
{
HIMAGELIST h = (HIMAGELIST)SendMessage(TCM_GETIMAGELIST);
if (CImageList::FromHandlePermanent(h) != NULL)
SendMessage(TCM_SETIMAGELIST, NULL, NULL);
CControlBar::OnDestroy();
}
BOOL CTabBarCtrl::Create(CFrameWnd* pParentWnd)
{
// initialize common controls
INITCOMMONCONTROLSEX icc;
icc.dwSize = sizeof(INITCOMMONCONTROLSEX);
icc.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&icc);
ASSERT_VALID(pParentWnd); // must have a parent
m_pParentFrame = pParentWnd;
m_dwStyle = NULL;
if (!CWnd::Create(WC_TABCONTROL, NULL, WS_CHILD | WS_VISIBLE/* | WS_BORDER*/| WS_DLGFRAME, CRect(0, 0, 0, 0), pParentWnd, AFX_IDC_TAB_CONTROL))
{
TRACE("Failed to create tab bar!\n");
return FALSE;
}
int iu = sizeof(DWORD);
LRESULT lres = ::SendMessage(m_hWnd, TCM_SETITEMEXTRA, 3 * sizeof(DWORD), 0);
return TRUE;
}
BOOL CTabBarCtrl::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
if (message != WM_DRAWITEM)
return CControlBar::OnChildNotify(message, wParam, lParam, pResult);
ASSERT(pResult == NULL); // no return needed
UNUSED(pResult);
DrawItem((LPDRAWITEMSTRUCT)lParam);
return TRUE;
}
void CTabBarCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// must override for overdrawn
ASSERT(FALSE);
}
BOOL CTabBarCtrl::GetItem(int iItem, TCITEM* pTabCtrlItem) const
{
ASSERT(::IsWindow(m_hWnd));
CTabBarCtrl *pBar = (CTabBarCtrl*)this;
return pBar->QueryInfo(TCM_GETITEM, iItem, pTabCtrlItem);
}
BOOL CTabBarCtrl::GetItem(int iItem, TCITEMHEADER* pBarItem) const
{
ASSERT(::IsWindow(m_hWnd));
BARTCITEM *pItem = (BARTCITEM*)pBarItem;
return (BOOL)::SendMessage(m_hWnd, TCM_GETITEM, iItem, (LPARAM)pBarItem);
}
BOOL CTabBarCtrl::SetItem(int iItem, TCITEM* pTabCtrlItem)
{
ASSERT(::IsWindow(m_hWnd));
return SetInfo(TCM_SETITEM, iItem, pTabCtrlItem);
}
DWORD CTabBarCtrl::GetItemState(int iItem, DWORD dwMask) const
{
ASSERT(::IsWindow(m_hWnd));
BARTCITEM item;
ZeroMemory(&item, sizeof(BARTCITEM));
item.hdr.mask = TCIF_STATE;
item.hdr.lpReserved1 = dwMask;
VERIFY(::SendMessage(m_hWnd, TCM_GETITEM, (WPARAM)iItem, (LPARAM)&item));
return item.hdr.lpReserved1;
}
BOOL CTabBarCtrl::SetItemState(int iItem, DWORD dwMask, DWORD dwState)
{
ASSERT(::IsWindow(m_hWnd));
BARTCITEM item;
ZeroMemory(&item, sizeof(BARTCITEM));
item.hdr.mask = TCIF_STATE;
item.hdr.lpReserved1 = dwMask;
return (BOOL)::SendMessage(m_hWnd, TCM_SETITEM, (WPARAM)iItem, (LPARAM)&item);
}
int CTabBarCtrl::SetCurSel(int iItem)
{
ASSERT(::IsWindow(m_hWnd));
return ::SendMessage(m_hWnd, TCM_SETCURSEL, iItem, 0L);
}
//////////////////////////////////////////////////////////////////////////
// InsertView overloads
int CTabBarCtrl::InsertView(CRuntimeClass *pViewClass, int iItem, LPCTSTR lpszItem, CCreateContext* pContext /*= NULL*/)
{
ASSERT(::IsWindow(m_hWnd));
return InsertView(pViewClass, TCIF_TEXT, iItem, lpszItem, 0, 0, 0, 0, pContext);
}
int CTabBarCtrl::InsertView(CRuntimeClass *pViewClass, int iItem, LPCTSTR lpszItem,
int iImage, CCreateContext* pContext/* = NULL*/)
{
ASSERT(::IsWindow(m_hWnd));
return InsertView(pViewClass, TCIF_TEXT | TCIF_IMAGE, iItem, lpszItem, iImage, 0, 0, 0, pContext);
}
int CTabBarCtrl::InsertView(CRuntimeClass *pViewClass, UINT uiMask, int iItem,
LPCTSTR lpszItem, int iImage, LPARAM lParam, CCreateContext* pContext/* = NULL*/)
{
ASSERT(::IsWindow(m_hWnd));
return InsertView(pViewClass, TCIF_TEXT | TCIF_IMAGE | TCIF_PARAM, iItem, lpszItem,
iItem, lParam, 0, 0, pContext);
}
int CTabBarCtrl::InsertView(CRuntimeClass *pViewClass, UINT uiMask, int iItem,
LPCTSTR lpszItem, int iImage, LPARAM lParam, DWORD dwState, DWORD dwStateMask, CCreateContext* pContext/* = NULL*/)
{
ASSERT(::IsWindow(m_hWnd));
TCITEM item;
ZeroMemory(&item, sizeof(TCITEM));
item.mask = uiMask;
item.iImage = iImage;
item.lParam = lParam;
item.pszText = (LPTSTR)lpszItem;
item.dwState = dwState;
item.dwStateMask = dwStateMask;
return InsertView(pViewClass, iItem, &item, pContext);
}
int CTabBarCtrl::InsertView(CRuntimeClass *pViewClass, int iItem, TCITEM* pTabCtrlItem, CCreateContext* pContext /*= NULL*/)
{
ASSERT(::IsWindow(m_hWnd));
BARTCITEM item(*pTabCtrlItem);
if(!CreateView(pViewClass, (TCITEMHEADER*)&item, pContext))
{
return FALSE;
}
return ::SendMessage(m_hWnd, TCM_INSERTITEM, iItem, (LPARAM)&item);
}
//////////////////////////////////////////////////////////////////////////
// AddView overloads
int CTabBarCtrl::AddView(CRuntimeClass *pViewClass, LPCTSTR lpszItem, CCreateContext* pContext/* = NULL*/)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -