www.pudn.com > ToolBar_ATL.rar > ToolBarView.cpp
// ToolBarView.cpp: implementation of the CToolBarView class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ktoolbar.h"
#include "ToolBarView.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CToolBarView::CToolBarView(CToolBarDoc* pDoc)
{
m_pDoc = pDoc;
m_hWnd = NULL;
m_hInstance = NULL;
m_hMemDC = NULL;
m_hMemBitmap = NULL;
m_hMemDcBkg = NULL;
m_hMemDcCover = NULL;
m_hBitmapBkg = NULL;
// m_pTBItems = (TBItem*)malloc(3*sizeof(TBItem));
m_BrushDown = CreateSolidBrush(RGB(254, 211, 142));
m_BrushHover = CreateSolidBrush(RGB(255, 245, 206));
m_StartColor = RGB(129, 169, 226); //RGB(153, 151, 180);
m_EndColor = RGB(221, 236, 254);//RGB(244, 244, 251);
InitToolTips();
}
CToolBarView::~CToolBarView()
{
if(m_BrushDown){
DeleteObject(m_BrushDown);
m_BrushDown = NULL;
}
if(m_BrushHover){
DeleteObject(m_BrushHover);
m_BrushHover = NULL;
}
if(m_hMemDC){
DeleteObject(m_hMemDC);
m_hMemDC = NULL;
}
if(m_hMemBitmap){
DeleteObject(m_hMemBitmap);
m_hMemBitmap = NULL;
}
if(m_hMemDcBkg){
DeleteObject(m_hMemDcBkg);
m_hMemDcBkg = NULL;
}
if(m_hBitmapBkg){
DeleteObject(m_hBitmapBkg);
m_hBitmapBkg = NULL;
}
if(m_hMemDcCover){
DeleteObject(m_hMemDcCover);
m_hMemDcCover = NULL;
}
if(m_hBitmapCover){
DeleteObject(m_hBitmapCover);
m_hBitmapCover = NULL;
}
if(m_hWndToolTips){
DestroyWindow(m_hWndToolTips);
}
}
void CToolBarView::ReDraw(HDC aDC, BOOL bReDrawAll)
{
try{
int DrawWidth = m_RectBar.right-m_RectBar.left;
int DrawHeight = m_RectBar.bottom-m_RectBar.top;
int i;
TBItem *pBtn;
int nPrevHoverIndex;
int nCurHoverIndex;
HPEN hPen;
int nBtnCnt;
int nSpaceX,nSpaceY;
nSpaceX = (m_HoverRectWidth - m_IconWidth)/2;
nSpaceY = (m_HoverRectHeight - m_IconHeight)/2;
nPrevHoverIndex = m_pDoc->GetPrevHoverIndex();
nCurHoverIndex = m_pDoc->GetCurHoverIndex();
//Only redraw part of buttons. Not necessary to draw all the buttons.
if(bReDrawAll == FALSE){
//Resume previous hover button.
HideToolTips();
if(nPrevHoverIndex != -1 && nPrevHoverIndex != nCurHoverIndex){
pBtn = m_pDoc->GetButton(nPrevHoverIndex);
BitBlt(m_hMemDC,pBtn->cx,0,DrawHeight,DrawHeight,m_hMemDcCover,0,0,SRCCOPY);
if(pBtn->Icon){
DrawIcon(m_hMemDC,pBtn->cx+nSpaceX,pBtn->cy+nSpaceY,pBtn->Icon,pBtn->Enabled);
}
m_pDoc->SetPrevHoverIndex(-1);
ATLTRACE(_T("Trace resume.\n"));
}
if(nCurHoverIndex!=-1){
pBtn = m_pDoc->GetButton(nCurHoverIndex);
if(pBtn->Style == BTN_ICON){
ShowToolTips(pBtn->ToolTips);
if(pBtn->Enabled == TRUE){
//Light current hover button.
if(pBtn->State == BTN_DOWN){
SelectObject(m_hMemDC, m_BrushDown);
}else{
SelectObject(m_hMemDC, m_BrushHover);
}
hPen = CreatePen(0,1,RGB(0,0,0));
SelectObject(m_hMemDC, hPen);
::Rectangle(m_hMemDC, pBtn->cx, pBtn->cy,pBtn->cx+m_HoverRectWidth, pBtn->cy+m_HoverRectHeight );
DeleteObject(hPen);
}
if(pBtn->Icon){
DrawIcon(m_hMemDC,pBtn->cx+nSpaceX,pBtn->cy+nSpaceY,pBtn->Icon,pBtn->Enabled);
}
//Button style is dropdown list.
}else{
}
}
BitBlt(aDC, m_RectBar.left, m_RectBar.top,DrawWidth,DrawHeight,m_hMemDC,0,0,SRCCOPY);
return ;
}
//Redraw the background and icon of toolbar.
if(m_hMemDC){
DeleteObject(m_hMemDC);
}
m_hMemDC = CreateCompatibleDC(aDC);
if(m_hMemBitmap){
DeleteObject(m_hMemBitmap);
}
m_hMemBitmap = CreateCompatibleBitmap(aDC, DrawWidth, DrawHeight);
SelectObject(m_hMemDC, m_hMemBitmap);
//Draw background and a trip to cover area of origin icon.
PrepareLayer0(aDC);
BitBlt(m_hMemDC,0,0,DrawWidth,DrawHeight,m_hMemDcBkg,0,0,SRCCOPY);
if(nCurHoverIndex != -1){
pBtn = m_pDoc->GetButton(nCurHoverIndex);
if(pBtn->Enabled == TRUE){
if(pBtn->State == BTN_DOWN){
SelectObject(m_hMemDC, m_BrushDown);
}else{
SelectObject(m_hMemDC, m_BrushHover);
}
hPen = CreatePen(0,1,RGB(0,0,0));
SelectObject(m_hMemDC, hPen);
::Rectangle(m_hMemDC, pBtn->cx, pBtn->cy,pBtn->cx+m_HoverRectWidth, pBtn->cy+m_HoverRectHeight );
DeleteObject(hPen);
}
}
nBtnCnt = m_pDoc->GetBtnCnt();
for(i=0;iGetButton(i);
if(pBtn->Style == BTN_SEPARATOR){
hPen = CreatePen(0,1,m_StartColor);
SelectObject(m_hMemDC,hPen);
MoveToEx(m_hMemDC,pBtn->cx+2, pBtn->cy+nSpaceY,NULL);
LineTo(m_hMemDC,pBtn->cx+2,pBtn->cy+nSpaceY+m_IconHeight);
DeleteObject(hPen);
hPen = CreatePen(0,1,RGB(0xFF,0xFF,0xFF));
SelectObject(m_hMemDC,hPen);
MoveToEx(m_hMemDC,pBtn->cx+3, pBtn->cy+nSpaceY+1,NULL);
LineTo(m_hMemDC,pBtn->cx+3,pBtn->cy+nSpaceY+1+m_IconHeight);
DeleteObject(hPen);
//Button style is icon.
}else if(pBtn->Style == BTN_ICON){
if(pBtn->Icon){
DrawIcon(m_hMemDC,pBtn->cx+nSpaceX,pBtn->cy+nSpaceY,pBtn->Icon,pBtn->Enabled);
}
}else{
}
}
//SetBkMode(m_hMemDC, TRANSPARENT);
/*
RGBTRIPLE *pBmData;
pBmData = (RGBTRIPLE*)malloc(DrawWidth*DrawHeight*sizeof(RGBTRIPLE));
BITMAPINFO info;
info.bmiHeader.biSize = sizeof(info.bmiHeader);
info.bmiHeader.biWidth = DrawWidth;
info.bmiHeader.biHeight = DrawHeight;
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biBitCount = 24;
GetDIBits(m_hMemDC,aBitMap,0,-DrawHeight,pBmData,&info,0);
*/
BitBlt(aDC, m_RectBar.left, m_RectBar.top,DrawWidth,DrawHeight,m_hMemDC,0,0,SRCCOPY);
/*
SetDIBitsToDevice(aDC,aRC.left,aRC.top,DrawWidth,DrawHeight,0,0,0,-DrawHeight,pBmData,&info,0);
free(pBmData);
*/
}catch (...) {
//不让异常抛出
}
}
//Check button index according to the mouse position.
//Return: Button index. If return -1, it means that the cursor is not on the buttons.
int CToolBarView::SearchBtnIndex(POINT point)
{
int tmpIndex;
int retIndex = -1;
int nBtnCnt;
TBItem *pBtn;
nBtnCnt = m_pDoc->GetBtnCnt();
//If the mouse position is not in the buttons area, return -1.
//Eg(16X16):
//m_BtnStartX=2 --Buttons left bound.
//m_BtnEndX=25+2=27 --Buttons right bound.
//m_BtnStartY=2 --Buttons top bound.
//m_BtnEndY=2+25=27 --Buttons bottom bound.
if((point.x < m_BtnsStartX) || (point.x > m_BtnsEndX) || (point.y < m_BtnsStartY) || (point.y > m_BtnsEndY) || (nBtnCnt <= 0)){
return -1;
}else{
//以25作为默认宽度进行搜索
tmpIndex = point.x/m_HoverRectWidth;
if(tmpIndex > nBtnCnt-1){
tmpIndex = nBtnCnt-1;
}
//检索直至鼠标在某区域:
while(TRUE){
//鼠标横坐标在当前Button左边
pBtn = m_pDoc->GetButton(tmpIndex);
if(point.x < pBtn->cx){
tmpIndex--;
//鼠标横坐标在当前Button+Width之右边
}else if(point.x > (pBtn->cx+pBtn->width)){
tmpIndex++;
}else{
retIndex = tmpIndex;
break;
}
//若搜索到尾部或者头部仍然没有搜索到,则返回不在应答区域
if((tmpIndex < 0) || (tmpIndex > nBtnCnt-1)){
retIndex = -1;
break;
}
}
}
return retIndex;
}
//Calculate coordination of toolbar and position of each button
//according to button style.
void CToolBarView::CalCoor()
{
int i;
int nBtnCnt;
int nPosX;
BarStyle barStyle;
TBItem *pBtn;
barStyle = m_pDoc->GetBarStyle();
m_BtnsStartY = 2;
switch(m_pDoc->GetBarStyle()) {
case BAR_16:
m_BtnsEndY = 26;
m_IconWidth = 16;
m_IconHeight = 16;
m_HoverRectWidth = 24;
m_HoverRectHeight = 24;
break;
case BAR_24:
m_BtnsEndY = 28;
m_IconWidth = 24;
m_IconHeight = 24;
m_HoverRectWidth = 26;
m_HoverRectHeight = 26;
break;
case BAR_32:
m_BtnsEndY = 36;
m_IconWidth = 32;
m_IconHeight = 32;
m_HoverRectWidth = 34;
m_HoverRectHeight = 34;
break;
case BAR_48:
m_BtnsEndY = 52;
m_IconWidth = 48;
m_IconHeight = 48;
m_HoverRectWidth = 50;
m_HoverRectHeight = 50;
break;
default:
m_BtnsEndY = 26;
m_IconWidth = 16;
m_IconHeight = 16;
m_HoverRectWidth = 24;
m_HoverRectHeight = 24;
break;
}
nBtnCnt = m_pDoc->GetBtnCnt();
nPosX = TOOLBAR_START_X;
//Calculate X position of each button.
for(i=0;iGetButton(i);
if(pBtn->Style == BTN_ICON){
pBtn->width = m_HoverRectWidth;
}else if(pBtn->Style == BTN_SEPARATOR){
pBtn->width = 6;
}else{
pBtn->width = m_HoverRectWidth;
}
pBtn->height = m_HoverRectHeight;
pBtn->cx = nPosX;
pBtn->cy = 2;
nPosX += pBtn->width;
}
m_BtnsStartX = TOOLBAR_START_X;
if(nBtnCnt>0){
pBtn = m_pDoc->GetButton(nBtnCnt-1);
m_BtnsEndX = pBtn->cx+pBtn->width;
}else{
m_BtnsEndX = TOOLBAR_START_X;
}
}
void CToolBarView::SetBarWidth(int newValue, BOOL bReDraw)
{
m_BarWidth = newValue;
m_RectBar.right = newValue;
if(bReDraw == TRUE){
HDC hDC = ::GetDC(m_hWnd);
ReDraw(hDC, TRUE);
ReleaseDC(m_hWnd, hDC);
}
}
void CToolBarView::SetBarHeight(int newValue, BOOL bReDraw)
{
m_BarHeight = newValue;
m_RectBar.bottom = newValue;
if(bReDraw == TRUE){
HDC hDC = ::GetDC(m_hWnd);
ReDraw(hDC, TRUE);
ReleaseDC(m_hWnd, hDC);
}
}
void CToolBarView::PrepareLayer0(HDC aDC)
{
float dR,dG,dB;
int sR,sG,sB;
int eR,eG,eB;
long i;
int DrawWidth = m_RectBar.right-m_RectBar.left;
int DrawHeight = m_RectBar.bottom-m_RectBar.top;
if(m_hMemDcBkg){
DeleteObject(m_hMemDcBkg);
}
m_hMemDcBkg = CreateCompatibleDC(aDC);
if(m_hBitmapBkg){
DeleteObject(m_hBitmapBkg);
}
m_hBitmapBkg = CreateCompatibleBitmap(aDC, DrawWidth, DrawHeight);
SelectObject(m_hMemDcBkg, m_hBitmapBkg);
if(m_hMemDcCover){
DeleteObject(m_hMemDcCover);
}
m_hMemDcCover = CreateCompatibleDC(aDC);
if(m_hBitmapCover){
DeleteObject(m_hBitmapCover);
}
m_hBitmapCover = CreateCompatibleBitmap(aDC, m_HoverRectWidth, DrawHeight);
SelectObject(m_hMemDcCover,m_hBitmapCover);
sR = (m_StartColor & 0xFF);
sG = (m_StartColor>>8) & 0xFF;
sB = (m_StartColor>>16) & 0xFF;
eR = (m_EndColor & 0xFF);
eG = (m_EndColor >>8) & 0xFF;
eB = (m_EndColor >>16) & 0xFF;
dR = (float)(sR-eR)/DrawHeight;
dG = (float)(sG-eG)/DrawHeight;
dB = (float)(sB-eB)/DrawHeight;
HPEN hPen;
for(i=0;ibmiHeader.biSize = sizeof(hBm->bmiHeader);
hBm->bmiHeader.biWidth = m_IconWidth;
hBm->bmiHeader.biHeight = m_IconHeight;
hBm->bmiHeader.biPlanes = 1;
hBm->bmiHeader.biBitCount = 24;
//Foreground DC.
HDC TmpDC = ::CreateCompatibleDC(aDC);
HBITMAP TmpBitmap = ::CreateCompatibleBitmap(aDC,m_IconWidth,m_IconHeight);
SelectObject(TmpDC, TmpBitmap);
//Background DC.
HDC BkgDC = ::CreateCompatibleDC(aDC);
HBITMAP BkgBitmap = ::CreateCompatibleBitmap(aDC,m_IconWidth,m_IconHeight);
SelectObject(BkgDC, BkgBitmap);
//Bak bkg DC
BitBlt(BkgDC,0,0,m_IconWidth,m_IconHeight, aDC, cx,cy,SRCCOPY);
::GetDIBits(BkgDC,BkgBitmap,0,m_IconHeight,pBkgData,hBm,0);
//Mask color
HBRUSH hBrush = CreateSolidBrush(MASK_COLOR);
//HPEN hPen = CreatePen(PS_NULL,0,0);
SelectObject(TmpDC,hBrush);
//SelectObject(TmpDC,hPen);
FillRect(TmpDC,&rectIcon,hBrush);
DeleteObject(hBrush);
//DeleteObject(hPen);
m_picHolder.Render(TmpDC,rectIcon,rectIcon);
::GetDIBits(TmpDC,TmpBitmap,0,m_IconHeight,pTmpData,hBm,0);
unsigned char greyColor;
for(int i=0;i