⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tabbedframe.h

📁 These listed libraries are written in WTL. But it s really hard to mix both MFC & WTL together. Obvi
💻 H
📖 第 1 页 / 共 3 页
字号:
		if(hWnd == NULL)
		{
			return -1;
		}

		int nImageIndex = this->AddBitmap(bitmap, crMask, hModule);

		return this->AddTab(hWnd, sTabText, nImageIndex);
	}

	// AddTabWithIcon (with a couple of overloaded versions)
	int AddTabWithIcon(HWND hWnd, LPCTSTR sTabText, HICON hIcon)
	{
		if(hWnd == NULL)
		{
			return -1;
		}

		int nImageIndex = this->AddIcon(hIcon);

		return this->AddTab(hWnd, sTabText, nImageIndex);
	}

	int AddTabWithIcon(HWND hWnd, LPCTSTR sTabText, _U_STRINGorID icon, HMODULE hModule = _Module.GetResourceInstance())
	{
		if(hWnd == NULL)
		{
			return -1;
		}

		int nImageIndex = this->AddIcon(icon, hModule);

		return this->AddTab(hWnd, sTabText, nImageIndex);
	}

	// AddTab - either referencing an image in the image list, or no image used
	int AddTab(HWND hWnd, LPCTSTR sTabText, int nImageIndex = -1)
	{
		if(hWnd == NULL)
		{
			return -1;
		}

		int nNewTabIndex = -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);

			int nOldCount = m_TabCtrl.GetItemCount();

			// The tab control takes ownership of the new item
			nNewTabIndex = m_TabCtrl.InsertItem(nOldCount, pItem);

			size_t nNewCount = m_TabCtrl.GetItemCount();

			if((nOldCount+1) == nNewCount)
			{
				T* pT = static_cast<T*>(this);
				pT->OnAddTab(nNewCount);
			}
		}

		return nNewTabIndex;
	}

	int DisplayTab(HWND hWnd, BOOL bAddIfNotFound = TRUE, BOOL bUseIcon = FALSE)
	{
		int nTab = -1;
		if(hWnd)
		{
			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;
				int 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)
							{
// need conditional code because types don't match in winuser.h
#ifdef _WIN64
								hIcon = (HICON)::GetClassLongPtr(hWnd, GCLP_HICONSM);
#else
								hIcon = (HICON)LongToHandle(::GetClassLongPtr(hWnd, GCLP_HICONSM));
#endif
							}
							if(hIcon == NULL)
							{
								hIcon = (HICON) ::SendMessage(hWnd, WM_GETICON, ICON_BIG, 0);
							}
							if(hIcon == NULL)
							{
// need conditional code because types don't match in winuser.h
#ifdef _WIN64
								hIcon = (HICON)::GetClassLongPtr(hWnd, GCLP_HICON);
#else
								hIcon = (HICON)LongToHandle(::GetClassLongPtr(hWnd, GCLP_HICON));
#endif
							}
						}

						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);
			}

		}

		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)
		{
			size_t nOldCount = m_TabCtrl.GetItemCount();

			bSuccess = m_TabCtrl.DeleteItem(nTab);

			size_t nNewCount = m_TabCtrl.GetItemCount();

			T* pT = static_cast<T*>(this);
			if((nOldCount-1) == nNewCount)
			{
				pT->OnRemoveTab(nNewCount);
			}
		}

		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_NS::CString sCurrentTabText = pItem->GetText();

			if(sText != NULL)
			{
				if(sCurrentTabText != sText)
				{
					bSuccess = pItem->SetText(sText);
					m_TabCtrl.UpdateLayout();
					m_TabCtrl.Invalidate();
				}
			}
			else
			{
				LPTSTR sWindowText = NULL;
				int 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_NS::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;
	}

	void SetImageSize(int cx, int cy)
	{
		m_cxImage = cx;
		m_cyImage = cy;
	}
};

/////////////////////////////////////////////////////////////////////////////
//
// 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 = WTL::CFrameWindowImpl<T, ATL::CWindow, ATL::CFrameWinTraits> >
class CTabbedFrameImpl :
	public TBase,
	public CCustomTabOwnerImpl<T, TTabCtrl>
{
protected:
	typedef CTabbedFrameImpl<T, TTabCtrl, TBase> thisClass;
	typedef TBase baseClass;
	typedef CCustomTabOwnerImpl<T, 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)
	{
		m_nMinTabCountForVisibleTabs = 1;
		m_bKeepTabsHidden = (m_nMinTabCountForVisibleTabs > 0);
	}

// 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;
	}

	void ModifyTabStyles(DWORD dwRemove, DWORD dwAdd)
	{
		DWORD dwNewStyle = (m_nTabStyles & ~dwRemove) | dwAdd;
		if(m_nTabStyles != dwNewStyle)
		{
			m_nTabStyles = dwNewStyle;
		}
	}

	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;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -