📄 tabbedframe.h
字号:
/////////////////////////////////////////////////////////////////////////////
// TabbedFrame.h - Base template class for supporting a frame
// window with multiple views that you switch between using
// a "CustomTabCtrl" (such as CDotNetTabCtrl)
//
// Written by Daniel Bowen (dbowen@es.com)
// Copyright (c) 2002-2005 Daniel Bowen.
//
// Depends on CustomTabCtrl.h originally by Bjarke Viksoe (bjarke@viksoe.dk)
// with the modifications by Daniel Bowen
//
// CCustomTabOwnerImpl -
// MI class that helps implement the parent of the actual custom tab control window.
// The class doesn't have a message map itself, and is meant
// to be inherited from along-side a CWindowImpl derived class.
// This class handles creation of the tab window as well as
// adding, removing, switching and renaming tabs based on an HWND.
// CTabbedFrameImpl -
// Base template to derive your specialized frame window class from to get
// a frame window with multiple "view" child windows that you
// switch between using a custom tab control (such as CDotNetTabCtrl).
// CTabbedPopupFrame -
// Simple class deriving from CTabbedFrameImpl that is suitable
// for implementing a tabbed "popup frame" tool window, with one or more views.
// CTabbedChildWindow -
// Simple class deriving from CTabbedFrameImpl that is suitable
// for implementing a tabbed child window, with one or more views.
//
//
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name is included.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage to you or your
// computer whatsoever.
//
// If you find bugs, have suggestions for improvements, etc.,
// please contact the author.
//
// History (Date/Author/Description):
// ----------------------------------
//
// 2005/07/13: Daniel Bowen
// - Namespace qualify the use of more ATL and WTL classes.
//
// 2005/04/12: Daniel Bowen
// - CCustomTabOwnerImpl::CalcTabAreaHeight -
// * CDC dc = TabCtrl.GetDC();
// should be
// CClientDC dc(TabCtrl);
//
// 2005/04/08: Daniel Bowen
// - Generalize support for having the tab control automatically hidden
// if the number of tabs is below a certain count.
// - CCustomTabOwnerImpl -
// * Change OnAddFirstTab and OnRemoveLastTab to be more
// general purpose. Have OnAddTab and OnRemoveTab instead,
// and have them called for every AddTab or RemoveTab.
// * Add KeepTabsHidden (overrideable). Previously only in CMDITabOwnerImpl.
// * Add ShowTabControl, HideTabControl (overrideable)
// * Add SetMinTabCountForVisibleTabs (method)
// - CTabbedFrameImpl -
// * Pass in T instead of CTabbedFrameImpl<T,...> to CCustomTabOwnerImpl inheritance
// (so you can override CCustomTabOwnerImpl overrideables in
// CTabbedFrameImpl derived classes)
// * Add ModifyTabStyles (method)
// * OnSettingChange - Call CalcTabAreaHeight through pT
// * SetTabAreaHeight - Support for "KeepTabsHidden"
//
// 2005/03/14: Daniel Bowen
// - Fix warnings when compiling for 64-bit.
//
// 2004/11/29: Daniel Bowen
// - Update all WM_NOTIFY handlers to check that the notification is
// from the tab control (and not from a sibling like a list view control)
// - Update CTabbedFrameImpl::OnRemoveLastTab to call this->Invalidate()
//
// 2004/06/28: Daniel Bowen
// - CCustomTabOwnerImpl -
// * HighlightTab
// - Clean up warnings on level 4
//
// 2004/06/21: Peter Carlson
// - CCustomTabOwnerImpl -
// * UpdateTabCanClose
//
// 2004/05/14: Daniel Bowen
// - CTabbedFrameImpl -
// * Update OnClick handling so it only sets focus to the tab view
// if the selected tab is being clicked. Without this update,
// other code that tries to minimize flickering when switching
// the active view doesn't get called.
//
// 2004/04/29: Daniel Bowen
// - Use LongToHandle with GetClassLong when getting HICON
// - CTabbedFrameImpl -
// * Only forward focus to the active view if
// the tab isn't currently capturing the mouse.
// * Respond to NM_CLICK, CTCN_ACCEPTITEMDRAG and CTCN_CANCELITEMDRAG
// from the tab control, and set focus to the tab item's view
//
// 2004/02/03: Daniel Bowen
// - CTabbedFrameImpl -
// * Add new Set/GetForwardNotifications in case you want the parent of the tab
// window to forward notifications on to its parent. A good example where
// you might want to use this would be with CTabbedChildWindow.
//
// 2004/01/19: Daniel Bowen
// - CTabbedFrameImpl -
// * Have new "CHAIN_ACTIVETABVIEW_CHILD_COMMANDS" and "CHAIN_ACTIVETABVIEW_CHILD_COMMANDS2"
// macro that is used to forward WM_COMMAND messages to the active view of a tab window
// from outside the implementation of that tab window (such as in the Main Frame).
//
// 2003/06/27: Daniel Bowen
// - CCustomTabOwnerImpl -
// * Remove WTL:: scope off of CImageList member.
// - CTabbedFrameImpl -
// * Have new "CHAIN_ACTIVETABVIEW_COMMANDS" macro that is used to forward
// WM_COMMAND messages to the active view. This is done after
// the CHAIN_MSG_MAP(baseClass), so be careful if the base
// class also handles WM_COMMAND messages (the default
// CFrameWindowImpl does not, and neither does CMDIChildWindowImpl
// or CTabbedMDIChildWindowImpl).
// * New "GetActiveView" that returns what CTabbedFrameImpl
// thinks is the active view.
// * Replace
// DECLARE_FRAME_WND_CLASS(_T("TabbedFrame"), 0)
// with
// DECLARE_FRAME_WND_CLASS_EX(_T("TabbedFrame"), 0, 0, COLOR_APPWORKSPACE)
// (gets rid of CS_DBLCLKS, CS_HREDRAW and CS_VREDRAW, sets background brush)
// * Support "empty" tabbed frame (have window class brush,
// let default handling of WM_ERASEBKGND happen if no active view,
// and NULL out m_hWndActive in OnRemoveLastTab).
// - CTabbedPopupFrame -
// * Replace
// DECLARE_FRAME_WND_CLASS(_T("TabbedPopupFrame"), 0)
// with
// DECLARE_FRAME_WND_CLASS_EX(_T("TabbedPopupFrame"), 0, 0, COLOR_APPWORKSPACE)
// (gets rid of CS_DBLCLKS, CS_HREDRAW and CS_VREDRAW, sets background brush)
// - CTabbedChildWindow -
// * Replace
// DECLARE_WND_CLASS(_T("TabbedChildWindow"))
// with
// DECLARE_FRAME_WND_CLASS_EX(_T("TabbedChildWindow"), 0, 0, COLOR_APPWORKSPACE)
// (gets rid of CS_DBLCLKS, CS_HREDRAW and CS_VREDRAW, sets background brush)
//
// 2003/02/27: Daniel Bowen
// - Use _U_STRINGorID instead of WTL::_U_STRINGorID.
// For VC7, this means you must #define _WTL_NO_UNION_CLASSES
// before including the WTL header files, or you will
// get compile errors (the ATL7 union classes are defined
// in atlwin.h).
//
// 2002/11/27: Daniel Bowen
// - CTabbedFrameImpl::GetTabStyles needs to return DWORD, not bool
//
// 2002/09/25: Daniel Bowen
// - CTabbedFrameImpl -
// * Expose "SetTabStyles" and "GetTabStyles" so that you can change
// the tab related styles to something different than the default
// - CTabbedPopupFrame -
// * Expose "SetCloseCommand" and "GetCloseCommand" so that
// instead of destroying the window when the close button
// on the popup frame is pushed, a command ID of your choice
// is sent to the parent (such as a menu ID that corresponds
// to toggling the visibility of the popup frame)
//
// 2002/06/26: Daniel Bowen
// - New "CTabbedChildWindow" that derives from CTabbedFrameImpl.
// You can use this class when you want a child window to
// use a tab control to switch between multiple views
// - Provide "PreTranslateMessage" function in CTabbedPopupFrame
// (and the new CTabbedChildWindow)
// - CCustomTabOwnerImpl -
// * Rename "GetTabs" method to "GetTabCtrl"
// * Rename member "m_tabs" to "m_TabCtrl"
// * Rename template argument "TTab" to "TTabCtrl"
// * Rename "ShowTabs" and "HideTabs" overrideables to "OnAddFirstTab" and "OnRemoveLastTab",
// and change the place that calls these to live up to those new names
// * Remove GetCurSel (just call GetTabCtrl().GetCurSel() instead)
// * DisplayTab -
// + Add new parameter that says whether to use the window's icon.
// If TRUE, the icon is requested first by sending the window WM_GETICON
// looking for the "small" icon, then asking the window class for a small icon.
// If no small icon is found, the same procedure is used to look for the
// "big" icon.
// + Call "SetCurSel" even if the tab to display has the same index
// as the current selection
// + Call "OnAddFirstTab" (which was "ShowTabs") only when the count
// of tabs goes from 0 to 1.
//
// 2002/06/12: Daniel Bowen
// - Publish codeproject article. For history prior
// to the release of the article, please see the article
// and the section "Note to previous users"
#ifndef __WTL_TABBED_FRAME_H__
#define __WTL_TABBED_FRAME_H__
#pragma once
#ifndef __cplusplus
#error TabbedFrame.h requires C++ compilation
#endif
#ifndef __ATLAPP_H__
#error TabbedFrame.h requires atlapp.h to be included first
#endif
#ifndef __ATLWIN_H__
#error TabbedFrame.h requires atlwin.h to be included first
#endif
#ifndef __ATLFRAME_H__
#error TabbedFrame.h requires atlframe.h to be included first
#endif
#ifndef __CUSTOMTABCTRL_H__
#include "CustomTabCtrl.h"
#endif
/////////////////////////////////////////////////////////////////////////////
//
// CCustomTabOwnerImpl
// an MI template to help implement the owner window that uses CustomTabCtrl
// to switch between windows / views
//
/////////////////////////////////////////////////////////////////////////////
template <class T, class TTabCtrl>
class CCustomTabOwnerImpl
{
// Member variables
protected:
TTabCtrl m_TabCtrl;
WTL::CImageList m_ImageList;
int m_cxImage, m_cyImage;
int m_nTabAreaHeight;
size_t m_nMinTabCountForVisibleTabs;
bool m_bKeepTabsHidden;
// Constructors
public:
CCustomTabOwnerImpl() :
m_cxImage(16),
m_cyImage(16),
m_nTabAreaHeight(24),
m_nMinTabCountForVisibleTabs(1)
{
m_bKeepTabsHidden = (m_nMinTabCountForVisibleTabs > 0);
}
// Overrideables
public:
void OnAddTab(size_t nNewTabCount)
{
T* pT = static_cast<T*>(this);
// NOTE: Derived classes should call this base class version as well
if(nNewTabCount == m_nMinTabCountForVisibleTabs)
{
pT->ShowTabControl();
}
}
void OnRemoveTab(size_t nNewTabCount)
{
T* pT = static_cast<T*>(this);
// NOTE: Derived classes should call this base class version as well
if((nNewTabCount+1) == m_nMinTabCountForVisibleTabs)
{
pT->HideTabControl();
}
else if(nNewTabCount == 0)
{
pT->Invalidate();
}
}
void KeepTabsHidden(bool bKeepTabsHidden = true)
{
if(m_bKeepTabsHidden != bKeepTabsHidden)
{
m_bKeepTabsHidden = bKeepTabsHidden;
// CalcTabAreaHeight will end up doing UpdateLayout and Invalidate
T* pT = static_cast<T*>(this);
pT->CalcTabAreaHeight();
}
}
void ShowTabControl(void)
{
T* pT = static_cast<T*>(this);
pT->KeepTabsHidden(false);
}
void HideTabControl(void)
{
T* pT = static_cast<T*>(this);
pT->KeepTabsHidden(true);
}
void SetTabAreaHeight(int nNewTabAreaHeight)
{
if(m_nTabAreaHeight != nNewTabAreaHeight)
{
m_nTabAreaHeight = nForceTabAreaHeight;
/*
T* pT = static_cast<T*>(this);
pT->UpdateLayout();
Invalidate();
*/
}
}
// A derived class might not need to override this although they can.
// (but they will probably need to specialize SetTabAreaHeight)
void CalcTabAreaHeight(void)
{
// Dynamically figure out a reasonable tab area height
// based on the tab's font metrics
const int nNominalHeight = 24;
const int nNominalFontLogicalUnits = 11; // 8 point Tahoma with 96 DPI
// Initialize nFontLogicalUnits to the typical case
// appropriate for CDotNetTabCtrl
LOGFONT lfIcon = { 0 };
::SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(lfIcon), &lfIcon, 0);
int nFontLogicalUnits = -lfIcon.lfHeight;
// Use the actual font of the tab control
TTabCtrl& TabCtrl = this->GetTabCtrl();
if(TabCtrl.IsWindow())
{
HFONT hFont = TabCtrl.GetFont();
if(hFont != NULL)
{
CClientDC dc(TabCtrl);
CFontHandle hFontOld = dc.SelectFont(hFont);
TEXTMETRIC tm = {0};
dc.GetTextMetrics(&tm);
nFontLogicalUnits = tm.tmAscent;
dc.SelectFont(hFontOld);
}
}
int nNewTabAreaHeight = nNominalHeight + ( ::MulDiv(nNominalHeight, nFontLogicalUnits, nNominalFontLogicalUnits) - nNominalHeight ) / 2;
T* pT = static_cast<T*>(this);
pT->SetTabAreaHeight(nNewTabAreaHeight);
}
// Methods
public:
TTabCtrl& GetTabCtrl(void)
{
return m_TabCtrl;
}
int GetTabAreaHeight(void) const
{
return m_nTabAreaHeight;
}
void SetMinTabCountForVisibleTabs(size_t nMinTabCountForVisibleTabs)
{
if(m_nMinTabCountForVisibleTabs != nMinTabCountForVisibleTabs)
{
T* pT = static_cast<T*>(this);
m_nMinTabCountForVisibleTabs = nMinTabCountForVisibleTabs;
size_t nCurrentTabCount = m_TabCtrl.GetItemCount();
if(nCurrentTabCount < m_nMinTabCountForVisibleTabs)
{
pT->HideTabControl();
}
else
{
pT->ShowTabControl();
}
}
}
void CreateTabWindow(HWND hWndTabParent, RECT rcTab, DWORD dwOtherStyles = CTCS_TOOLTIPS)
{
if(m_TabCtrl.IsWindow())
{
m_TabCtrl.DestroyWindow();
}
BOOL bCreate = FALSE;
bCreate = m_ImageList.Create(m_cxImage, m_cyImage, ILC_COLOR32 | ILC_MASK, 4, 4);
if(bCreate)
{
m_TabCtrl.SetImageList(m_ImageList);
}
DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | dwOtherStyles;
m_TabCtrl.Create(hWndTabParent, rcTab, NULL, dwStyle);
T* pT = static_cast<T*>(this);
pT->CalcTabAreaHeight();
}
BOOL DestroyTabWindow()
{
return m_ImageList.Destroy();
}
// AddBitmap (with a couple of overloaded versions)
int AddBitmap(HBITMAP hBitmap, HBITMAP hBitmapMask = NULL)
{
return m_ImageList.Add(hBitmap, hBitmapMask);
}
int AddBitmap(HBITMAP hBitmap, COLORREF crMask)
{
return m_ImageList.Add(hBitmap, crMask);
}
int AddBitmap(_U_STRINGorID bitmap, COLORREF crMask, HMODULE hModule = _Module.GetResourceInstance())
{
HBITMAP hBitmap = (HBITMAP)::LoadImage(
hModule,
bitmap.m_lpstr,
IMAGE_BITMAP, 0, 0, LR_SHARED);
return hBitmap ? m_ImageList.Add(hBitmap, crMask) : -1;
}
int AddBitmap(_U_STRINGorID bitmap, HBITMAP hBitmapMask = NULL, HMODULE hModule = _Module.GetResourceInstance())
{
HBITMAP hBitmap = (HBITMAP)::LoadImage(
hModule,
bitmap.m_lpstr,
IMAGE_BITMAP, 0, 0, LR_SHARED);
return hBitmap ? m_ImageList.Add(hBitmap, hBitmapMask) : -1;
}
// AddIcon (with a couple of overloaded versions)
int AddIcon(HICON hIcon)
{
return m_ImageList.AddIcon(hIcon);
}
int AddIcon(_U_STRINGorID icon, HMODULE hModule = _Module.GetResourceInstance())
{
HICON hIcon = (HICON)::LoadImage(
hModule,
icon.m_lpstr,
IMAGE_ICON, m_cxImage, m_cyImage, LR_SHARED);
return hIcon ? m_ImageList.AddIcon(hIcon) : -1;
}
// AddTabWithBitmap (with a couple of overloaded versions)
int AddTabWithBitmap(HWND hWnd, LPCTSTR sTabText, HBITMAP hBitmap, HBITMAP hBitmapMask = NULL)
{
if(hWnd == NULL)
{
return -1;
}
int nImageIndex = this->AddBitmap(hBitmap, hBitmapMask);
return this->AddTab(hWnd, sTabText, nImageIndex);
}
int AddTabWithBitmap(HWND hWnd, LPCTSTR sTabText, HBITMAP hBitmap, COLORREF crMask)
{
if(hWnd == NULL)
{
return -1;
}
int nImageIndex = this->AddBitmap(hBitmap, crMask);
return this->AddTab(hWnd, sTabText, nImageIndex);
}
int AddTabWithBitmap(HWND hWnd, LPCTSTR sTabText, _U_STRINGorID bitmap, HBITMAP hBitmapMask = NULL, HMODULE hModule = _Module.GetResourceInstance())
{
if(hWnd == NULL)
{
return -1;
}
int nImageIndex = this->AddBitmap(bitmap, hBitmapMask, hModule);
return this->AddTab(hWnd, sTabText, nImageIndex);
}
int AddTabWithBitmap(HWND hWnd, LPCTSTR sTabText, _U_STRINGorID bitmap, COLORREF crMask, HMODULE hModule = _Module.GetResourceInstance())
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -