www.pudn.com > PressMonitor_q.zip > 3DBarCtrl.cpp


// 3DBarCtrl.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "3DBarCtrl.h" 
#include "3DBarCtrl.h" 
#include "MemDC.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
static COLORREF tmpColor[]= 
{ 
    RGB(0,0,0), RGB(96,0,0),RGB(125,0,0), RGB(150,0,0), 
    RGB(170,0,0),RGB(200,0,0),RGB(225,0,0),RGB(255,0,0) 
}; 
///////////////////////////////////////////////////////////////////////////// 
// C3DBarCtrl 
#define BAR_UNIT		"MPa" 
#define NCOLORSHADES	128		// this many shades in gradient 
 
 
#define PANE_LEFT		10 
#define PANE_RIGHT		8 
#define PANE_TOP		15 
#define PANE_BOTTOM		15 
 
 
C3DBarCtrl::C3DBarCtrl() 
{ 
	//fontHeight Value 
	m_nScaleHeight		= 12; 
	m_nUnitHeight		= 12; 
	m_nNameHeight		= 12; 
	m_nValueHeight		= 14; 
 
 
    m_nValueStep        = 4; 
    m_MiniValue         = 0; 
    m_MaxiValue         = 32; 
    m_strUnit			= BAR_UNIT; 
	m_bGridBelow		= false; 
     
    m_VertRatio         = 1.0; 
	m_nFrameWidth		= 4; 
 
    m_crWindowText		= GetSysColor(COLOR_WINDOWTEXT); 
    m_crWindow		    = GetSysColor(COLOR_WINDOW); 
    m_cr3DFace          = GetSysColor(COLOR_3DFACE); 
	m_crShadow          = GetSysColor(COLOR_3DSHADOW) ; 
	m_crDkShadow        = GetSysColor(COLOR_3DDKSHADOW); 
	m_crLight           = GetSysColor(COLOR_3DLIGHT); 
	m_crHighlight       = GetSysColor(COLOR_3DHIGHLIGHT) ; 
 
	m_crFrame			= GetSysColor(COLOR_3DFACE); 
	m_crBkGnd			= RGB(128,128,255); 
    m_crGrid		    = RGB(255,255,255); 
	m_crPane            = GetSysColor(COLOR_3DFACE); 
	m_crScale			= GetSysColor(COLOR_WINDOWTEXT); 
	m_crValue			= GetSysColor(COLOR_WINDOWTEXT); 
	m_crName			= GetSysColor(COLOR_WINDOWTEXT); 
	m_crRuler			= GetSysColor(COLOR_WINDOWTEXT); 
	m_crUnit			= GetSysColor(COLOR_WINDOWTEXT); 
} 
 
C3DBarCtrl::~C3DBarCtrl() 
{ 
	if ((m_pBitmapOldBackground) &&  
		  (m_bitmapBackground.GetSafeHandle()) &&  
			(m_dcBackground.GetSafeHdc())) 
	{ 
		m_dcBackground.SelectObject(m_pBitmapOldBackground); 
		m_dcBackground.DeleteDC() ; 
		m_bitmapBackground.DeleteObject(); 
	} 
 
	for(int i=0;iSetValue(value); 
    } 
} 
 
CString C3DBarCtrl::GetItemName(int Index) 
{ 
	CBarItemInfo* zone = (CBarItemInfo*)m_BarItemArray.GetAt(Index); 
 
    if(zone) 
	    return zone->GetName(); 
    else 
    { 
        ASSERT(FALSE); 
        return ""; 
    } 
} 
 
float C3DBarCtrl::GetItemValue(int Index) 
{ 
	CBarItemInfo* zone = (CBarItemInfo*)m_BarItemArray.GetAt(Index); 
 
    if(zone) 
	    return zone->GetValue(); 
    else 
    { 
        ASSERT(FALSE); 
        return 0; 
    } 
} 
 
 
COLORREF C3DBarCtrl::GetItemColor(int Index) 
{ 
	CBarItemInfo* zone = (CBarItemInfo*)m_BarItemArray.GetAt(Index); 
 
    if(zone) 
	    return zone->GetColor(); 
    else 
    { 
        ASSERT(FALSE); 
        return RGB(0,0,0); 
    } 
} 
 
BOOL C3DBarCtrl::Initialize() 
{ 
    SetScaleFromToValue(0,32,4); 
 
    /*  
     * Add  some items  
     * The Value of : 
     *      UserName 
     *      ItemValue 
     *      ItemColor 
     *      ItemIcon 
     */ 
	AddBarItem("1#",0,tmpColor[1]); 
	AddBarItem("2#",0,tmpColor[2]); 
	AddBarItem("3#",0,tmpColor[3]); 
	AddBarItem("4#",0,tmpColor[4]); 
	AddBarItem("主系统",0,tmpColor[5]); 
	AddBarItem("主缸",0,tmpColor[6]); 
	AddBarItem("回程缸",0,tmpColor[7]); 
 
    return ReCalculating(); 
} 
 
BOOL C3DBarCtrl::ReCalculating() 
{ 
	m_nItemCount = m_BarItemArray.GetSize(); 
 
    if(m_nItemCount <=1) 
    { 
        TRACE("No  Bar item exists.\n"); 
        return TRUE; 
    } 
 
    GetClientRect(&m_rectCtrl); 
	m_rectPane=m_rectCtrl; 
	m_rectPane.DeflateRect(m_nFrameWidth,m_nFrameWidth,m_nFrameWidth,m_nFrameWidth); 
 
	m_rectPaneLeft.left=m_rectPane.left; 
	m_rectPaneLeft.right=m_rectPane.left+m_rectPane.Width()*PANE_LEFT/100; 
	m_rectPaneLeft.top=m_rectPane.top+m_rectPane.Height()*PANE_TOP/100; 
	m_rectPaneLeft.bottom=m_rectPane.bottom-m_rectPane.Height()*PANE_BOTTOM/100; 
 
	m_rectPaneRight.left=m_rectPane.right-m_rectPane.Width()*PANE_RIGHT/100; 
	m_rectPaneRight.right=m_rectPane.right; 
	m_rectPaneRight.top=m_rectPane.top+m_rectPane.Height()*PANE_TOP/100; 
	m_rectPaneRight.bottom=m_rectPane.bottom-m_rectPane.Height()*PANE_BOTTOM/100; 
 
	m_rectPaneTop.left=m_rectPane.left+m_rectPane.Width()*PANE_LEFT/100; 
	m_rectPaneTop.right=m_rectPane.right-m_rectPane.Width()*PANE_RIGHT/100; 
	m_rectPaneTop.top=m_rectPane.top; 
	m_rectPaneTop.bottom=m_rectPane.top+m_rectPane.Height()*PANE_TOP/100; 
 
	m_rectPaneBottom.left=m_rectPane.left+m_rectPane.Width()*PANE_LEFT/100; 
	m_rectPaneBottom.right=m_rectPane.right-m_rectPane.Width()*PANE_RIGHT/100; 
	m_rectPaneBottom.top=m_rectPane.bottom-m_rectPane.Height()*PANE_BOTTOM/100; 
	m_rectPaneBottom.bottom=m_rectPane.bottom; 
 
	m_rectChart.left=m_rectPane.left+m_rectPane.Width()*PANE_LEFT/100; 
	m_rectChart.right=m_rectPane.right-m_rectPane.Width()*PANE_RIGHT/100; 
	m_rectChart.top=m_rectPane.top+m_rectPane.Height()*PANE_TOP/100; 
	m_rectChart.bottom=m_rectPane.bottom-m_rectPane.Height()*PANE_BOTTOM/100; 
 
    m_VertRatio = (float)m_rectChart.Height()/(float)(m_MaxiValue - m_MiniValue); 
 
	//设置字体高度 
	m_nNameHeight=(int)(m_rectPaneBottom.Height()*0.45); 
	m_nValueHeight=(int)(m_rectPaneTop.Height()*0.50); 
	m_nScaleHeight=(int)(m_rectPaneLeft.Height()*0.37/m_nValueStep); 
	m_nUnitHeight=(int)(m_rectPaneTop.Height()*0.45); 
 
 
    return TRUE; 
} 
 
BOOL C3DBarCtrl::SetScaleFromToValue(float from, float to, int ValueStep) 
{ 
    m_MiniValue = (float)from; 
    m_MaxiValue = (float)to; 
 
    if(to<=from) 
    { 
        TRACE("From Value >= To Value, Cannot create Chart!\n"); 
        return FALSE; 
    } 
 
    m_nValueStep = ValueStep ; 
 
    return TRUE; 
} 
 
void C3DBarCtrl::DrawFrame(CDC *pDC) 
{ 
	CPen penFrame,penHighlight,penShadow,*pPenOld; 
	penFrame.CreatePen(PS_SOLID, m_nFrameWidth*2,m_crFrame) ; 
	penHighlight.CreatePen(PS_SOLID, 1, m_crHighlight) ; 
	penShadow.CreatePen(PS_SOLID, 1, m_crShadow) ; 
 
    CBrush brushBack,*pBrushOld; 
	brushBack.CreateSolidBrush(m_crBkGnd) ; 
 
// 画外边框 
	pPenOld = pDC->SelectObject(&penFrame) ; 
	pBrushOld = pDC->SelectObject(&brushBack) ; 
 
	pDC->Rectangle(m_rectCtrl) ; 
/* 
	// draw the left and top sides with the highlight 
	pDC->SelectObject(&penHighlight) ; 
	pDC->MoveTo(m_rectCtrl.left, m_rectCtrl.bottom) ; 
	pDC->LineTo(m_rectCtrl.left, m_rectCtrl.top) ; 
	pDC->LineTo(m_rectCtrl.right-1, m_rectCtrl.top) ; 
 
	// draw the right and bottom sides with the shadow 
	pDC->SelectObject(&penShadow) ; 
	pDC->LineTo(m_rectCtrl.right-1, m_rectCtrl.bottom-1) ; 
	pDC->LineTo(m_rectCtrl.left, m_rectCtrl.bottom-1) ; 
*/ 
 
	// draw the left and top sides with the shadow 
	pDC->SelectObject(&penShadow) ; 
	pDC->MoveTo(m_rectPane.left, m_rectPane.bottom) ; 
	pDC->LineTo(m_rectPane.left, m_rectPane.top) ; 
	pDC->LineTo(m_rectPane.right-1, m_rectPane.top) ; 
 
	// draw the right and bottom sides with the highlight 
	pDC->SelectObject(&penHighlight) ; 
	pDC->LineTo(m_rectPane.right-1, m_rectPane.bottom-1) ; 
	pDC->LineTo(m_rectPane.left, m_rectPane.bottom-1) ; 
 
	pDC->SelectObject(pPenOld); 
	pDC->SelectObject(pBrushOld); 
	 
} 
 
void C3DBarCtrl::DrawPane(CDC *pDC) 
{ 
    CBrush brushPane,*pBrushOld; 
	brushPane.CreateSolidBrush(m_crPane) ; 
	CPen	penShadow,penLight,*pPenOld; 
	penShadow.CreatePen(PS_SOLID, 1, m_crShadow) ; 
	penLight.CreatePen(PS_SOLID, 1, m_crLight) ; 
 
	pBrushOld=pDC->SelectObject(&brushPane) ; 
 
     // Draw Pane left part 
    pDC->FillRect(CRect(m_rectPaneLeft.left+1, m_rectPaneTop.top+1,  
		m_rectPaneLeft.right+1,  m_rectPaneBottom.bottom-1), 
        &brushPane); 
	//Draw Pane right part 
    pDC->FillRect(CRect(m_rectPaneRight.left, m_rectPaneTop.top+1,  
        m_rectPaneRight.right-1,  m_rectPaneBottom.bottom-1), 
        &brushPane); 
 
    // Draw Pane top part 
    pDC->FillRect(CRect(m_rectPaneTop.left, m_rectPaneTop.top+1, 
        m_rectPaneTop.right,  m_rectPaneTop.bottom), 
        &brushPane); 
 
	// Draw Pane bottom part 
    pDC->FillRect(CRect(m_rectPaneBottom.left, m_rectPaneBottom.top, 
        m_rectPaneBottom.right,  m_rectPaneBottom.bottom-1), 
        &brushPane); 
 
	//对Chart区画3D边框 
	// draw the left and top sides with the shadow 
	pPenOld=pDC->SelectObject(&penShadow) ; 
	pDC->MoveTo(m_rectChart.left+1, m_rectChart.bottom) ; 
	pDC->LineTo(m_rectChart.left+1, m_rectChart.top) ; 
	pDC->LineTo(m_rectChart.right-1, m_rectChart.top) ; 
 
	// draw the right and bottom sides with the highlight 
	pDC->SelectObject(&penLight) ; 
	pDC->LineTo(m_rectChart.right-1, m_rectChart.bottom) ; 
	pDC->LineTo(m_rectChart.left, m_rectChart.bottom) ; 
	 
	pDC->SelectObject(pPenOld); 
	pDC->SelectObject(pBrushOld); 
} 
 
void C3DBarCtrl::DrawRuler(CDC *pDC) 
{ 
	int  yy; 
	double nHeight; 
	CPen penRuler,*pPenOld; 
	penRuler.CreatePen(PS_SOLID,1,m_crRuler); 
 
	pPenOld=pDC->SelectObject(&penRuler); 
 
	nHeight=m_rectChart.Height()*1.0/ m_nValueStep; 
	for(int i=0; i<= m_nValueStep ;i++) 
	{ 
		yy = m_rectPaneLeft.bottom -(int)(i * nHeight); 
		pDC->MoveTo(m_rectPaneLeft.right-1,yy); 
		pDC->LineTo(m_rectPaneLeft.right-8,yy); 
 
	} 
 
	for( i=0; i<= 5*m_nValueStep ;i++) 
	{ 
		yy =m_rectPaneLeft.bottom -(int)(0.2 * i * nHeight ); 
		pDC->MoveTo(m_rectPaneLeft.right-1,yy); 
		pDC->LineTo(m_rectPaneLeft.right-5,yy); 
	} 
 
	for( i=0; i<= m_nValueStep ;i++) 
	{ 
		yy = m_rectPaneLeft.bottom -(int)(i * nHeight); 
		pDC->MoveTo(m_rectPaneRight.left,yy); 
		pDC->LineTo(m_rectPaneRight.left+7,yy); 
 
	} 
 
	for( i=0; i<= 5*m_nValueStep ;i++) 
	{ 
		yy =m_rectPaneLeft.bottom -(int)(0.2* i * nHeight); 
		pDC->MoveTo(m_rectPaneRight.left,yy); 
		pDC->LineTo(m_rectPaneRight.left+4,yy); 
	} 
	 
	pDC->SelectObject(pPenOld); 
 
} 
 
void C3DBarCtrl::DrawScale(CDC *pDC) 
{ 
	int  yy; 
	double  nHeight; 
    CString pStr; 
 
	CRect rectScale; 
	CFont fontUse,*pFontOld; 
 
	fontUse.CreateFont(m_nScaleHeight,0,0,0,600, 
						FALSE, FALSE, 0, ANSI_CHARSET, 
						OUT_DEFAULT_PRECIS,  
						CLIP_DEFAULT_PRECIS, 
						DEFAULT_QUALITY,  
						DEFAULT_PITCH|FF_SWISS, "Times New Roman") ; 
	pFontOld=pDC->SelectObject(&fontUse); 
 
	int nBkModeOld=pDC->SetBkMode(TRANSPARENT); 
	pDC->SetTextColor(m_crScale); 
 
	nHeight=m_rectChart.Height()*1.0/ m_nValueStep; 
	for(int i=0; i<= m_nValueStep ;i++) 
	{ 
        pStr.Format("%.1f", m_MiniValue + (m_MaxiValue  - m_MiniValue) *  i/ m_nValueStep); 
		yy = m_rectPaneLeft.bottom -(int)(i*nHeight); 
			 
        rectScale.SetRect(m_rectPaneLeft.left,yy-(int)(nHeight/2),m_rectPaneLeft.right-10,yy+(int)(nHeight/2)); 
		pDC->DrawText(pStr,&rectScale,DT_RIGHT|DT_VCENTER|DT_SINGLELINE); 
 
	} 
	pDC->SelectObject(pFontOld); 
	pDC->SetBkMode(nBkModeOld); 
} 
 
void C3DBarCtrl::DrawGrid(CDC *pDC) 
{ 
	int  yy; 
	double nHeight; 
	CPen penGrid,*pPenOld; 
	penGrid.CreatePen(PS_DOT,1,m_crGrid); 
 
	pPenOld=pDC->SelectObject(&penGrid); 
	int nBkMode=pDC->SetBkMode(TRANSPARENT); 
 
	nHeight=m_rectChart.Height()*1.0/ m_nValueStep; 
	for(int i=1; iMoveTo(m_rectChart.left,yy); 
		pDC->LineTo(m_rectChart.right,yy); 
 
	} 
	pDC->SelectObject(pPenOld); 
	pDC->SetBkMode(nBkMode); 
} 
 
void C3DBarCtrl::DrawName(CDC *pDC) 
{ 
	int  xx; 
	double  nWidth; 
    CString pStr; 
 
	CRect rectName; 
	CFont fontUse,*pFontOld; 
 
	fontUse.CreateFont(m_nNameHeight,0,0,0,100, 
						FALSE, FALSE, 0, ANSI_CHARSET, 
						OUT_DEFAULT_PRECIS,  
						CLIP_DEFAULT_PRECIS, 
						DEFAULT_QUALITY,  
						DEFAULT_PITCH|FF_SWISS, "Arial") ; 
	pFontOld=pDC->SelectObject(&fontUse); 
 
	int nBkModeOld=pDC->SetBkMode(TRANSPARENT); 
	pDC->SetTextColor(m_crName); 
 
	nWidth=m_rectChart.Width()*1.0/m_nItemCount; 
	for(int i=0; i< m_nItemCount ;i++) 
	{ 
        pStr=GetItemName(i); 
		pDC->SetTextColor(GetItemColor(i)); 
		xx = m_rectChart.left+(int)(i*nWidth); 
			 
        rectName.SetRect(xx,m_rectPaneBottom.top,xx+(int)nWidth,m_rectPaneBottom.bottom); 
		pDC->DrawText(pStr,&rectName,DT_CENTER|DT_VCENTER|DT_SINGLELINE); 
 
	} 
	pDC->SelectObject(pFontOld); 
	pDC->SetBkMode(nBkModeOld); 
} 
 
void C3DBarCtrl::DrawValue(CDC *pDC) 
{ 
	int  xx; 
	double  nWidth; 
    CString pStr; 
 
	CRect rectValue; 
	CFont fontUse,*pFontOld; 
 
	fontUse.CreateFont(m_nValueHeight,0,0,0,300, 
						FALSE, FALSE, 0, ANSI_CHARSET, 
						OUT_DEFAULT_PRECIS,  
						CLIP_DEFAULT_PRECIS, 
						DEFAULT_QUALITY,  
						DEFAULT_PITCH|FF_SWISS, "Arial") ; 
	pFontOld=pDC->SelectObject(&fontUse); 
 
	int nBkModeOld=pDC->SetBkMode(TRANSPARENT); 
	pDC->SetTextColor(m_crValue); 
 
	nWidth=m_rectChart.Width()*1.0/m_nItemCount; 
	for(int i=0; i< m_nItemCount ;i++) 
	{ 
        pStr.Format("%.1f",GetItemValue(i)); 
		pDC->SetTextColor(GetItemColor(i)); 
		xx = m_rectChart.left+(int)(i*nWidth); 
			 
        rectValue.SetRect(xx,m_rectPaneTop.top,xx+(int)nWidth,m_rectPaneTop.bottom); 
		pDC->DrawText(pStr,&rectValue,DT_CENTER|DT_VCENTER|DT_SINGLELINE); 
 
	} 
	pDC->SelectObject(pFontOld); 
	pDC->SetBkMode(nBkModeOld); 
} 
 
void C3DBarCtrl::DrawUnit(CDC *pDC) 
{ 
	CRect rectUnit; 
	CFont fontUse,*pFontOld; 
 
	fontUse.CreateFont(m_nUnitHeight,0,0,0,900, 
						FALSE, FALSE, 0, ANSI_CHARSET, 
						OUT_DEFAULT_PRECIS,  
						CLIP_DEFAULT_PRECIS, 
						DEFAULT_QUALITY,  
						DEFAULT_PITCH|FF_SWISS, "Arial") ; 
	pFontOld=pDC->SelectObject(&fontUse); 
 
	int nBkModeOld=pDC->SetBkMode(TRANSPARENT); 
	pDC->SetTextColor(m_crUnit); 
 
	//top unit		 
    rectUnit.SetRect(m_rectPaneLeft.left,m_rectPane.top,m_rectPaneLeft.right-10,m_rectPaneLeft.top-(int)(m_nScaleHeight)); 
	pDC->DrawText(m_strUnit,&rectUnit,DT_RIGHT|DT_BOTTOM|DT_SINGLELINE); 
/*	//bottom unit 
    rectUnit.SetRect(m_rectPaneLeft.left,m_rectPaneLeft.bottom+(int)(m_nScaleHeight),m_rectPaneLeft.right-10,m_rectPane.bottom); 
	pDC->DrawText(m_strUnit,&rectUnit,DT_RIGHT|DT_TOP|DT_SINGLELINE); 
*/ 
	pDC->SelectObject(pFontOld); 
	pDC->SetBkMode(nBkModeOld); 
} 
 
void C3DBarCtrl::DrawBar(CDC *pDC) 
{ 
	int		xx,yy; 
	double	nWidth;  
    float	value;			 
	CRect	rectBar; 
	 
	nWidth = m_rectChart.Width()*1.0/m_nItemCount; 
 
	for(int i=0;im_rectChart.bottom) 
			yy=m_rectChart.bottom; 
		else if(yy= xDelta)  
	{ 
		x -= xDelta; 
		if (r1 > r2) 
			r = r1 - (r1-r2)*(w-x)/w; 
		else 
			r = r1 + (r2-r1)*(w-x)/w; 
 
		if (g1 > g2) 
			g = g1 - (g1-g2)*(w-x)/w; 
		else 
			g = g1 + (g2-g1)*(w-x)/w; 
 
		if (b1 > b2) 
			b = b1 - (b1-b2)*(w-x)/w; 
		else 
			b = b1 + (b2-b1)*(w-x)/w; 
 
        if(ascend) // Paint from  left to right; 
		     PaintRect(pDC, rect.left+x, rect.top, xDelta, cyCap, RGB(r, g, b)); 
        else               // Paint from  right to left; 
             PaintRect(pDC, rect.right-x-xDelta, rect.top, xDelta, cyCap, RGB(r, g, b)); 
	} 
} 
 
void C3DBarCtrl::PaintRect(CDC *pDC, int x, int y, int w, int h, COLORREF color) 
{ 
	CBrush brush(color); 
	CBrush* pOldBrush = pDC->SelectObject(&brush); 
	pDC->PatBlt(x, y, w, h, PATCOPY); 
	pDC->SelectObject(pOldBrush); 
}