// ECGSTWave.cpp : 实现文件
//

#include "stdafx.h"
#include "../Verbpo.h"
#include "ECGSTWave.h"


// CECGSTWave

IMPLEMENT_DYNAMIC(CECGSTWave, CStatic)

CECGSTWave::CECGSTWave(int Width, int Height, int LeadType)
{
	int i = 0;
	for (i = 0; i < 2; i++)
	{
		m_i16WaveDate[i] = new INT16[ST_PATTERN_LENGTH];
		m_pCPonit[i] = new CPoint[ST_PATTERN_LENGTH];
	}
	m_i16WaveDateLength = 0;
	penECGLine = new CPen(PS_SOLID, 1, RGB(0, 255, 0));
	penISO = new CPen(PS_SOLID, 1, RGB(255 ,128 ,0));
	penST = new CPen(PS_SOLID, 1, RGB(61 ,89 ,171));

	m_i32ISO = 64;
	m_i32ST = 80;
	m_i32Height = Height;
	m_i32Width = Width;

	m_i32Y1 = (Height - 4) / 4;
	m_i32Y2 = (Height - 4) *3/4+4 ;

	m_i32YLimit1 = (Height - 4) / 2;
	m_i32YLimit2 = Height;
	UnitLength_Pixel = (Height - 4) / 10;
}

CECGSTWave::~CECGSTWave()
{
	int i = 0;
	for (i = 0; i < 2; i++)
	{
		delete m_i16WaveDate[i] ;
		delete m_pCPonit[i];
	}
	delete penECGLine;
	delete penISO;
	delete penST;
	dcMem.DeleteDC();
	bmp.DeleteObject();
	font.DeleteObject();
}


BEGIN_MESSAGE_MAP(CECGSTWave, CStatic)
	ON_WM_PAINT()
	ON_WM_CREATE()
END_MESSAGE_MAP()



// CECGSTWave 消息处理程序

int CECGSTWave::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	CString str;
	// 获得控件区域
	GetClientRect(&m_rectCtrl);
	//客户区DC，可以绘画及显示
	CClientDC pDC(this);
	//依附窗口DC创建兼容内存DC
	dcMem.CreateCompatibleDC(&pDC);
	//创建兼容位图
	bmp.CreateCompatibleBitmap(&pDC, m_rectCtrl.Width(), m_rectCtrl.Height());
	dcMem.SelectObject(&bmp); //将位图选择进内存DC
	dcMem.FillSolidRect(0, 0, m_i32Width, m_i32Height, RGB(0, 0, 0));

	font.CreateFont(20,                                          //   nHeight     
		0,                                                   //   nWidth     
		0,                                                   //   nEscapement   
		0,                                                   //   nOrientation     
		FW_NORMAL,                                   //   nWeight     
		FALSE,                                           //   bItalic     
		FALSE,                                           //   bUnderline     
		0,                                                   //   cStrikeOut     
		ANSI_CHARSET,                             //   nCharSet     
		OUT_DEFAULT_PRECIS,                 //   nOutPrecision     
		CLIP_DEFAULT_PRECIS,               //   nClipPrecision     
		DEFAULT_QUALITY,                       //   nQuality     
		DEFAULT_PITCH | FF_SWISS,     //   nPitchAndFamily       
		_T("宋体"));
	dcMem.SelectObject(&font);
	dcMem.SetTextColor(RGB(0, 255, 0));
	str = "I";
	dcMem.DrawText(str, CRect(0, m_i32Y1 - 10, 30, m_i32Y1 + 10), 0);

	str = "II";
	dcMem.DrawText(str, CRect(0, m_i32Y2 - 10, 30, m_i32Y2 + 10), 0);
	//	return CStatic::OnCreate(lpCreateStruct);
	return TRUE;
}

void CECGSTWave::OnPaint()
{
	CPaintDC pDC(this);

	pDC.BitBlt(m_rectCtrl.left, m_rectCtrl.top, m_rectCtrl.Width(), m_rectCtrl.Height(), &dcMem, 0, 0, SRCCOPY);
	//	CStatic::OnPaint();
}

void CECGSTWave::Set_UnitLength_Value(int Value)
{
	UnitLength_Value = Value;

}
void CECGSTWave::draw_wave()
{
	CPen *pOldPen;
	int a_i32WaveDateLength = 0, a_i32WaveDateR, i = 0, y = 0, a_i32Temp = 0, j = 0, t = 0;
	int a_i32X=0, a_p32LastX = 0, a_i32Y, a_p32LastY = 0;
	register int a_i32Height1_2=0, a_UnitLength_Pixel = UnitLength_Pixel, a_UnitLength_Value = UnitLength_Value;
	register int a_i32PointNum = 0;
	
	
	m_CSWaveData.Lock();
	if (m_i16WaveDateLength)
	{
		a_i32Height1_2 = m_i32Y1;
		m_pCPonit[0][a_i32PointNum].x = a_i32X;
		m_pCPonit[0][a_i32PointNum].y = m_i32Y1;
		a_i32PointNum++;
		for (i = 0; i < ST_PATTERN_LENGTH; i++)
		{
			a_i32Y = a_i32Height1_2 - ((m_i16WaveDate[0][i] - 2048)*a_UnitLength_Pixel / a_UnitLength_Value) / 2;
			if (a_i32Y >= m_i32YLimit1)
				a_i32Y = m_i32YLimit1 - 1;
			else if (a_i32Y < 0)
				a_i32Y = 0;

			a_i32X++;

			m_pCPonit[0][a_i32PointNum].x = a_i32X;
			m_pCPonit[0][a_i32PointNum].y = a_i32Y;
			a_i32PointNum++;
		}

		a_i32PointNum = 0;
		a_i32Height1_2 = m_i32Y2;
		m_pCPonit[0][a_i32PointNum].x = a_i32X;
		m_pCPonit[0][a_i32PointNum].y = m_i32Y2;
		a_i32PointNum++;
		for (i = 0; i < ST_PATTERN_LENGTH; i++)
		{
			a_i32Y = a_i32Height1_2 - ((m_i16WaveDate[1][i] - 2048)*a_UnitLength_Pixel / a_UnitLength_Value) / 2;
			if (a_i32Y >= m_i32YLimit2)
				a_i32Y = m_i32YLimit2 - 1;
			else if (a_i32Y < m_i32YLimit1 + 4)
				a_i32Y = m_i32YLimit1 + 4;

			a_i32X++;
			a_i32Y += m_i32Y2;
			m_pCPonit[1][a_i32PointNum].x = a_i32X;
			m_pCPonit[1][a_i32PointNum].y = a_i32Y;
			a_i32PointNum++;
		}
	}
	
	m_CSWaveData.Unlock();

	dcMem.FillSolidRect(40, 0, m_i32Width-40, m_i32Height, RGB(0, 0, 0));
	pOldPen = dcMem.SelectObject(penECGLine);
	dcMem.Polyline(m_pCPonit[0], a_i32PointNum);
	dcMem.Polyline(m_pCPonit[1], a_i32PointNum);
	pOldPen = dcMem.SelectObject(penISO);
	a_i32Temp = m_i32ISO/4;
	if (a_i32Temp > 124)
		a_i32Temp = 124;
	dcMem.MoveTo(165 - a_i32Temp, 0);
	dcMem.LineTo(165 - a_i32Temp, m_i32Height);

	pOldPen = dcMem.SelectObject(penST);
	a_i32Temp = m_i32ST / 4;
	if (a_i32Temp > 124)
		a_i32Temp = 124;
	dcMem.MoveTo(165 + a_i32Temp, 0);
	dcMem.LineTo(165 + a_i32Temp, m_i32Height);
	dcMem.SelectObject(penISO);
	
}

void CECGSTWave::set_st_iso(int st, int iso)
{
	m_i32ISO = iso, m_i32ST=st;

}