?? mytabctrl.cpp
字號(hào):
// MyTabCtrl.cpp : implementation file
//
/////////////////////////////////////////////////////
// This class is provided as is and Ben Hill takes no
// responsibility for any loss of any kind in connection
// to this code.
/////////////////////////////////////////////////////
// Is is meant purely as a educational tool and may
// contain bugs.
/////////////////////////////////////////////////////
// ben@shido.fsnet.co.uk
// http://www.shido.fsnet.co.uk
/////////////////////////////////////////////////////
// Thanks to a mystery poster in the C++ forum on
// www.codeguru.com I can't find your name to say thanks
// for your Control drawing code. If you are that person
// thank you very much. I have been able to use some of
// you ideas to produce this sample application.
/////////////////////////////////////////////////////
#include "stdafx.h"
#include "Resource.h"
#include "MyTabCtrl.h"
#include "..\Button\DlgBasic.h"
#include "..\Button\DlgAdvanced.h"
#include "..\Button\DlgTransparent.h"
#include "..\Button\DlgShadeButtonST.h"
#include "..\Button\CustomButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyTabCtrl
DWORD CMyTabCtrl::s_dwCustomLook = 0;
enum { PADDING = 3, EDGE = 20};
CMyTabCtrl::CMyTabCtrl()
{
::ZeroMemory(&m_tabPages, sizeof(m_tabPages));
m_tabPages[0]=new CDlgBasic;
m_tabPages[1]=new CDlgAdvanced;
m_tabPages[2]=new CDlgTransparent;
m_tabPages[3]=new CDlgShadeButtonST;
m_tabPages[4]=new CCustomButton;
m_nNumberOfPages=5;
}
CMyTabCtrl::~CMyTabCtrl()
{
for(int nCount=0; nCount < m_nNumberOfPages; nCount++){
delete m_tabPages[nCount];
}
}
void CMyTabCtrl::Init()
{
m_tabCurrent=0;
m_tabPages[0]->Create(IDD_BASIC, this);
m_tabPages[1]->Create(IDD_ADVANCED, this);
m_tabPages[2]->Create(IDD_TRANSPARENT, this);
m_tabPages[3]->Create(IDD_SHADED, this);
m_tabPages[4]->Create(IDD_BROWSECTRLDEMO_DIALOG, this);
m_tabPages[0]->ShowWindow(SW_SHOW);
m_tabPages[1]->ShowWindow(SW_HIDE);
m_tabPages[2]->ShowWindow(SW_HIDE);
m_tabPages[3]->ShowWindow(SW_HIDE);
m_tabPages[4]->ShowWindow(SW_HIDE);
SetRectangle();
}
void CMyTabCtrl::SetRectangle()
{
CRect tabRect, itemRect;
int nX, nY, nXc, nYc;
GetClientRect(&tabRect);
GetItemRect(0, &itemRect);
nX=itemRect.left;
nY=itemRect.bottom+1;
nXc=tabRect.right-itemRect.left-1;
nYc=tabRect.bottom-nY-1;
m_tabPages[0]->SetWindowPos(&wndTop, nX, nY, nXc, nYc, SWP_SHOWWINDOW);
for(int nCount=1; nCount < m_nNumberOfPages; nCount++)
{
m_tabPages[nCount]->SetWindowPos(&wndTop, nX, nY, nXc, nYc, SWP_HIDEWINDOW);
}
}
BEGIN_MESSAGE_MAP(CMyTabCtrl, CBaseTabCtrl)
//{{AFX_MSG_MAP(CMyTabCtrl)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyTabCtrl message handlers
void CMyTabCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
CBaseTabCtrl::OnLButtonDown(nFlags, point);
if(m_tabCurrent != GetCurFocus()){
m_tabPages[m_tabCurrent]->ShowWindow(SW_HIDE);
m_tabCurrent=GetCurFocus();
m_tabPages[m_tabCurrent]->ShowWindow(SW_SHOW);
m_tabPages[m_tabCurrent]->SetFocus();
}
}
void CMyTabCtrl::DrawItem(LPDRAWITEMSTRUCT lpdis)
{
TC_ITEM tci;
CDC* pDC = CDC::FromHandle(lpdis->hDC);
HIMAGELIST hilTabs = (HIMAGELIST)TabCtrl_GetImageList(GetSafeHwnd());
BOOL bSelected = (lpdis->itemID == (UINT)GetCurSel());
BOOL bColor = (s_dwCustomLook & ETC_COLOR);
CRect rItem(lpdis->rcItem);
if (bSelected)
rItem.bottom -= 1;
else
rItem.bottom += 2;
// tab
// blend from back color to COLOR_3DFACE if 16 bit mode or better
COLORREF crFrom = GetTabColor(bSelected);
if (s_dwCustomLook & ETC_GRADIENT && pDC->GetDeviceCaps(BITSPIXEL) >= 16)
{
COLORREF crTo = bSelected ? ::GetSysColor(COLOR_3DFACE) : Darker(!bColor || m_crBack == -1 ? ::GetSysColor(COLOR_3DFACE) : m_crBack, 0.7f);
int nROrg = GetRValue(crFrom);
int nGOrg = GetGValue(crFrom);
int nBOrg = GetBValue(crFrom);
int nRDiff = GetRValue(crTo) - nROrg;
int nGDiff = GetGValue(crTo) - nGOrg;
int nBDiff = GetBValue(crTo) - nBOrg;
int nHeight = rItem.Height();
for (int nLine = 0; nLine < nHeight; nLine += 2)
{
int nRed = nROrg + (nLine * nRDiff) / nHeight;
int nGreen = nGOrg + (nLine * nGDiff) / nHeight;
int nBlue = nBOrg + (nLine * nBDiff) / nHeight;
pDC->FillSolidRect(CRect(rItem.left, rItem.top + nLine, rItem.right, rItem.top + nLine + 2),
RGB(nRed, nGreen, nBlue));
}
}
else // simple solid fill
pDC->FillSolidRect(rItem, crFrom);
// text & icon
rItem.left += PADDING;
rItem.top += PADDING + (bSelected ? 1 : 0);
pDC->SetBkMode(TRANSPARENT);
CString sTemp;
tci.mask = TCIF_TEXT | TCIF_IMAGE;
tci.pszText = sTemp.GetBuffer(100);
tci.cchTextMax = 99;
GetItem(lpdis->itemID, &tci);
sTemp.ReleaseBuffer();
// icon
if (hilTabs)
{
ImageList_Draw(hilTabs, tci.iImage, *pDC, rItem.left, rItem.top, ILD_TRANSPARENT);
rItem.left += 16 + PADDING;
}
// text
rItem.right -= PADDING;
FormatText(sTemp, pDC, rItem.Width());
pDC->SetTextColor(GetTabTextColor(bSelected));
pDC->DrawText(sTemp, rItem, DT_NOPREFIX | DT_LEFT);
}
void CMyTabCtrl::DrawItemBorder(LPDRAWITEMSTRUCT lpdis)
{
ASSERT (s_dwCustomLook & ETC_FLAT);
BOOL bSelected = (lpdis->itemID == (UINT)GetCurSel());
BOOL bBackTabs = (s_dwCustomLook & ETC_BACKTABS);
CRect rItem(lpdis->rcItem);
CDC* pDC = CDC::FromHandle(lpdis->hDC);
COLORREF crTab = GetTabColor(bSelected);
COLORREF crHighlight = Lighter(crTab, 1.5f);
COLORREF crShadow = Darker(crTab, 0.75f);
if (bSelected || bBackTabs)
{
rItem.bottom += bSelected ? -1 : 1;
// edges
pDC->FillSolidRect(CRect(rItem.left, rItem.top, rItem.left + 1, rItem.bottom), crHighlight);
pDC->FillSolidRect(CRect(rItem.left, rItem.top, rItem.right, rItem.top + 1), crHighlight);
pDC->FillSolidRect(CRect(rItem.right - 1, rItem.top, rItem.right, rItem.bottom), crShadow);
}
else // draw simple dividers
{
pDC->FillSolidRect(CRect(rItem.left - 1, rItem.top, rItem.left, rItem.bottom), crShadow);
pDC->FillSolidRect(CRect(rItem.right - 1, rItem.top, rItem.right, rItem.bottom), crShadow);
}
}
void CMyTabCtrl::DrawMainBorder(LPDRAWITEMSTRUCT lpdis)
{
CRect rBorder(lpdis->rcItem);
CDC* pDC = CDC::FromHandle(lpdis->hDC);
COLORREF crTab = GetTabColor();
COLORREF crHighlight = Lighter(crTab, 1.5f);
COLORREF crShadow = Darker(crTab, 0.75f);
pDC->Draw3dRect(rBorder, crHighlight, crShadow);
}
COLORREF CMyTabCtrl::GetTabColor(BOOL bSelected)
{
BOOL bColor = (s_dwCustomLook & ETC_COLOR);
BOOL bHiliteSel = (s_dwCustomLook & ETC_SELECTION);
BOOL bBackTabs = (s_dwCustomLook & ETC_BACKTABS);
BOOL bFlat = (s_dwCustomLook & ETC_FLAT);
if (bSelected && bHiliteSel)
{
if (bColor)
return Lighter((m_crBack == -1) ? ::GetSysColor(COLOR_3DFACE) : m_crBack, 1.4f);
else
return Lighter(::GetSysColor(COLOR_3DFACE), 1.4f);
}
else if (!bSelected)
{
if (bBackTabs || !bFlat)
{
if (bColor)
return Darker((m_crBack == -1) ? ::GetSysColor(COLOR_3DFACE) : m_crBack, 0.9f);
else
return Darker(::GetSysColor(COLOR_3DFACE), 0.9f);
}
else
return (m_crBack == -1) ? ::GetSysColor(COLOR_3DFACE) : m_crBack;
}
// else
return ::GetSysColor(COLOR_3DFACE);
}
COLORREF CMyTabCtrl::GetTabTextColor(BOOL bSelected)
{
BOOL bColor = (s_dwCustomLook & ETC_COLOR);
BOOL bFlat = (s_dwCustomLook & ETC_FLAT);
if (bSelected)
{
return ::GetSysColor(COLOR_WINDOWTEXT);
}
else
{
if (bColor || bFlat)
return Darker((m_crBack == -1) ? ::GetSysColor(COLOR_3DFACE) : m_crBack, 0.5f);
else
return Darker(::GetSysColor(COLOR_3DFACE), 0.5f);
}
// else
return Darker(::GetSysColor(COLOR_3DFACE), 0.5f);
}
void CMyTabCtrl::EnableCustomLook(BOOL bEnable, DWORD dwStyle)
{
if (!bEnable)
dwStyle = 0;
s_dwCustomLook = dwStyle;
}
void CMyTabCtrl::PreSubclassWindow()
{
CBaseTabCtrl::PreSubclassWindow();
if (s_dwCustomLook)
ModifyStyle(0, TCS_OWNERDRAWFIXED);
}
COLORREF CMyTabCtrl::Darker(COLORREF crBase, float fFactor)
{
ASSERT (fFactor < 1.0f && fFactor > 0.0f);
fFactor = min(fFactor, 1.0f);
fFactor = max(fFactor, 0.0f);
BYTE bRed, bBlue, bGreen;
BYTE bRedShadow, bBlueShadow, bGreenShadow;
bRed = GetRValue(crBase);
bBlue = GetBValue(crBase);
bGreen = GetGValue(crBase);
bRedShadow = (BYTE)(bRed * fFactor);
bBlueShadow = (BYTE)(bBlue * fFactor);
bGreenShadow = (BYTE)(bGreen * fFactor);
return RGB(bRedShadow, bGreenShadow, bBlueShadow);
}
COLORREF CMyTabCtrl::Lighter(COLORREF crBase, float fFactor)
{
ASSERT (fFactor > 1.0f);
fFactor = max(fFactor, 1.0f);
BYTE bRed, bBlue, bGreen;
BYTE bRedHilite, bBlueHilite, bGreenHilite;
bRed = GetRValue(crBase);
bBlue = GetBValue(crBase);
bGreen = GetGValue(crBase);
bRedHilite = (BYTE)min((int)(bRed * fFactor), 255);
bBlueHilite = (BYTE)min((int)(bBlue * fFactor), 255);
bGreenHilite = (BYTE)min((int)(bGreen * fFactor), 255);
return RGB(bRedHilite, bGreenHilite, bBlueHilite);
}
CSize CMyTabCtrl::FormatText(CString& sText, CDC* pDC, int nWidth)
{
CRect rect(0, 0, nWidth, 20);
UINT uFlags = DT_CALCRECT | DT_SINGLELINE | DT_MODIFYSTRING | DT_END_ELLIPSIS;
::DrawText(pDC->GetSafeHdc(), sText.GetBuffer(sText.GetLength() + 4), -1, rect, uFlags);
sText.ReleaseBuffer();
return pDC->GetTextExtent(sText);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -