// MyToolBar.cpp : 实现文件
//////////////////////////////////////////////////////////////////////////
//该类为工具栏类
//派生于CMFCToorBar类
//功能 用于修改工具条的背景颜色
//绝大多数部分来自MFC库,只修改了一点内容 在相应部分作出了声明
//////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MyToolBar.h"
// CMyToolBar
IMPLEMENT_DYNAMIC(CMyToolBar, CMFCToolBar)
CMyToolBar::CMyToolBar():m_Impl1(this)
{
}
CMyToolBar::~CMyToolBar()
{
}
//消息映射开始 由MFC 类向导自动生成
BEGIN_MESSAGE_MAP(CMyToolBar, CMFCToolBar)
ON_WM_NCPAINT()
END_MESSAGE_MAP()
//mfc框架 消息映射结束 类向导自动生成
// CMyToolBar 消息处理程序
//绘制 工具栏
void CMyToolBar::DoPaint(CDC* pDCPaint)
{
//////////////////////////////////////////////////////////////////////////
//CMFCToolBar里面的源代码
//不是自定义部分 请勿修改
//////////////////////////////////////////////////////////////////////////
ASSERT_VALID(this);
ASSERT_VALID(pDCPaint);
CRect rectClip;
pDCPaint->GetClipBox(rectClip);
BOOL bHorz = GetCurrentAlignment() & CBRS_ORIENT_HORZ ? TRUE : FALSE;
CRect rectClient;
GetClientRect(rectClient);
CMemDC memDC(*pDCPaint, this);
CDC* pDC = &memDC.GetDC();
/*pDC->FillSolidRect(rectClient,RGB(83,84,85));*/
if ((GetStyle() & TBSTYLE_TRANSPARENT) == 0)
{
/*CMFCVisualManager::GetInstance()->OnFillBarBackground(pDC, this, rectClient, rectClip);*/
}
pDC->SetTextColor(afxGlobalData.clrBtnText);
pDC->SetBkMode(TRANSPARENT);
//////////////////////////////////////////////////////////////////////////
//
//自定义绘制开始
//////////////////////////////////////////////////////////////////////////
//
//获得客户区 大小
CRect rect;
GetClientRect(rect);
//填充客户区
pDC->FillSolidRect(rect,RGB(83,84,85));
//
//自定义绘制结束
//////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////
//以下部分是CMFCToolBar里面的源代码
//不是自定义部分 请勿修改
//////////////////////////////////////////////////////////////////////////
/* Force the full size of the button:*/
if (bHorz)
{
rect.bottom = rect.top + GetRowHeight();
}
else
{
rect.right = rect.left + GetColumnWidth();
}
BOOL bDontScaleImages = m_bLocked ? m_bDontScaleLocked : FALSE;/*m_bDontScaleImages;*/
const double dblImageScale = bDontScaleImages ? 1.0 : afxGlobalData.GetRibbonImageScale();
CMFCToolBarImages* pImages = GetImageList(m_Images, m_ImagesLocked, m_LargeImages, m_LargeImagesLocked);
CMFCToolBarImages* pHotImages = pImages;
CMFCToolBarImages* pColdImages = GetImageList(m_ColdImages, m_ColdImagesLocked, m_LargeColdImages, m_LargeColdImagesLocked);
CMFCToolBarImages* pDisabledImages = GetImageList(m_DisabledImages, m_DisabledImagesLocked, m_LargeDisabledImages, m_LargeDisabledImagesLocked);
CMFCToolBarImages* pMenuImages = !m_bLocked ? &m_MenuImages : &m_MenuImagesLocked;
CMFCToolBarImages* pDisabledMenuImages = !m_bLocked ? &m_DisabledMenuImages : &m_DisabledMenuImagesLocked;
BOOL bDrawImages = pImages->IsValid();
pHotImages->SetTransparentColor(afxGlobalData.clrBtnFace);
BOOL bFadeInactiveImages = false;/*CMFCVisualManager::GetInstance()->IsFadeInactiveImage();*/
CSize sizeImageDest = m_bMenuMode ? m_sizeMenuImage : GetImageSize();
if (dblImageScale != 1.)
{
if (m_bMenuMode && sizeImageDest == CSize(-1, -1))
{
sizeImageDest = GetImageSize();
if (dblImageScale > 1. && m_bLargeIconsAreEnbaled)
{
sizeImageDest = m_sizeImage;
}
}
sizeImageDest = CSize((int)(.5 + sizeImageDest.cx * dblImageScale), (int)(.5 + sizeImageDest.cy * dblImageScale));
}
CAfxDrawState ds;
if (bDrawImages)
{
if (dblImageScale != 1.0 && pHotImages->GetScale() == 1.0)
{
pHotImages->SmoothResize(dblImageScale);
}
if (!pHotImages->PrepareDrawImage(ds, sizeImageDest, bFadeInactiveImages))
{
return; // something went wrong
}
}
CFont* pOldFont;
if (bHorz)
{
pOldFont = SelectDefaultFont(pDC);
}
else
{
pOldFont = (CFont*) pDC->SelectObject(&afxGlobalData.fontVert);
}
if (pColdImages->GetCount() > 0)
{
// Disable fade effect for inactive buttons:
CMFCVisualManager::GetInstance()->SetFadeInactiveImage(FALSE);
}
// Draw buttons:
int iButton = 0;
for (POSITION pos = m_Buttons.GetHeadPosition(); pos != NULL; iButton ++)
{
CMFCToolBarButton* pButton = (CMFCToolBarButton*) m_Buttons.GetNext(pos);
if (pButton == NULL)
{
break;
}
ASSERT_VALID(pButton);
rect = pButton->Rect();
CRect rectInter;
if (pButton->m_nStyle & TBBS_SEPARATOR)
{
BOOL bHorzSeparator = bHorz;
CRect rectSeparator; rectSeparator.SetRectEmpty();
OnCalcSeparatorRect(pButton, rectSeparator, bHorz);
if (pButton->m_bWrap && bHorz)
{
bHorzSeparator = FALSE;
}
if (rectInter.IntersectRect(rectSeparator, rectClip) && !pButton->IsHidden())
{
DrawSeparator(pDC, rectSeparator, bHorzSeparator);
}
continue;
}
if (!rectInter.IntersectRect(rect, rectClip))
{
continue;
}
BOOL bHighlighted = IsButtonHighlighted(iButton);
BOOL bDisabled = (pButton->m_nStyle & TBBS_DISABLED) && !IsCustomizeMode();
if (pDC->RectVisible(&rect))
{
BOOL bDrawDisabledImages = FALSE;
if (bDrawImages)
{
CMFCToolBarImages* pNewImages = NULL;
if (pButton->m_bUserButton)
{
if (pButton->GetImage() >= 0)
{
pNewImages = m_pUserImages;
}
}
else
{
if (m_bMenuMode)
{
if (bDisabled && pDisabledMenuImages->GetCount() > 0)
{
bDrawDisabledImages = TRUE;
pNewImages = pDisabledMenuImages;
}
else if (pMenuImages->GetCount() > 0)
{
pNewImages = pMenuImages;
}
else
{
bDrawDisabledImages = (bDisabled && pDisabledImages->GetCount() > 0);
pNewImages = bDrawDisabledImages ? pDisabledImages : pHotImages;
}
}
else // Toolbar mode
{
bDrawDisabledImages = (bDisabled && pDisabledImages->GetCount() > 0);
pNewImages = bDrawDisabledImages ? pDisabledImages : pHotImages;
if (!bHighlighted && !bDrawDisabledImages && (pButton->m_nStyle & TBBS_PRESSED) == 0 && pColdImages->GetCount() > 0 && !pButton->IsDroppedDown())
{
pNewImages = pColdImages;
}
}
}
if (bDrawImages && pNewImages != pImages && pNewImages != NULL)
{
pImages->EndDrawImage(ds);
pNewImages->SetTransparentColor(afxGlobalData.clrBtnFace);
if (dblImageScale != 1.0 && pNewImages->GetScale() == 1.0)
{
pNewImages->SmoothResize(dblImageScale);
}
pNewImages->PrepareDrawImage (ds, sizeImageDest, bFadeInactiveImages);
pImages = pNewImages;
}
}
DrawButton(pDC, pButton, bDrawImages ? pImages : NULL, bHighlighted, bDrawDisabledImages);
}
}
// Highlight selected button in the toolbar customization mode:
if (m_iSelected >= m_Buttons.GetCount())
{
m_iSelected = -1;
}
if ((IsCustomizeMode() || m_bAltCustomizeMode) && m_iSelected >= 0 && !m_bLocked && m_pSelToolbar == this)
{
CMFCToolBarButton* pSelButton = GetButton(m_iSelected);
ENSURE(pSelButton != NULL);
if (pSelButton != NULL && pSelButton->CanBeStored())
{
CRect rectDrag1 = pSelButton->Rect();
pDC->Draw3dRect(&rectDrag1, afxGlobalData.clrBtnText, afxGlobalData.clrBtnText);
rectDrag1.DeflateRect(1, 1);
pDC->Draw3dRect(&rectDrag1, afxGlobalData.clrBtnText, afxGlobalData.clrBtnText);
}
}
if (IsCustomizeMode() && m_iDragIndex >= 0 && !m_bLocked)
{
DrawDragCursor(pDC);
}
pDC->SelectObject(pOldFont);
if (bDrawImages)
{
pImages->EndDrawImage(ds);
}
CMFCVisualManager::Get