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

#include "stdafx.h"
#include "Verbpo.h"
#include "StaticValueECGNormal.h"
#include "includes.h"


INT CStaticValueECGNormal::m_i32NormalWidth = 340, CStaticValueECGNormal::m_i32NormalHeight = 160;
INT CStaticValueECGNormal::m_i32BigWidth = 412, CStaticValueECGNormal::m_i32BigHeight = 195;

INT CStaticValueECGNormal::m_i32NormalX=940, CStaticValueECGNormal::m_i32NormalY=60;
INT CStaticValueECGNormal::m_i32BigX=868, CStaticValueECGNormal::m_i32BigY=60;

COLORREF CStaticValueECGNormal::m_rgbBlink = RGB(255, 140, 64);
COLORREF CStaticValueECGNormal::m_rgbText = RGB(0, 0xE1, 0);


// CStaticValueECGNormal

IMPLEMENT_DYNAMIC(CStaticValueECGNormal, CStatic)

CStaticValueECGNormal::CStaticValueECGNormal()
{
}

CStaticValueECGNormal::~CStaticValueECGNormal()
{
	font.DeleteObject();
	fontHR.DeleteObject();
	fontHRBig.DeleteObject();
	fontHRAlm.DeleteObject();

	dcMem.DeleteDC();
	bmp.DeleteObject();
	bitmaBeat.DeleteObject();
	dcMemBeat.DeleteDC();

	bitmapAlm.DeleteObject();
	dcMemAlm.DeleteDC();
}


BEGIN_MESSAGE_MAP(CStaticValueECGNormal, CStatic)
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_CREATE()
	ON_WM_LBUTTONDBLCLK()
END_MESSAGE_MAP()

int CStaticValueECGNormal::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	//	this->MoveWindow(0, 0, m_i32Width, m_i32Height);
	m_i32Width = CStaticValueECGNormal::m_i32NormalWidth;
	m_i32Height = CStaticValueECGNormal::m_i32NormalHeight;
	m_i32Style = 0;

	// 获得控件区域
	GetClientRect(&m_rectCtrl);
	//客户区DC，可以绘画及显示
	CClientDC pDC(this);
	
	
	//依附窗口DC创建兼容内存DC
	dcMem.CreateCompatibleDC(&pDC);
	//创建兼容位图
	bmp.CreateCompatibleBitmap(&pDC, CStaticValueECGNormal::m_i32BigWidth, CStaticValueECGNormal::m_i32BigHeight);
	dcMem.SelectObject(&bmp); //将位图选择进内存DC
	dcMem.FillSolidRect(0, 0, CStaticValueECGNormal::m_i32NormalWidth, CStaticValueECGNormal::m_i32NormalHeight, RGB(0, 0, 0));

	dcMem.FillSolidRect(0, 0, 2, CStaticValueECGNormal::m_i32NormalHeight, RGB(0, 0, 0));
	//dcMem.FillSolidRect(1200, 0, 2, 160, CStaticValueECGNormal::m_rgbText);
	dcMem.FillSolidRect(70, 0, CStaticValueECGNormal::m_i32NormalWidth, 2, RGB(0, 0, 0));
	dcMem.FillSolidRect(0, CStaticValueECGNormal::m_i32NormalHeight - 2, CStaticValueECGNormal::m_i32BigWidth, CStaticValueECGNormal::m_i32NormalHeight, RGB(0, 0, 0));

	//dcMem.FillSolidRect(0, 0, CStaticValueECGNormal::m_i32BigWidth, CStaticValueECGNormal::m_i32NormalHeight, RGB(0, 0, 0));
	//dcMem.FillSolidRect(0, 0, 2, CStaticValueECGNormal::m_i32BigHeight, CStaticValueECGNormal::m_rgbText);
	//dcMem.FillSolidRect(1200, 0, 2, 160, CStaticValueECGNormal::m_rgbText);
	//dcMem.FillSolidRect(70, 0, CStaticValueECGNormal::m_i32BigWidth, 2, CStaticValueECGNormal::m_rgbText);
	//dcMem.FillSolidRect(0, CStaticValueECGNormal::m_i32BigHeight - 2, CStaticValueECGNormal::m_i32BigWidth, CStaticValueECGNormal::m_i32BigHeight, CStaticValueECGNormal::m_rgbText);


	bitmaBeat.LoadBitmap(IDB_BITMAP_BEAT);
	dcMemBeat.CreateCompatibleDC(&pDC);
	dcMemBeat.SelectObject(&bitmaBeat);

	bitmapAlm.LoadBitmap(IDB_BITMAP_ALM);
	dcMemAlm.CreateCompatibleDC(&pDC);
	dcMemAlm.SelectObject(&bitmapAlm);

	fontHR.CreateFont(96,                                          //   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("宋体"));

	fontHRBig.CreateFont(140,                                          //   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("宋体"));

	fontHRAlm.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("宋体"));

	font.CreateFont(24,                                          //   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.FillSolidRect(5, 0, 40, 20, RGB(0, 0, 0));
	dcMem.SelectObject(&font);
	dcMem.SetTextColor(RGB(0, 0xE1, 0));
	dcMem.TextOut(5, 0, _T("心电"));

	m_i32BlinkNum = 0;
	m_i32Blink = 0;
	m_i32AlmFlag = 0;
	//	return CStatic::OnCreate(lpCreateStruct);
	return TRUE;
}


void  CStaticValueECGNormal::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	this->GetParent()->PostMessage(WM_STATIC_VALUE_ECG_NORMAL_DBLCLK, 0, 0);
}


BOOL CStaticValueECGNormal::OnEraseBkgnd(CDC* pDC)
{

	return TRUE;
}


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

	pDC.BitBlt(0, 0, m_i32Width, m_i32Height, &dcMem, 0, 0, SRCCOPY);
	//	CStatic::OnPaint();
}
// CStaticValueECGNormal 消息处理程序



// 设置心律
int CStaticValueECGNormal::set_HR(INT16 hr, UINT8 redraw)
{
	CString a_cs;
	if ((m_i16HR != hr) || (redraw))
	{
		m_i16HR = hr;
		if (hr == CData::m_i16HRInvalidValue)
		{
			//-100代表无效值
			a_cs.Format(_T("%s"), _T("---"));
		}
		else
		{
			if (hr < 0)
				hr = 0;
			else if (hr>300)
				hr = 300;
			a_cs.Format(_T("%d"), hr);
		}

		if (m_i32Style == 0)
		{
			dcMem.FillSolidRect(4, 55, 151, 94, RGB(0, 0, 0));
			dcMem.SelectObject(&fontHR);
			dcMem.SetTextColor(RGB(0, 0xE1, 0));
			dcMem.TextOut(4, 55, a_cs);
		}
		else
		{
			dcMem.FillSolidRect(4, 55, 160, 128, RGB(0, 0, 0));
			dcMem.SelectObject(&fontHRBig);
			dcMem.SetTextColor(RGB(0, 0xE1, 0));
			dcMem.TextOut(4, 55, a_cs);
		}

		m_i32ReDraw = 1;
	}
	

	return 0;
}


// 设置ST1的值
int CStaticValueECGNormal::set_ST1(INT16 st1, UINT8 redraw)
{
	CString a_cs;
	float f;

	if ((m_i16ST1 != st1) || (redraw))
	{
		m_i16ST1 = st1;

		if (st1 == CData::m_i16STInvalidValue)
		{
			//-10000代表无效值
			a_cs.Format(_T("ST1 %s"), _T("---"));
		}
		else
		{
			if (st1 < -100)
				st1 = -100;
			else if (st1>100)
				st1 = 100;

			f = st1;
			f = f / 100.0;
			a_cs.Format(_T("ST1 %0.2f"), f);
		}

		if (m_i32Style == 0)
		{
			dcMem.FillSolidRect(220, 70, 120, 26, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.SetTextColor(RGB(0, 0xE1, 0));
			dcMem.TextOut(220, 70, a_cs);
		}
		else
		{
			dcMem.FillSolidRect(290, 90, 120, 26, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.SetTextColor(RGB(0, 0xE1, 0));
			dcMem.TextOut(290, 90, a_cs);
		}

		m_i32ReDraw = 1;
	}
	

	return 0;
}


// 设置ST2的值
int CStaticValueECGNormal::set_ST2(INT16 st2, UINT8 redraw)
{
	CString a_cs;
	float f;
	if ((m_i16ST2 != st2) || (redraw))
	{
		m_i16ST2 = st2;
		if (st2 == CData::m_i16STInvalidValue)
		{
			//-10000代表无效值
			a_cs.Format(_T("ST2 %s"), _T("---"));
		}
		else
		{
			if (st2 < -100)
				st2 = -100;
			else if (st2>100)
				st2 = 100;

			f = st2;
			f = f / 100.0;
			a_cs.Format(_T("ST2 %0.2f"), f);
		}

		if (m_i32Style == 0)
		{
			dcMem.FillSolidRect(220, 100, 120, 26, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.SetTextColor(RGB(0, 0xE1, 0));
			dcMem.TextOut(220, 100, a_cs);
		}
		else
		{
			dcMem.FillSolidRect(290, 120, 120, 26, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.SetTextColor(RGB(0, 0xE1, 0));
			dcMem.TextOut(290, 120, a_cs);
		}

		m_i32ReDraw = 1;
	}
	return 0;
}


// 设置心律报警上限
int CStaticValueECGNormal::set_HR_Alm_H(INT16 h, UINT8 redraw)
{
	CString a_cs;

	if ((m_i16HRH != h) || (redraw))
	{
		m_i16HRH = h;
		if (h < 0)
			h = 0;
		else if (h>300)
			h = 300;

		a_cs.Format(_T("%d"), h);

		if (m_i32Style == 0)
		{
			dcMem.FillSolidRect(162, 70, 41, 20, RGB(0, 0, 0));
			dcMem.SelectObject(&fontHRAlm);
			dcMem.SetTextColor(RGB(0, 0xE1, 0));
			dcMem.TextOut(162, 70, a_cs);
		}
		else
		{
			dcMem.FillSolidRect(235, 90, 41, 20, RGB(0, 0, 0));
			dcMem.SelectObject(&fontHRAlm);
			dcMem.SetTextColor(RGB(0, 0xE1, 0));
			dcMem.TextOut(235, 90, a_cs);
		}

		m_i32ReDraw = 1;
	}
	return 0;
}


// 设置心律报警下限
int CStaticValueECGNormal::set_HR_Alm_L(INT16 l, UINT8 redraw)
{
	CString a_cs;
	if ((m_i16HRL != l) || (redraw))
	{
		m_i16HRL = l;
		if (l < 0)
			l = 0;
		else if (l>300)
			l = 300;

		a_cs.Format(_T("%d"), l);

		if (m_i32Style == 0)
		{
			dcMem.FillSolidRect(162, 130, 41, 20, RGB(0, 0, 0));
			dcMem.SelectObject(&fontHRAlm);
			dcMem.SetTextColor(RGB(0, 0xE1, 0));
			dcMem.TextOut(162, 130, a_cs);
		}
		else
		{
			dcMem.FillSolidRect(235, 140, 41, 20, RGB(0, 0, 0));
			dcMem.SelectObject(&fontHRAlm);
			dcMem.SetTextColor(RGB(0, 0xE1, 0));
			dcMem.TextOut(235, 140, a_cs);
		}

		m_i32ReDraw = 1;
	}
	return 0;
}


// 心搏标识
int CStaticValueECGNormal::set_beat(INT16 b, UINT8 redraw)
{	
	if ((m_u8Beat != b) || (redraw))
	{
		m_u8Beat = b;
		if (m_i32Style == 0)
		{
			if (b == 0)
				dcMem.FillSolidRect(70, 10, 30, 30, RGB(0, 0, 0));
			else
				dcMem.BitBlt(70, 10, 30, 30, &dcMemBeat, 0, 0, SRCCOPY);
		}
		else
		{
			if (b == 0)
				dcMem.FillSolidRect(70, 10, 30, 30, RGB(0, 0, 0));
			else
				dcMem.BitBlt(70, 10, 30, 30, &dcMemBeat, 0, 0, SRCCOPY);
		}

		m_i32ReDraw = 1;
	}
	return 0;
}


// 心律来源
int CStaticValueECGNormal::set_HR_from(INT16 f, UINT8 redraw)
{
	CString a_cs;
	if ((m_u8HRFrom != f) || (redraw))
	{
		m_u8HRFrom = f;

		if (f == 0)
		{
			a_cs = _T("ECG");
			dcMem.SetTextColor(RGB(0, 0xE1, 0));
		}
		else
		{
			a_cs = _T("血氧");
			dcMem.SetTextColor(RGB(6, 200, 230));
		}

		if (m_i32Style == 0)
		{
			dcMem.FillSolidRect(160, 10, 60, 26, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.TextOut(160, 10, a_cs);
		}
		else
		{
			dcMem.FillSolidRect(230, 10, 60, 26, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.TextOut(230, 10, a_cs);
		}

		m_i32ReDraw = 1;
	}
	return 0;
}


// 设置PVCs值
int CStaticValueECGNormal::set_PVCs(INT16 p, UINT8 redraw)
{
	CString a_cs;
	if ((m_i16PVCs != p) || (redraw))
	{
		m_i16PVCs = p;
		if (p < 0)
			p = 0;
		else if (p>99)
			p = 99;


		a_cs.Format(_T("PVCs %d"), p);

		if (m_i32Style == 0)
		{
			dcMem.FillSolidRect(220, 130, 110, 26, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.SetTextColor(RGB(0, 0xE1, 0));
			dcMem.TextOut(220, 130, a_cs);
		}
		else
		{
			dcMem.FillSolidRect(290, 150, 110, 26, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.SetTextColor(RGB(0, 0xE1, 0));
			dcMem.TextOut(290, 150, a_cs);
		}

		m_i32ReDraw = 1;
	}
	return 0;
}


// 设置起搏
int CStaticValueECGNormal::set_pace(INT16 p, UINT8 redraw)
{
	CString a_cs;
	if ((m_i16Pace != p) || (redraw))
	{
		m_i16Pace = p;

		if (p == 0)
		{
			a_cs = _T("起搏关");
			dcMem.SetTextColor(RGB(0, 0xE1, 0));
		}
		else
		{
			a_cs = _T("起搏开");
			dcMem.SetTextColor(RGB(0, 0xE1, 0));
		}


		if (m_i32Style == 0)
		{
			dcMem.FillSolidRect(220, 10, 80, 20, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.TextOut(220, 10, a_cs);
		}
		else
		{
			dcMem.FillSolidRect(290, 10, 80, 20, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.TextOut(290, 10, a_cs);
		}

		m_i32ReDraw = 1;
	}
	return 0;
}


// 重画
void CStaticValueECGNormal::Redraw()
{
	if (m_i32ReDraw == 1)
	{
		m_i32ReDraw = 0;
		Invalidate(1);
	}
}


// 闪烁
void CStaticValueECGNormal::Blink()
{
	if (m_i32Blink)
	{
		m_i32BlinkNum++;
		//if (m_i32Style == 0)
		{
			if (m_i32BlinkNum & 1)
			{
				dcMem.FillSolidRect(0, 0, 2, m_i32Height, CStaticValueECGNormal::m_rgbText);
				//dcMem.FillSolidRect(1200, 0, 2, 160, CStaticValueECGNormal::m_rgbText);
				dcMem.FillSolidRect(70, 0, m_i32Width, 2, CStaticValueECGNormal::m_rgbText);
				dcMem.FillSolidRect(0, m_i32Height - 2, m_i32Width, m_i32Height, CStaticValueECGNormal::m_rgbText);
			}
			else
			{
				dcMem.FillSolidRect(0, 0, 2, m_i32Height, CStaticValueECGNormal::m_rgbBlink);
				//dcMem.FillSolidRect(1200, 0, 2, 160, CStaticValueECGNormal::m_rgbText);
				dcMem.FillSolidRect(70, 0, m_i32Width, 2, CStaticValueECGNormal::m_rgbBlink);
				dcMem.FillSolidRect(0, m_i32Height - 2, m_i32Width, m_i32Height, CStaticValueECGNormal::m_rgbBlink);
			}
		}	
	}
	else
	{
		m_i32BlinkNum = 0;
				
		dcMem.FillSolidRect(0, 0, 2, m_i32Height, RGB(0, 0, 0));
		//dcMem.FillSolidRect(1200, 0, 2, 160, CStaticValueECGNormal::m_rgbText);
		dcMem.FillSolidRect(70, 0, m_i32Width, 2, RGB(0, 0, 0));
		dcMem.FillSolidRect(0, m_i32Height - 2, m_i32Width, m_i32Height, RGB(0, 0, 0));
	}
	m_i32ReDraw = 1;
}

void CStaticValueECGNormal::Set_Blink(int Blink)
{
	m_i32Blink = Blink;
//	m_i32BlinkNum = 0;
}

void CStaticValueECGNormal::Set_Style(INT style)
{
	m_i32Style = style;
	
	if (m_i32Style == 0)
	{
		
		dcMem.FillSolidRect(0, 0, CStaticValueECGNormal::m_i32NormalWidth, CStaticValueECGNormal::m_i32NormalHeight, RGB(0, 0, 0));
		dcMem.FillSolidRect(5, 0, 40, 20, RGB(20, 20, 20));
		dcMem.SelectObject(&font);
		dcMem.SetTextColor(RGB(0, 0xE1, 0));
		dcMem.TextOut(5, 0, _T("心电"));
		m_i32Width = CStaticValueECGNormal::m_i32NormalWidth;
		m_i32Height = CStaticValueECGNormal::m_i32NormalHeight;
	}
	else
	{
		dcMem.FillSolidRect(0, 0, CStaticValueECGNormal::m_i32BigWidth, CStaticValueECGNormal::m_i32BigHeight, RGB(0, 0, 0));
		dcMem.FillSolidRect(5, 0, 40, 20, RGB(20, 20, 20));
		dcMem.SelectObject(&font);
		dcMem.SetTextColor(RGB(0, 0xE1, 0));
		dcMem.TextOut(5, 0, _T("心电"));
		m_i32Width = CStaticValueECGNormal::m_i32BigWidth;
		m_i32Height = CStaticValueECGNormal::m_i32BigHeight;
	}

	// 设置心律
	set_HR(m_i16HR,1);
	// 设置ST1的值
	set_ST1(m_i16ST1, 1);
	// 设置ST2的值
	set_ST2(m_i16ST2, 1);
	// 设置心律报警上限
	set_HR_Alm_H(m_i16HRH, 1);
	// 设置心律报警下限
	set_HR_Alm_L(m_i16HRL, 1);
	// 心搏标识
	set_beat(m_u8Beat, 1);
	// 心律来源
	set_HR_from(m_u8HRFrom, 1);
	// 设置PVCs值
	set_PVCs(m_i16PVCs, 1);
	// 设置起搏
	set_pace(m_i16Pace, 1);
	//设置、绘制报警开关标识
	Set_AlmFlag(m_i32AlmFlag, 1);
	UpdateWindow();
}

//设置、绘制报警开关标识
void CStaticValueECGNormal::Set_AlmFlag(INT a, UINT8 redraw)
{
	
	if ((m_i32AlmFlag != a) || (redraw))
	{
		m_i32AlmFlag = a;
		if (m_i32Style == 0)
		{
			if (a)
				dcMem.FillSolidRect(70+32, 10, 30, 30, RGB(0, 0, 0));
			else
				dcMem.BitBlt(70 + 32, 10, 30, 30, &dcMemAlm, 0, 0, SRCCOPY);
		}
		else
		{
			if (a)
				dcMem.FillSolidRect(70 + 32, 10, 30, 30, RGB(0, 0, 0));
			else
				dcMem.BitBlt(70 + 32, 10, 30, 30, &dcMemAlm, 0, 0, SRCCOPY);
		}

		m_i32ReDraw = 1;
	}
}