www.pudn.com > wendu.rar > Test.cpp


// Test.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "wendu.h" 
#include "Test.h" 
#include "MemDC.h" 
#include "math.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CTest 
 
CTest::CTest() 
{ 
        Scale=10;                    //刻度数 
		Subscale=5;                  //子刻度数 
		CurveMax=160;                //最大刻度读数 
		CurveMin=-20;                //最小刻度读数 
        TempValue=0;                 //当前温度值 
        clrText=RGB(0, 255, 0);   //字体颜色 
        clrBar=RGB(0, 0, 255);       //填充区域颜色 
		 
	   //设置控件上显示的文字的字体和大小 
	   m_SmallFont.CreateFont(-12, 0, 0, 0, FW_THIN, false, false, false, DEFAULT_CHARSET, 
		                       OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, 
		                       VARIABLE_PITCH, _TEXT("Times New Roman")); 
	// 关键代码 
	// 初始化关键代码的 C_S 结构 
	InitializeCriticalSection(&g_csData); 
} 
 
CTest::~CTest() 
{ 
} 
 
 
BEGIN_MESSAGE_MAP(CTest, CStatic) 
	//{{AFX_MSG_MAP(CTest) 
	ON_WM_PAINT() 
 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CTest message handlers 
 
void CTest::OnPaint()  
{ 
	CPaintDC dc(this); // device context for painting 
	 
	// TODO: Add your message handler code here 
        //获得控件区域 
	    GetClientRect(&temprect); 
	    // 创建内存 DC 
        CMemDC *pMemDC=new CMemDC(&dc,&temprect); 
 
		CPoint orgBrushOrigin=pMemDC->GetBrushOrg(); 
 
		if(m_dcBackground.GetSafeHdc()==NULL||m_pBitmapBackground.m_hObject==NULL) 
		{ 
          m_dcBackground.CreateCompatibleDC(&dc); 
		  m_pBitmapBackground.CreateCompatibleBitmap(&dc,temprect.Width(),temprect.Height()); 
		  m_pBitmapOldBackground=m_dcBackground.SelectObject(&m_pBitmapBackground); 
		  //绘制背景 
          DrawBackground(&m_dcBackground); 
		} 
		  pMemDC->BitBlt(0,0,temprect.Width(),temprect.Height(),&m_dcBackground,0,0,SRCCOPY); 
 
 
          //画温度计框架 
          DrawFrame(pMemDC); 
		  //填充温度计内部 
		  FillFrame(pMemDC); 
		  //显示标题 
		  ShowTitle(pMemDC); 
		  //显示温度 
		  ShowValue(pMemDC); 
 
		pMemDC->SetBrushOrg(orgBrushOrigin.x,orgBrushOrigin.y); 
		delete pMemDC; 
	// Do not call CStatic::OnPaint() for painting messages 
} 
void CTest::DrawBackground(CDC *pDC) 
{ 
	    //填充背景 
        pDC->FillSolidRect(&temprect,RGB(0,0,0)); 
 
} 
void CTest::DrawFrame(CDC *pDC) 
{ 
	    //用关键代码同步 
	    EnterCriticalSection(&g_csData); 
        //定义画笔 
		CPen pen(PS_SOLID,1,RGB(0,255,0)); 
		pDC->SelectObject(&pen); 
		//定义字体 
        CFont * pOldFont ; 
	    pOldFont=pDC->SelectObject(&m_SmallFont); 
 	    //给各坐标点赋值 
		Point1_X=temprect.Width()*3/7; 
        Point1_Y=temprect.Height()/10; 
        Point2_X=temprect.Width()*4/7; 
        Point2_Y=Point1_Y; 
        RadiusStartPoint_X=Point1_X; 
        RadiusStartPoint_Y=temprect.Height()*8/10; 
        RadiusEndPoint_X=Point2_X; 
        RadiusEndPoint_Y=RadiusStartPoint_Y; 
		R=temprect.Width()/7; 
        RadiusCenterPoint_X=temprect.Width()/2; 
        RadiusCenterPoint_Y=RadiusStartPoint_Y+R*sqrt(3.0)/2; 
        CurveStartPoint_X=Point2_X; 
        CurveStartPoint_Y=RadiusEndPoint_Y-temprect.Width()/14; 
        CurveEndPoint_X=CurveStartPoint_X; 
        CurveEndPoint_Y=temprect.Height()/9; 
        CurveLength=CurveStartPoint_Y-CurveEndPoint_Y; 
        ValueBound=CurveMax-CurveMin; 
        //定义存放刻度值的数组 
        TCHAR ScaleValue[MAX_PATH*sizeof(TCHAR)+1]; 
	    ZeroMemory(&ScaleValue, sizeof(ScaleValue)); 
 
 
 
 
 
        //设置背景为透明模式和字体颜色 
  	    pDC->SetBkMode(TRANSPARENT);	 
		pDC->SetTextColor(clrText); 
 
        //绘制温度计图形		 
		pDC->MoveTo(Point1_X,Point1_Y); 
		pDC->LineTo(RadiusStartPoint_X,RadiusStartPoint_Y); 
		pDC->MoveTo(Point2_X,Point2_Y); 
		pDC->LineTo(RadiusEndPoint_X,RadiusEndPoint_Y); 
		pDC->MoveTo(Point1_X,Point1_Y); 
		pDC->LineTo(Point2_X,Point2_Y); 
	    pDC->MoveTo(RadiusStartPoint_X,RadiusStartPoint_Y); 
		pDC->AngleArc(RadiusCenterPoint_X,RadiusCenterPoint_Y,R,120,300); 
        //绘制刻度 
		for(int i=0; i<=(Scale-1)*Subscale ; i++) 
		{ 
			if(i%Subscale==0) 
			{ 
				pDC->MoveTo(CurveStartPoint_X,CurveStartPoint_Y-i*CurveLength/((Scale-1)*Subscale)); 
				pDC->LineTo(CurveStartPoint_X+temprect.Width()/20,CurveStartPoint_Y-i*CurveLength/((Scale-1)*Subscale)); 
		        //显示刻度值 
	            _stprintf(ScaleValue, _TEXT ("%3.0f"), CurveMin+ValueBound*i/((Scale-1)*Subscale)); 
	            pDC->TextOut(CurveStartPoint_X+temprect.Width()/15,CurveStartPoint_Y-(i+1)*CurveLength/((Scale-1)*Subscale), ScaleValue); 
                 
			} 
			if(i%Subscale!=0) 
			{ 
				pDC->MoveTo(CurveStartPoint_X,CurveStartPoint_Y-i*CurveLength/((Scale-1)*Subscale)); 
				pDC->LineTo(CurveStartPoint_X+temprect.Width()/30,CurveStartPoint_Y-i*CurveLength/((Scale-1)*Subscale)); 
 
			} 
		} 
 
	    // 离开关键代码 
	    LeaveCriticalSection(&g_csData); 
} 
 
void CTest::FillFrame(CDC *pDC) 
{ 
	    // 用关键代码同步 
	    EnterCriticalSection(&g_csData); 
        //填充图形 
		CPen pen(PS_SOLID,1,clrBar); 
		pDC->SelectObject(&pen); 
        //定义画刷 
  	    CBrush BarBush; 
	    CBrush *pBarBush; 
	    BarBush.CreateSolidBrush(clrBar);//实体画刷 
	    pBarBush=pDC->SelectObject(&BarBush); 
        //确定填充区域 
		FillHeight=CurveStartPoint_Y-(TempValue-CurveMin)*CurveLength/ValueBound;   
	    pDC->Rectangle(Point1_X+1,FillHeight,RadiusEndPoint_X-1,RadiusEndPoint_Y); 
	    pDC->Ellipse(RadiusCenterPoint_X-R+1,RadiusCenterPoint_Y-R+1,RadiusCenterPoint_X+R-1,RadiusCenterPoint_Y+R-1); 
 
	    BarBush.DeleteObject(); 
        pDC->SelectObject(pBarBush); 
 
		// 离开关键代码 
	    LeaveCriticalSection(&g_csData); 
} 
 
void CTest::GetData(double TValue) 
{ 
	    // 用关键代码同步 
	    EnterCriticalSection(&g_csData); 
 
        TempValue=TValue; 
        //使原值无效,重新赋值 
	    Invalidate(); 
 
		// 离开关键代码 
	    LeaveCriticalSection(&g_csData); 
} 
 
void CTest::ShowTitle(CDC *pDC) 
{      
	    //定义画笔 
		CPen pen(PS_SOLID,1,RGB(0,255,0)); 
		pDC->SelectObject(&pen);	 
		//绘制标题 
	    TCHAR Titles[MAX_PATH*sizeof(TCHAR)+1]; // 控件标题 
	    ZeroMemory(&Titles, sizeof(Titles)); 
        _stprintf(Titles, _TEXT ("%s"), "温度(°C):"); 
	    pDC->TextOut(temprect.Width()*2/7,temprect.top, Titles); 
 
} 
 
void CTest::ShowValue(CDC *pDC) 
{ 
	    //定义画笔 
		CPen pen(PS_SOLID,1,RGB(0,255,0)); 
		pDC->SelectObject(&pen);	 
		//显示温度值 
	    TCHAR value[MAX_PATH*sizeof(TCHAR)+1];  
	    ZeroMemory(&value, sizeof(value)); 
        _stprintf(value, _TEXT ("%3.2f"), TempValue); 
	    pDC->TextOut(temprect.Width()*4/7,temprect.top, value); 
 
}