?? tabbedframe.h
字號:
{
return -1;
}
TTabCtrl::TItem* pItem = m_TabCtrl.CreateNewItem();
if(pItem)
{
pItem->SetText(sTabText);
pItem->SetImageIndex(nImageIndex);
// NOTE: You must use a tab item class derived off of CCustomTabCtrl
// that tracks a view HWND, such as CTabViewTabItem
pItem->SetTabView(hWnd);
// The tab control takes ownership of the new item
return m_TabCtrl.InsertItem(m_TabCtrl.GetItemCount(), pItem);
}
return -1;
}
int DisplayTab(HWND hWnd, BOOL bAddIfNotFound = TRUE, BOOL bUseIcon = FALSE)
{
int nTab = -1;
if(hWnd)
{
size_t nOldCount = m_TabCtrl.GetItemCount();
TTabCtrl::TItem tcItem;
tcItem.SetTabView(hWnd);
nTab = m_TabCtrl.FindItem(&tcItem, CTFI_TABVIEW);
if((bAddIfNotFound == TRUE) && (nTab < 0))
{
// The corresponding tab doesn't exist yet. Create it.
LPTSTR sWindowText = NULL;
size_t cchWindowText = ::GetWindowTextLength(hWnd);
if(cchWindowText > 0)
{
sWindowText = new TCHAR[cchWindowText + 1];
if(sWindowText != NULL)
{
::GetWindowText(hWnd, sWindowText, cchWindowText+1);
HICON hIcon = NULL;
if(bUseIcon)
{
if(hIcon == NULL)
{
hIcon = (HICON) ::SendMessage(hWnd, WM_GETICON, ICON_SMALL, 0);
}
if(hIcon == NULL)
{
hIcon = (HICON) LongToHandle(::GetClassLong(hWnd, GCL_HICONSM));
}
if(hIcon == NULL)
{
hIcon = (HICON) ::SendMessage(hWnd, WM_GETICON, ICON_BIG, 0);
}
if(hIcon == NULL)
{
hIcon = (HICON) LongToHandle(::GetClassLong(hWnd, GCL_HICON));
}
}
if(hIcon == NULL)
{
nTab = AddTab(hWnd, sWindowText);
}
else
{
nTab = AddTabWithIcon(hWnd, sWindowText, hIcon);
}
delete [] sWindowText;
}
}
if(nTab < 0)
{
// We had trouble getting the window text
// TODO: What should we put for the text and/or icon
// in this case?
ATLASSERT(0 && "Adding a tab where no name was provided");
nTab = AddTab(hWnd, _T("Untitled"));
}
}
if(nTab >= 0)
{
m_TabCtrl.SetCurSel(nTab);
if((nOldCount == 0) && (m_TabCtrl.GetItemCount() == 1))
{
T* pT = static_cast<T*>(this);
pT->OnAddFirstTab();
}
}
}
return nTab;
}
BOOL RemoveTab(HWND hWnd)
{
BOOL bSuccess = FALSE;
TTabCtrl::TItem tcItem;
tcItem.SetTabView(hWnd);
int nTab = m_TabCtrl.FindItem(&tcItem, CTFI_TABVIEW);
if(nTab >= 0)
{
bSuccess = m_TabCtrl.DeleteItem(nTab);
if(m_TabCtrl.GetItemCount() < 1)
{
T* pT = static_cast<T*>(this);
pT->OnRemoveLastTab();
}
}
return bSuccess;
}
BOOL UpdateTabText(HWND hWnd, LPCTSTR sText = NULL)
{
BOOL bSuccess = FALSE;
TTabCtrl::TItem tcItem;
tcItem.SetTabView(hWnd);
int nTab = m_TabCtrl.FindItem(&tcItem, CTFI_TABVIEW);
if(nTab >= 0)
{
TTabCtrl::TItem* pItem = m_TabCtrl.GetItem(nTab);
CString sCurrentTabText = pItem->GetText();
if(sText != NULL)
{
if(sCurrentTabText != sText)
{
bSuccess = pItem->SetText(sText);
m_TabCtrl.UpdateLayout();
m_TabCtrl.Invalidate();
}
}
else
{
LPTSTR sWindowText = NULL;
size_t cchWindowText = ::GetWindowTextLength(hWnd);
if(cchWindowText > 0)
{
sWindowText = new TCHAR[cchWindowText + 1];
if(sWindowText != NULL)
{
::GetWindowText(hWnd, sWindowText, cchWindowText+1);
if(sWindowText != NULL &&
sCurrentTabText != sWindowText)
{
bSuccess = pItem->SetText(sWindowText);
m_TabCtrl.UpdateLayout();
m_TabCtrl.Invalidate();
}
delete [] sWindowText;
}
}
}
}
return bSuccess;
}
BOOL UpdateTabImage(HWND hWnd, int nImageIndex = -1)
{
BOOL bSuccess = FALSE;
TTabCtrl::TItem tcItem;
tcItem.SetTabView(hWnd);
int nTab = m_TabCtrl.FindItem(&tcItem, CTFI_TABVIEW);
if(nTab >= 0)
{
TTabCtrl::TItem* pItem = m_TabCtrl.GetItem(nTab);
int nCurrentImageIndex = pItem->GetImageIndex();
if(nCurrentImageIndex != nImageIndex)
{
bSuccess = pItem->SetImageIndex(nImageIndex);
m_TabCtrl.UpdateLayout();
m_TabCtrl.Invalidate();
}
}
return bSuccess;
}
BOOL UpdateTabToolTip(HWND hWnd, LPCTSTR sToolTip = NULL)
{
BOOL bSuccess = FALSE;
TTabCtrl::TItem tcItem;
tcItem.SetTabView(hWnd);
int nTab = m_TabCtrl.FindItem(&tcItem, CTFI_TABVIEW);
if(nTab >= 0)
{
TTabCtrl::TItem* pItem = m_TabCtrl.GetItem(nTab);
CString sCurrentToolTip = pItem->GetToolTip();
if(sCurrentToolTip != sToolTip)
{
bSuccess = pItem->SetToolTip(sToolTip);
}
}
return bSuccess;
}
BOOL HighlightTab(HWND hWnd, bool bHighlight = true)
{
BOOL bSuccess = FALSE;
TTabCtrl::TItem tcItem;
tcItem.SetTabView(hWnd);
int nTab = m_TabCtrl.FindItem(&tcItem, CTFI_TABVIEW);
if(nTab >= 0)
{
bSuccess = m_TabCtrl.HighlightItem((size_t)nTab, bHighlight);
}
return bSuccess;
}
BOOL UpdateTabCanClose(HWND hWnd, bool bCanClose = true)
{
BOOL bSuccess = FALSE;
TTabCtrl::TItem tcItem;
tcItem.SetTabView(hWnd);
int nTab = m_TabCtrl.FindItem(&tcItem, CTFI_TABVIEW);
if(nTab >= 0)
{
TTabCtrl::TItem* pItem = m_TabCtrl.GetItem(nTab);
bool bCurrentCanClose = pItem->CanClose();
if(bCurrentCanClose != bCanClose)
{
bSuccess = pItem->SetCanClose(bCanClose);
m_TabCtrl.UpdateLayout();
m_TabCtrl.Invalidate();
}
}
return bSuccess;
}
};
/////////////////////////////////////////////////////////////////////////////
//
// CTabbedFrameImpl
//
/////////////////////////////////////////////////////////////////////////////
#define CHAIN_ACTIVETABVIEW_COMMANDS() \
if(uMsg == WM_COMMAND && m_hWndActive != NULL) \
::SendMessage(m_hWndActive, uMsg, wParam, lParam);
#define CHAIN_ACTIVETABVIEW_CHILD_COMMANDS(tabClass) \
if(uMsg == WM_COMMAND) \
{ \
HWND hWndChild = tabClass.GetActiveView(); \
if(hWndChild != NULL) \
::SendMessage(hWndChild, uMsg, wParam, lParam); \
}
// Use this if forwarding to an ActiveX control.
#define CHAIN_ACTIVETABVIEW_CHILD_COMMANDS2(tabClass) \
if(uMsg == WM_COMMAND) \
{ \
HWND hWndChild = tabClass.GetActiveView(); \
if(hWndChild != NULL) \
::SendMessage(hWndChild, uMsg, wParam, 0); \
}
template <
class T,
class TTabCtrl = CDotNetTabCtrl<CTabViewTabItem>,
class TBase = CFrameWindowImpl<T, CWindow, CFrameWinTraits> >
class CTabbedFrameImpl :
public TBase,
public CCustomTabOwnerImpl< CTabbedFrameImpl<T, TTabCtrl, TBase>, TTabCtrl>
{
protected:
typedef CTabbedFrameImpl<T, TTabCtrl, TBase> thisClass;
typedef TBase baseClass;
typedef CCustomTabOwnerImpl< CTabbedFrameImpl<T, TTabCtrl, TBase>, TTabCtrl> customTabOwnerClass;
// Member variables
protected:
bool m_bReflectNotifications, m_bForwardNotifications;
DWORD m_nTabStyles;
HWND m_hWndActive;
// Constructors
public:
CTabbedFrameImpl(bool bReflectNotifications = false, bool bForwardNotifications = false) :
m_bReflectNotifications(bReflectNotifications),
m_bForwardNotifications(bForwardNotifications),
m_nTabStyles(CTCS_BOTTOM | CTCS_TOOLTIPS),
m_hWndActive(NULL)
{
}
// Methods
public:
void SetReflectNotifications(bool bReflectNotifications = true)
{
m_bReflectNotifications = bReflectNotifications;
}
bool GetReflectNotifications(void) const
{
return m_bReflectNotifications;
}
void SetForwardNotifications(bool bForwardNotifications = true)
{
m_bForwardNotifications = bForwardNotifications;
}
bool GetForwardNotifications(void) const
{
return m_bForwardNotifications;
}
void SetTabStyles(DWORD nTabStyles)
{
m_nTabStyles = nTabStyles;
}
DWORD GetTabStyles(void) const
{
return m_nTabStyles;
}
HWND GetActiveView(void) const
{
return m_hWndActive;
}
virtual void OnFinalMessage(HWND /*hWnd*/)
{
// TODO: Have support both for "new"ing an
// instance of this class, or having
// a member variable of this class.
// Currently, we don't support deleting our
// instance because someone created us with "new"
//delete this;
}
// Message Handling
public:
// The class that derives from this class should set an appropriate background brush
DECLARE_FRAME_WND_CLASS_EX(_T("TabbedFrame"), 0, 0, COLOR_APPWORKSPACE)
BEGIN_MSG_MAP(thisClass)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
MESSAGE_HANDLER(WM_SETTINGCHANGE, OnSettingChange)
MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBackground)
MESSAGE_HANDLER(WM_SETFOCUS, OnSetFocus)
MESSAGE_HANDLER(WM_FORWARDMSG, OnForwardMsg)
NOTIFY_CODE_HANDLER(NM_CLICK, OnClick)
NOTIFY_CODE_HANDLER(CTCN_ACCEPTITEMDRAG, OnAcceptItemDrag)
NOTIFY_CODE_HANDLER(CTCN_CANCELITEMDRAG, OnCancelItemDrag)
NOTIFY_CODE_HANDLER(CTCN_DELETEITEM, OnDeleteItem)
NOTIFY_CODE_HANDLER(CTCN_SELCHANGING, OnSelChanging)
NOTIFY_CODE_HANDLER(CTCN_SELCHANGE, OnSelChange)
CHAIN_MSG_MAP(baseClass)
// If there are key messages that haven't been handled yet,
// pass those along to the active child window
if(uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST)
{
if(m_hWndActive != NULL && ::IsWindow(m_hWndActive))
{
lResult = ::SendMessage(m_hWndActive, uMsg, wParam, lParam);
return TRUE;
}
}
CHAIN_ACTIVETABVIEW_COMMANDS()
if(m_bReflectNotifications)
{
REFLECT_NOTIFICATIONS()
}
if(m_bForwardNotifications)
{
FORWARD_NOTIFICATIONS()
}
END_MSG_MAP()
LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
// "baseClass::OnCreate()"
LRESULT lRet = DefWindowProc(uMsg, wParam, lParam);
bHandled = TRUE;
if(lRet == -1)
{
return -1;
}
// The derived C++ class should set the background brush for
// the window class (DECLARE_FRAME_WND_CLASS_EX)
//::SetClassLongPtr(m_hWnd, GCLP_HBRBACKGROUND, COLOR_APPWORKSPACE+1);
this->CreateTabWindow(m_hWnd, rcDefault, m_nTabStyles);
return 0;
}
LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
DestroyTabWindow();
// Say that we didn't handle it so that anyone else
// interested gets to handle the message
bHandled = FALSE;
return 0;
}
LRESULT OnSettingChange(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
// Be sure tab gets message before we recalculate the tab area height,
// so that it can adjust its font metrics first.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -