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


// 3DtempCtrl.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "monitor.h" 
#include "3DtempCtrl.h" 
#include  
 
#include "MemDC.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// C3DtempCtrl 
 
C3DtempCtrl::C3DtempCtrl() 
{ 
//	m_Value		= 1300.0; 
	m_bNewAngle		= true; 
	m_ForegroudPal	= true; 
 
	m_uIDBitmap			= 0; 
	m_TipColor			= RGB(255,255,0);	// Color of the small tip  
	m_DirectionColor	= RGB(127,127,255);	// Color of the part that holds the tip 
	m_TailColor			= RGB(127,127,127);	// Color of the tail part of the needle 
	m_ShadowColor		= RGB(0,0,127);		// Color of the shadow 
} 
 
C3DtempCtrl::~C3DtempCtrl() 
{ 
} 
 
 
void C3DtempCtrl::SetValue(double Value, bool bRedraw /*=true*/) 
{ 
	m_Value = Value; 
	m_bNewAngle = true; 
 
	Invalidate(); 
} 
void C3DtempCtrl::SetRange(double maxValue,double minValue) 
{ 
	m_maxValue=maxValue; 
	m_minValue=minValue; 
} 
 
void C3DtempCtrl::SizeToBitmap() 
{ 
/*	DOES NOT WORK!? Shifts to about the bottom-right corner 
	m_WorkBitmap.Load(m_uIDBitmap); 
	CSize bmSize = m_WorkBitmap.GetBitmapDimension(); 
 
	CRect Rect; 
	GetWindowRect(&Rect); 
	Rect.SetRect(Rect.TopLeft(), Rect.TopLeft()+bmSize); 
	MapDialogRect(m_hWnd, &Rect); 
	MoveWindow(&Rect); 
*/ 
} 
 
BEGIN_MESSAGE_MAP(C3DtempCtrl, CWnd) 
	//{{AFX_MSG_MAP(C3DtempCtrl) 
	ON_WM_PAINT() 
	ON_WM_ERASEBKGND() 
	ON_WM_QUERYNEWPALETTE() 
	ON_WM_PALETTECHANGED() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
 
///////////////////////////////////////////////////////////////////////////// 
// C3DtempCtrl message handlers 
void C3DtempCtrl::OnPaint()  
{ 
	CPaintDC dc(this); // device context for painting 
 
	// Create the compass drawing 
	CRect tempRect; 
 
	GetClientRect(&tempRect); 
 
	int rectWidth=tempRect.Width(); 
	int rectHigh=tempRect.Height(); 
	 
/*  2000下可以稳定运行的程序部分! 98下则应替换为下面的程序! 
	CMemDC memDC(&dc,&tempRect); 
 
	CBitmap Bitmap,workBitmap; 
	Bitmap.LoadBitmap(m_uIDBitmap); 
 
	BITMAP bm; 
	// Get dimension 
	Bitmap.GetBitmap(&bm); 
	// Width of the bitmap 
	int bitWidth=bm.bmWidth; 
	int bitHigh=bm.bmHeight; 
	int CenterX =bitWidth/2;// workBitmap.GetBitmapDimension().cx/2; 
	int CenterY =bitHigh/2;// workBitmap.GetBitmapDimension().cy/2; 
 
 
	workBitmap.LoadBitmap(m_uIDBitmap); 
	memDC.SelectObject(&workBitmap);   ////存在问题! 
	 
	 
	 
	CBrush ValueBrush; 
	ValueBrush.CreateSolidBrush(RGB(255,0,0)); 
	 
	///////////////////////////////////// 
	int aftValue; 
	if (m_Value<=m_minValue) { 
		m_Value=m_minValue; 
		//	aftValue=0; 
	} 
	if (m_minValue>m_maxValue) { 
		AfxMessageBox("错误:温度表的下限大于上限"); 
		return; 
	} 
	aftValue=(int)((m_Value-m_minValue)/(m_maxValue-m_minValue)*90); 
	CPoint ValuePoint[4] ={ 
		CPoint(-2,48),CPoint(-2,44-aftValue),CPoint(2,44-aftValue),CPoint(2,48) 
			//	CPoint(-2,48),CPoint(-2,44-90),CPoint(2,44-90),CPoint(2,48) 
	}; 
	for (int i = 0;i <4;i++) 
	{ 
		ValuePoint[i].Offset(CenterX, CenterY); 
	} 
	 
	{ 
		CRgn ValueRgn; 
		ValueRgn.CreatePolygonRgn(ValuePoint, 4, ALTERNATE); 
		memDC.FillRgn(&ValueRgn, &ValueBrush); 
	} 
 
	//////////////////////////////////////////////// 
	CFont fontUse,*pFontOld ; 
	fontUse.CreateFont (16, 0, 0, 0, 300, 
		FALSE, FALSE, 0, ANSI_CHARSET, 
		OUT_DEFAULT_PRECIS,  
		CLIP_DEFAULT_PRECIS, 
		DEFAULT_QUALITY,  
		DEFAULT_PITCH|FF_SWISS, "Times New Roman") ; 
	 
	pFontOld = memDC.SelectObject(&fontUse) ; 
	int nMode=memDC.SetBkMode(TRANSPARENT); 
	// set the crs based on the system crs 
	memDC.SetTextColor(RGB(0,250,0)) ; 
	 
	// draw the text in the recessed rectangle 
	CString strTemp; 
	strTemp.Format("%.*f", 1, m_Value) ; 
	memDC.TextOut(5,10,strTemp); 
	 
	// restore the color and the font 
	memDC.SelectObject(pFontOld) ; 
	memDC.SetBkMode(nMode); 
	fontUse.DeleteObject(); 
	/////////////////////////////////////////////// 
*/ 
//	dc.BitBlt(tempRect.left,tempRect.top,bitWidth,bitHigh,&memDC,tempRect.Width(),tempRect.Height(),SRCCOPY); 
//  在98下可以稳定运行! 
	if (m_bNewAngle) 
	{ 
		m_bNewAngle = false; 
		// Create the compass drawing 
		m_WorkBitmap.Load(m_uIDBitmap); 
 
		int CenterX = m_WorkBitmap.GetWidth()/2; 
		int CenterY = m_WorkBitmap.GetHeight()/2; 
 
		 
		///////////////////////////////////// 
		int aftValue; 
		if (m_Value<=m_minValue) { 
			m_Value=m_minValue; 
		//	aftValue=0; 
		} 
		if (m_minValue>m_maxValue) { 
			AfxMessageBox("错误:温度表的下限大于上限"); 
			return; 
		} 
		aftValue=(int)((m_Value-m_minValue)/(m_maxValue-m_minValue)*90); 
 
		CPoint ValuePoint[4] ={ 
			CPoint(-2,48),CPoint(-2,44-aftValue),CPoint(2,44-aftValue),CPoint(2,48) 
		//	CPoint(-2,48),CPoint(-2,44-90),CPoint(2,44-90),CPoint(2,48) 
		}; 
 
		CDC dcWork; 
		dcWork.CreateCompatibleDC(&dc); 
		dcWork.SelectObject(&m_WorkBitmap); 
 
		CBrush ValueBrush; 
		ValueBrush.CreateSolidBrush(RGB(255,0,0)); 
 
		for (int i = 0;i <4;i++) 
		{ 
			ValuePoint[i].Offset(CenterX, CenterY); 
		} 
 
		{ 
			CRgn ValueRgn; 
			ValueRgn.CreatePolygonRgn(ValuePoint, 4, ALTERNATE); 
			dcWork.FillRgn(&ValueRgn, &ValueBrush); 
		} 
 
	} 
	// We only have to blit the background bitmap 
 
	CSize bmSize = m_WorkBitmap.GetBitmapDimension(); 
	CRect bmRect(CPoint(0,0), bmSize); 
	 
	m_WorkBitmap.Blit(&dc, bmRect); 
	m_WorkBitmap.RealizePalette(&dc, !m_ForegroudPal); 
 
//////////////////////////////////////////////// 
	CFont fontUse,*pFontOld ; 
	fontUse.CreateFont (16, 0, 0, 0, 300, 
		FALSE, FALSE, 0, ANSI_CHARSET, 
		OUT_DEFAULT_PRECIS,  
		CLIP_DEFAULT_PRECIS, 
		DEFAULT_QUALITY,  
		DEFAULT_PITCH|FF_SWISS, "Times New Roman") ; 
	 
	pFontOld = dc.SelectObject(&fontUse) ; 
	int nMode=dc.SetBkMode(TRANSPARENT); 
	// set the crs based on the system crs 
	dc.SetTextColor(RGB(0,250,0)) ; 
	 
	// draw the text in the recessed rectangle 
	CString strTemp; 
	strTemp.Format("%.*f", 1, m_Value) ; 
	dc.TextOut(5,10,strTemp); 
	 
	// restore the color and the font 
	dc.SelectObject(pFontOld) ; 
	dc.SetBkMode(nMode); 
	fontUse.DeleteObject(); 
/////////////////////////////////////////////// 
 
} 
 
// We only have to blit the background bitmap 
 
BOOL C3DtempCtrl::OnEraseBkgnd(CDC*)  
{ 
	return TRUE;		// We erase the background ourselves 
} 
 
BOOL C3DtempCtrl::OnQueryNewPalette() 
{ 
	// Force the next Redraw to set the palette to foreground 
	m_ForegroudPal = true; 
	RedrawWindow(); 
 
	return TRUE; 
} 
 
void C3DtempCtrl::OnPaletteChanged(CWnd* pFocusWnd)  
{ 
	//CWnd::OnPaletteChanged(pFocusWnd); 
	if (pFocusWnd != this) 
	{ 
		CPaintDC dc(this); 
		dc.UpdateColors(); 
	} 
	// TODO: Add your message handler code here	 
}