📄 tabbarctrl.cpp
字号:
{
ASSERT(::IsWindow(m_hWnd));
return AddView(pViewClass, TCIF_TEXT, lpszItem, 0, 0, 0, 0, pContext);
}
int CTabBarCtrl::AddView(CRuntimeClass *pViewClass, LPCTSTR lpszItem, int iImage, CCreateContext* pContext/* = NULL*/)
{
ASSERT(::IsWindow(m_hWnd));
return AddView(pViewClass, TCIF_TEXT | TCIF_IMAGE, lpszItem, iImage, 0, 0, 0, pContext);
}
int CTabBarCtrl::AddView(CRuntimeClass *pViewClass, LPCTSTR lpszItem,
int iImage, LPARAM lParam, CCreateContext* pContext/* = NULL*/)
{
ASSERT(::IsWindow(m_hWnd));
return AddView(pViewClass, TCIF_TEXT | TCIF_IMAGE | TCIF_PARAM, lpszItem, iImage, lParam, 0, 0, pContext);
}
int CTabBarCtrl::AddView(CRuntimeClass *pViewClass, UINT uiMask, LPCTSTR lpszItem,
int iImage, LPARAM lParam, DWORD dwState, DWORD dwStateMask, CCreateContext* pContext/* = NULL*/)
{
ASSERT(::IsWindow(m_hWnd));
int iCount = GetItemCount();
return InsertView(pViewClass, uiMask, iCount, lpszItem, iImage, lParam, dwState,
dwStateMask, pContext);
}
int CTabBarCtrl::AddView(CRuntimeClass *pViewClass, TCITEM* pTabCtrlItem, CCreateContext* pContext /*= NULL*/)
{
ASSERT(::IsWindow(m_hWnd));
int iCount = GetItemCount();
return InsertView(pViewClass, iCount, pTabCtrlItem, pContext);
}
// AddView overloads END
//////////////////////////////////////////////////////////////////////////
// RemoveView contains certain guards to tell whenever attempted remove is not valid
BOOL CTabBarCtrl::RemoveView(int iItem)
{
ASSERT(::IsWindow(m_hWnd));
int iCount = GetItemCount();
// if this fires, it is an attempt to remove view that does not exist or the last view
ASSERT((iCount > iItem) || (iCount > 1) || (iItem > 0));
// return FALSE to continue working
if((iCount <= iItem) || (iCount < 2))
{
return FALSE;
}
BARTCITEM item;
item.hdr.mask = TCIF_PARAM;
VERIFY(GetItem(iItem, (TCITEMHEADER*)&item));
ASSERT(item.pWnd);
ASSERT(IsWindow(*item.pWnd));
int iTabItem = -1;
CWnd *pNewView = NULL;
CWnd *pRemoveView = item.pWnd; // just to simplify
int iSetCurr = iItem + 1;
// this will set active tab either after or if tab does not exist before removed one
if(AFX_IDW_PANE_FIRST == pRemoveView->GetDlgCtrlID()) //removing active?
{
// to set active tab after removed
pNewView = GetViewFromItem(iSetCurr);
// is it last tab? if so set tab before
if(NULL == pNewView)
{
iSetCurr = iItem - 1;
// attempt to set active tab before removved
pNewView = GetViewFromItem(iSetCurr);
}
}
m_mapUsedID.RemoveKey(item.uiID);
pRemoveView->DestroyWindow();
// we just removed active, set new view.
if(pNewView)
{
pNewView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
pNewView->ShowWindow(SW_SHOW);
SetCurSel(iSetCurr);
m_pParentFrame->RecalcLayout();
pNewView->SetFocus();
}
return (BOOL)::SendMessage(m_hWnd, TCM_DELETEITEM, iItem, 0L);
}
BOOL CTabBarCtrl::RemoveAllViews()
{
ASSERT(::IsWindow(m_hWnd));
int iCount = GetItemCount();
ASSERT(iCount > 1); // 1 view must be left
if(iCount < 2)
{
return FALSE;
}
BARTCITEM itemSave;
BARTCITEM item;
CString csBuffer;
CString csItemText;
// kill all views except current
for(int iIndx = 0; iIndx < iCount; iIndx++)
{
item.hdr.mask = TCIF_ALL_BAR;
LPTSTR pBuf = csBuffer.GetBufferSetLength(MAX_PATH);
item.hdr.cchTextMax = MAX_PATH;
item.hdr.pszText = pBuf;
GetItem(iIndx, (TCITEMHEADER*)&item);
csBuffer.ReleaseBuffer();
ASSERT(item.pWnd);
if(AFX_IDW_PANE_FIRST == item.pWnd->GetDlgCtrlID())
{
// csBuffer has different pointer on each iteration
// this will just copy pointer of csBuffer string to csItemText data member
// so it will point to the same string in itemSave
csItemText = csBuffer;
// save for default view, do not destroy
itemSave = item;
continue;
}
item.pWnd->DestroyWindow();
}
// delete all items
BOOL bResult = ::SendMessage(m_hWnd, TCM_DELETEALLITEMS, 0, 0);
if(!bResult)
{
ASSERT(bResult); // something is wrong not right TCM_DELETEALLITEMS failed
return bResult;
}
m_mapUsedID.RemoveAll();
// insert first item to manage current view
bResult &= (-1 < ::SendMessage(m_hWnd, TCM_INSERTITEM, 0, (LPARAM)&itemSave));
m_mapUsedID.SetAt(itemSave.uiID, itemSave.uiID);
// TCM_INSERTITEM ignores state, we have to set item to set the same state
itemSave.hdr.mask = TCIF_STATE;
bResult &= ::SendMessage(m_hWnd, TCM_SETITEM, 0, (LPARAM)&itemSave);
ASSERT(bResult);
m_pParentFrame->RecalcLayout();
return bResult;
}
void CTabBarCtrl::OnPaint()
{
// do default tab view painting, not a control bar painting
Default();
}
void CTabBarCtrl::EnableDocking(DWORD dwDockStyle)
{
// docking not supported
ASSERT(FALSE);
}
void CTabBarCtrl::OnSelchange(NMHDR* pNMHDR, LRESULT* pResult)
{
int iSel = GetCurSel();
ASSERT(m_pParentFrame);
ASSERT(IsWindow(*m_pParentFrame));
// get current view
CWnd *pViewCurrent = m_pParentFrame->GetDlgItem(AFX_IDW_PANE_FIRST);
ASSERT(pViewCurrent);
ASSERT(IsWindow(*pViewCurrent));
// retrieve original ID
UINT uiID = GetViewTabID(pViewCurrent);
pViewCurrent->SetDlgCtrlID(uiID);
pViewCurrent->ShowWindow(SW_HIDE);
// get View for selected tab
CWnd *pNewViewWnd = GetViewFromItem(iSel);
ASSERT(pNewViewWnd);
ASSERT(IsWindow(*pNewViewWnd));
// make view current
pNewViewWnd->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
pNewViewWnd->ShowWindow(SW_SHOW);
m_pParentFrame->RecalcLayout();
m_pParentFrame->SetActiveView((CView*)pNewViewWnd);
*pResult = 0;
}
void CTabBarCtrl::OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler)
{
Default();
}
BOOL CTabBarCtrl::CreateView(CRuntimeClass *pViewClass, TCITEMHEADER* pBarItem, CCreateContext *pContext)
{
UINT uiNewID = FIRST_TAB_VIEW_ID;
UINT uiFound = 0;
BARTCITEM* pItem = (BARTCITEM*)pBarItem;
// find valid ID >= FIRST_TAB_VIEW_ID
while(m_mapUsedID.Lookup(uiNewID, uiFound))
{
uiNewID++;
}
// Create View
CCreateContext cntxt;
cntxt.m_pCurrentDoc = pContext->m_pCurrentDoc;
cntxt.m_pCurrentFrame = pContext->m_pCurrentFrame;
cntxt.m_pLastView = pContext->m_pLastView;
cntxt.m_pNewDocTemplate = pContext->m_pNewDocTemplate;
cntxt.m_pNewViewClass = pViewClass;
ASSERT(m_pParentFrame);
ASSERT(IsWindow(*m_pParentFrame));
CWnd *pWnd = m_pParentFrame->CreateView(&cntxt, uiNewID);
if(NULL == pWnd)
{
TRACE("Failed to create view.");
return FALSE;
}
pItem->hdr.mask |= TCIF_PARAM;
pItem->pWnd = pWnd;
pItem->uiID = uiNewID;
m_mapUsedID.SetAt(uiNewID, uiNewID);
if(m_bSendInitialUpdate)
{
pWnd->SendMessage(WM_INITIALUPDATE);
}
return TRUE;
}
UINT CTabBarCtrl::GetViewTabID(CWnd* pWnd)
{
CWnd *pView = NULL;
int iTabItem = -1;
int iCount = GetItemCount();
for(int iIndx = 0; iIndx < iCount; iIndx++)
{
BARTCITEM barItem;
barItem.hdr.mask = TCIF_PARAM;
GetItem(iIndx, (TCITEMHEADER*)&barItem);
if(barItem.pWnd == pWnd)
{
// minimum ID is AFX_IDW_PANE_FIRST + 1
ASSERT(barItem.uiID > AFX_IDW_PANE_FIRST);
return barItem.uiID;
}
}
ASSERT(FALSE);
return 0;
}
CWnd* CTabBarCtrl::GetViewFromItem(int iItem)
{
BARTCITEM barItem;
barItem.hdr.mask = TCIF_PARAM;
BOOL bResult = GetItem(iItem, (TCITEMHEADER*)&barItem);
// item do not exists
if(!bResult)
{
return NULL;
}
ASSERT(barItem.uiID > AFX_IDW_PANE_FIRST); // minimum ID is AFX_IDW_PANE_FIRST + 1
return barItem.pWnd;
}
int CTabBarCtrl::QueryInfo(UINT uiType, int iItem, TCITEMHEADER* pBarItem)
{
return ::SendMessage(m_hWnd, uiType, iItem, (LPARAM)pBarItem);
}
int CTabBarCtrl::QueryInfo(UINT uiType, int iItem, TCITEM *pTabCtrlItem)
{
BARTCITEM BarItem(*pTabCtrlItem);
return QueryInfo(uiType, iItem, (TCITEMHEADER*)&BarItem);
}
int CTabBarCtrl::SetInfo(UINT uiType, int iItem, TCITEMHEADER* pBarItem)
{
return ::SendMessage(m_hWnd, uiType, iItem, (LPARAM)pBarItem);
}
int CTabBarCtrl::SetInfo(UINT uiType, int iItem, TCITEM *pTabCtrlItem)
{
BARTCITEM BarItem(*pTabCtrlItem);
BOOL bParam = ((pTabCtrlItem->mask & TCIF_PARAM) == TCIF_PARAM);
if(bParam)
{
BARTCITEM barItemQuerry;
barItemQuerry.hdr.mask = TCIF_PARAM;
VERIFY(GetItem(iItem, (TCITEMHEADER*)&barItemQuerry));
BarItem.uiID = barItemQuerry.uiID;
BarItem.pWnd = barItemQuerry.pWnd;
}
int iRet = SetInfo(uiType, iItem, (TCITEMHEADER*)&BarItem);
return iRet;
}
BOOL CTabBarCtrl::SetItemData(int iItem, LPARAM lParam)
{
BARTCITEM item;
item.hdr.mask = TCIF_PARAM;
if(!QueryInfo(TCM_GETITEM, iItem, (TCITEMHEADER*)&item))
{
return FALSE;
}
item.lParam = lParam;
return SetInfo(TCM_SETITEM, iItem, (TCITEMHEADER*)&item);
}
int CTabBarCtrl::SetItemImage(int iItem, UINT uiImage)
{
BARTCITEM item;
item.hdr.mask = TCIF_IMAGE;
item.hdr.iImage = uiImage;
return SetInfo(TCM_SETITEM, iItem, (TCITEMHEADER*)&item);
}
BOOL CTabBarCtrl::SetItemText(int iItem, CString csText)
{
BARTCITEM item;
item.hdr.mask = TCIF_TEXT;
item.hdr.pszText = (LPTSTR)(LPCTSTR)csText;
return SetInfo(TCM_SETITEM, iItem, (TCITEMHEADER*)&item);
}
CWnd* CTabBarCtrl::GetItemView(int iItem)
{
return GetViewFromItem(iItem);
}
LRESULT CTabBarCtrl::OnDoNothing(WPARAM wParam, LPARAM lParam)
{
// do nothing
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -