?? dotnettabctrl.h
字號:
#ifndef __DOTNET_TABCTRL_H__
#define __DOTNET_TABCTRL_H__
#pragma once
/////////////////////////////////////////////////////////////////////////////
// CDotNetTabCtrl - Tab control derived from CCustomTabCtrl
// meant to look like the tabs in VS.Net (MDI tabs,
// solution explorer tabs, etc.)
// CDotNetButtonTabCtrl - Tab control derived from CCustomTabCtrl
// meant to look like VS.Net view of HTML with the Design/HTML buttons
//
//
// Written by Daniel Bowen (dbowen@es.com).
// Copyright (c) 2001-2004 Daniel Bowen.
//
// 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. It's free, so don't hassle me about it.
//
// Beware of bugs.
//
// History (Date/Author/Description):
// ----------------------------------
//
// 2005/07/13: Daniel Bowen
// - Namespace qualify the use of more ATL and WTL classes.
//
// 2005/03/14: Daniel Bowen
// - Fix warnings when compiling for 64-bit.
//
// 2004/06/28: Daniel Bowen:
// - Clean up warnings on level 4
//
// 2004/06/21: Peter Carlson:
// - "CanClose" for items.
//
// 2004/04/29: Daniel Bowen
// - Update CDotNetTabCtrlImpl::OnSettingChange so that
// if the color depth is not greater than 8bpp
// (so, 256 colors or less), instead of this
// m_hbrBackground = CDCHandle::GetHalftoneBrush();
// (which makes things unreadable at 256 colors), do this:
// m_hbrBackground.CreateSysColorBrush(COLOR_WINDOW);
// - Support for highlighting items.
// Add CCustomTabItem::GetHighlight/SetHighlight.
// Uses the custom draw state CDIS_MARKED.
//
// 2004/01/06: Daniel Bowen
// - Fix compile issue under ICL thanks to Richard Crossley.
// CDotNetButtonTabCtrl missing template parameters.
//
// 2003/08/01: Daniel Bowen
// - CDotNetButtonTabCtrl can now be used for MDI tabs.
// - There is now a CDotNetButtonTabCtrlImpl, which
// CDotNetButtonTabCtrl inherits from.
// - Reorganize so that CDotNetTabCtrlImpl is broken up a little bit
// more into overridable pieces, and have CDotNetButtonTabCtrlImpl
// inherit from CDotNetTabCtrlImpl. Have CDotNetButtonTabCtrlImpl
// only override the parts of CDotNetTabCtrlImpl needed.
//
// 2003/06/27: Daniel Bowen
// - Replace DECLARE_WND_CLASS with DECLARE_WND_CLASS_EX.
// Use CS_DBLCLKS as the style and COLOR_WINDOW as the brush -
// which essentially means that it doesn't use CS_HREDRAW or CS_VREDRAW now.
//
// 2002/12/05: Daniel Bowen
// - In CDotNetTabCtrlImpl::UpdateLayout_Default,
// the code that calculates nRatioWithSelectionFullSize
// would sometimes get a 0 in the divisor.
// Guard against divide-by-0.
// - Handle WM_SYSCOLORCHANGE in case its broadcast to us
// from a top-level window. Call OnSettingChange.
//
// 2002/11/13: Daniel Bowen
// - CDotNetTabCtrl:
// * Tweaks so that CDotNetTabCtrl matches just a little more
// closely to the tabs in VS.NET (when tabs are on top).
// * Override new "CalcSize_NonClient" method as part of the
// tweaks for CDotNetTabCtrl. Currently, only the left
// and right sides of the client area are adjusted for
// non-client areas (since the drawing code already considers
// non-client areas above and below in its drawing).
// In the future, non-client areas could be accounted for
// in CalcSize_NonClient, and the drawing code could
// be updated appropriately.
// * New CTCS_FLATEDGE style. CDotNetTabCtrl asks about this
// style when drawing the outline around the tab. A good
// use for this style is if you are using the tab control
// for the MDI tabs, and you have your MDIClient drawn flat
// (like VS.NET)
//
// 2002/07/16: Daniel Bowen
// - Fix problem that would cause applications using CDotNetTabCtrl
// with CTCS_CLOSEBUTTON and/or CTCS_SCROLL to not exit "cleanly".
// There were 2 places referencing m_tooltip that needed to
// first check if(m_tooltip.IsWindow()).
//
// 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 __CUSTOMTABCTRL_H__
#include "CustomTabCtrl.h"
#endif
// NOTE: If you are compiling under VC7, be sure to put the following in
// your precompiled header:
//
//extern "C" const int _fltused = 0;
template<typename T, typename TItem = CCustomTabItem, class TBase = ATL::CWindow, class TWinTraits = CCustomTabCtrlWinTraits>
class CDotNetTabCtrlImpl :
public CCustomTabCtrl<T, TItem, TBase, TWinTraits>
{
protected:
typedef CDotNetTabCtrlImpl<T, TItem, TBase, TWinTraits> thisClass;
typedef CCustomTabCtrl<T, TItem, TBase, TWinTraits> customTabClass;
protected:
CBrush m_hbrBackground;
COLORREF m_clrTextInactiveTab, m_clrSelectedTab;
signed char m_nFontSizeTextTopOffset;
const signed char m_nMinWidthToDisplayText;
// Constructor
public:
CDotNetTabCtrlImpl() :
m_clrTextInactiveTab(::GetSysColor(COLOR_GRAYTEXT)),
m_clrSelectedTab(::GetSysColor(COLOR_BTNFACE)),
m_nFontSizeTextTopOffset(0),
m_nMinWidthToDisplayText(12)
{
}
// Message Handling
public:
DECLARE_WND_CLASS_EX(_T("WTL_DotNetTabCtrl"), CS_DBLCLKS, COLOR_WINDOW)
BEGIN_MSG_MAP(thisClass)
MESSAGE_HANDLER(WM_SETTINGCHANGE, OnSettingChange)
MESSAGE_HANDLER(WM_SYSCOLORCHANGE, OnSettingChange)
CHAIN_MSG_MAP(customTabClass)
END_MSG_MAP()
LRESULT OnSettingChange(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
DWORD dwStyle = this->GetStyle();
// Initialize/Reinitialize font
// Visual Studio.Net seems to use the "icon" font for the tabs
LOGFONT lfIcon = { 0 };
::SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(lfIcon), &lfIcon, 0);
bool bResetFont = true;
if(!m_font.IsNull())
{
LOGFONT lf = {0};
if(m_font.GetLogFont(&lf))
{
if(lstrcmpi(lf.lfFaceName, lfIcon.lfFaceName) == 0 &&
lf.lfHeight == lfIcon.lfHeight)
{
bResetFont = false;
}
}
}
if(bResetFont)
{
if(!m_font.IsNull()) m_font.DeleteObject();
if(!m_fontSel.IsNull()) m_fontSel.DeleteObject();
HFONT font = m_font.CreateFontIndirect(&lfIcon);
if(font==NULL)
{
m_font.Attach(AtlGetDefaultGuiFont());
}
if(CTCS_BOLDSELECTEDTAB == (dwStyle & CTCS_BOLDSELECTEDTAB))
{
lfIcon.lfWeight = FW_BOLD;
}
font = m_fontSel.CreateFontIndirect(&lfIcon);
if(font==NULL)
{
m_fontSel.Attach(AtlGetDefaultGuiFont());
}
}
// Background brush
if(!m_hbrBackground.IsNull()) m_hbrBackground.DeleteObject();
WTL::CWindowDC dcWindow(NULL);
int nBitsPerPixel = dcWindow.GetDeviceCaps(BITSPIXEL);
if(nBitsPerPixel > 8)
{
//COLORREF clrBtnHilite = ::GetSysColor(COLOR_BTNHILIGHT);
//COLORREF clrBtnFace = ::GetSysColor(COLOR_BTNFACE);
//m_clrBackground =
// RGB( GetRValue(clrBtnFace) + ((GetRValue(clrBtnHilite) - GetRValue(clrBtnFace)) / 3 * 2),
// GetGValue(clrBtnFace) + ((GetGValue(clrBtnHilite) - GetGValue(clrBtnFace)) / 3 * 2),
// GetBValue(clrBtnFace) + ((GetBValue(clrBtnHilite) - GetBValue(clrBtnFace)) / 3 * 2),
// );
//m_hbrBackground.CreateSolidBrush(m_clrBackground);
COLORREF clrBtnFace = ::GetSysColor(COLOR_BTNFACE);
// This is a brave attempt to mimic the algorithm that Visual Studio.Net
// uses to calculate the tab's background color and inactive tab color.
// The other colors that VS.Net uses seems to be standard ones,
// but these two colors are calculated.
BYTE nRed = 0, nGreen = 0, nBlue = 0, nMax = 0;
// Early experiments seemed to reveal that the background color is dependant
// on COLOR_BTNFACE. The following algorithm is just an attempt
// to match several empirical results. I tested with 20 variations
// on COLOR_BTNFACE and kept track of what the tab background became.
// I then brought the numbers into Excel, and started crunching on the numbers
// until I came up with a formula that seems to pretty well match.
nRed = GetRValue(clrBtnFace);
nGreen = GetGValue(clrBtnFace);
nBlue = GetBValue(clrBtnFace);
nMax = (nRed > nGreen) ? ((nRed > nBlue) ? nRed : nBlue) : ((nGreen > nBlue) ? nGreen : nBlue);
const BYTE nMagicBackgroundOffset = (nMax > (0xFF - 35)) ? (BYTE)(0xFF - nMax) : (BYTE)35;
if(nMax == 0)
{
nRed = (BYTE)(nRed + nMagicBackgroundOffset);
nGreen = (BYTE)(nGreen + nMagicBackgroundOffset);
nBlue = (BYTE)(nBlue + nMagicBackgroundOffset);
}
else
{
nRed = (BYTE)(nRed + (nMagicBackgroundOffset*(nRed/(double)nMax) + 0.5));
nGreen = (BYTE)(nGreen + (nMagicBackgroundOffset*(nGreen/(double)nMax) + 0.5));
nBlue = (BYTE)(nBlue + (nMagicBackgroundOffset*(nBlue/(double)nMax) + 0.5));
}
m_hbrBackground.CreateSolidBrush(RGB(nRed, nGreen, nBlue));
// The inactive tab color seems to be calculated in a similar way to
// the tab background, only instead of lightening BNTFACE, it darkens GRAYTEXT.
COLORREF clrGrayText = ::GetSysColor(COLOR_GRAYTEXT);
nRed = GetRValue(clrGrayText);
nGreen = GetGValue(clrGrayText);
nBlue = GetBValue(clrGrayText);
nMax = (nRed > nGreen) ? ((nRed > nBlue) ? nRed : nBlue) : ((nGreen > nBlue) ? nGreen : nBlue);
const BYTE nMagicInactiveOffset = 43;
if(nMax != 0)
{
if(nRed < nMagicInactiveOffset)
nRed = (BYTE)(nRed / 2);
else
nRed = (BYTE)(nRed - (nMagicInactiveOffset*(nRed/(double)nMax) + 0.5));
if(nGreen < nMagicInactiveOffset)
nGreen = (BYTE)(nGreen / 2);
else
nGreen = (BYTE)(nGreen - (nMagicInactiveOffset*(nGreen/(double)nMax) + 0.5));
if(nBlue < nMagicInactiveOffset)
nBlue = (BYTE)(nBlue / 2);
else
nBlue = (BYTE)(nBlue - (nMagicInactiveOffset*(nBlue/(double)nMax) + 0.5));
}
m_clrTextInactiveTab = RGB(nRed, nGreen, nBlue);
}
else
{
m_hbrBackground.CreateSysColorBrush(COLOR_WINDOW);
m_clrTextInactiveTab = ::GetSysColor(COLOR_GRAYTEXT);
}
m_settings.iIndent = 5;
m_settings.iPadding = 4;
m_settings.iMargin = 3;
m_settings.iSelMargin = 3;
int nHeightLogicalUnits = -lfIcon.lfHeight;
// In MSDN for "LOGFONT", they give the following formula for calculating
// the log font height given a point size.
//long lfHeight = -MulDiv(PointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);
const int nNominalFontLogicalUnits = 11; // 8 point Tahoma with 96 DPI
m_nFontSizeTextTopOffset = (BYTE)((nHeightLogicalUnits - nNominalFontLogicalUnits) / 2);
T* pT = static_cast<T*>(this);
pT->UpdateLayout();
pT->Invalidate();
return 0;
}
// Overrideables
public:
void DrawBackground(RECT rcClient, LPNMCTCCUSTOMDRAW lpNMCustomDraw)
{
WTL::CDCHandle dc( lpNMCustomDraw->nmcd.hdc );
// Set up the text color and background mode
dc.SetTextColor(lpNMCustomDraw->clrBtnText);
dc.SetBkMode(TRANSPARENT);
RECT rcClip = {0};
dc.GetClipBox(&rcClip);
if(::EqualRect(&rcClip, &m_rcCloseButton) ||
::EqualRect(&rcClip, &m_rcScrollRight) ||
::EqualRect(&rcClip, &m_rcScrollLeft))
{
// Paint needed in only "other button" area
HBRUSH hOldBrush = dc.SelectBrush(lpNMCustomDraw->hBrushBackground);
dc.PatBlt(rcClip.left, rcClip.top, rcClip.right-rcClip.left, rcClip.bottom-rcClip.top, PATCOPY);
dc.SelectBrush(hOldBrush);
}
else
{
// Paint needed in tab item area or more
// Erase Background
// (do it here instead of a handler for WM_ERASEBKGND
// so that we can do flicker-free drawing with the help
// of COffscreenDrawRect that's in the base class)
// TODO: Don't "erase" entire client area.
// Do a smarter erase of just what needs it
RECT rc = rcClient;
HBRUSH hOldBrush = dc.SelectBrush(lpNMCustomDraw->hBrushBackground);
dc.PatBlt(rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, PATCOPY);
dc.SelectBrush(hOldBrush);
// Connect with the client area.
DWORD dwStyle = this->GetStyle();
if(CTCS_BOTTOM == (dwStyle & CTCS_BOTTOM))
{
rc.bottom = rc.top + 3;
dc.FillSolidRect(&rc, lpNMCustomDraw->clrBtnFace);
CPen penText;
penText.CreatePen(PS_SOLID, 1, lpNMCustomDraw->clrBtnText);
CPenHandle penOld = dc.SelectPen(penText);
dc.MoveTo(rc.left, rc.bottom);
dc.LineTo(rc.right, rc.bottom);
dc.SelectPen(penOld);
}
else
{
int nOrigTop = rc.top;
rc.top = rc.bottom - 2;
dc.FillSolidRect(&rc, lpNMCustomDraw->clrBtnFace);
CPen penHilight;
penHilight.CreatePen(PS_SOLID, 1, lpNMCustomDraw->clrBtnHighlight);
CPenHandle penOld = dc.SelectPen(penHilight);
dc.MoveTo(rc.left, rc.top-1);
dc.LineTo(rc.right, rc.top-1);
rc.top = nOrigTop;
CPen penShadow, pen3D;
penShadow.CreatePen(PS_SOLID, 1, lpNMCustomDraw->clrBtnShadow);
pen3D.CreatePen(PS_SOLID, 1, lpNMCustomDraw->clrBtnFace);
dc.SelectPen(penShadow);
dc.MoveTo(rc.left, rc.bottom);
dc.LineTo(rc.left, rc.top);
dc.LineTo(rc.right-1, rc.top);
if(0 == (dwStyle & CTCS_FLATEDGE))
{
dc.SelectPen(penHilight);
}
dc.LineTo(rc.right-1, rc.bottom);
dc.SelectPen(pen3D);
dc.MoveTo(rc.right-2, rc.bottom-3);
dc.LineTo(rc.right-2, rc.top);
dc.MoveTo(rc.left+1, rc.bottom-3);
dc.LineTo(rc.left+1, rc.top);
dc.SelectPen(penOld);
}
}
}
void DrawItem_InitBounds(DWORD dwStyle, RECT rcItem, RECT& rcTab, RECT& rcText, int& nIconVerticalCenter)
{
if(CTCS_BOTTOM == (dwStyle & CTCS_BOTTOM))
{
rcTab.top += 3;
rcTab.bottom -= 2;
rcText.top = rcTab.top+1 + m_nFontSizeTextTopOffset;
rcText.bottom = rcItem.bottom;
//nIconVerticalCenter = rcTab.top + (rc.bottom - rcTab.top) / 2;
//nIconVerticalCenter = rcTab.top + rcText.Height() / 2;
nIconVerticalCenter = (rcItem.bottom + rcItem.top) / 2 + rcTab.top / 2;
}
else
{
rcTab.top += 3;
rcTab.bottom -= 2;
rcText.top = rcItem.top+1 + m_nFontSizeTextTopOffset;
rcText.bottom = rcItem.bottom;
nIconVerticalCenter = (rcItem.bottom + rcItem.top) / 2 + rcTab.top / 2;
}
}
void DrawItem_TabSelected(DWORD dwStyle, LPNMCTCCUSTOMDRAW lpNMCustomDraw, RECT& rcTab)
{
// Tab is selected, so paint tab folder
bool bHighlighted = (CDIS_MARKED == (lpNMCustomDraw->nmcd.uItemState & CDIS_MARKED));
WTL::CDCHandle dc(lpNMCustomDraw->nmcd.hdc);
rcTab.right--;
if(bHighlighted)
{
dc.FillSolidRect(&rcTab, lpNMCustomDraw->clrHighlight);
}
else
{
dc.FillSolidRect(&rcTab, lpNMCustomDraw->clrSelectedTab);
}
WTL::CPen penText, penHilight;
penText.CreatePen(PS_SOLID, 1, lpNMCustomDraw->clrBtnText);
penHilight.CreatePen(PS_SOLID, 1, lpNMCustomDraw->clrBtnHighlight);
if(CTCS_BOTTOM == (dwStyle & CTCS_BOTTOM))
{
WTL::CPenHandle penOld = dc.SelectPen(penText);
dc.MoveTo(rcTab.right, rcTab.top);
dc.LineTo(rcTab.right, rcTab.bottom);
dc.LineTo(rcTab.left, rcTab.bottom);
dc.SelectPen(penHilight);
dc.LineTo(rcTab.left, rcTab.top-1);
dc.SelectPen(penOld);
}
else
{
WTL::CPenHandle penOld = dc.SelectPen(penHilight);
dc.MoveTo(rcTab.left, rcTab.bottom-1);
dc.LineTo(rcTab.left, rcTab.top);
dc.LineTo(rcTab.right, rcTab.top);
dc.SelectPen(penText);
dc.LineTo(rcTab.right, rcTab.bottom);
dc.SelectPen(penOld);
}
}
void DrawItem_TabInactive(DWORD dwStyle, LPNMCTCCUSTOMDRAW lpNMCustomDraw, RECT& rcTab)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -