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

#include "stdafx.h"
#include "Verbpo.h"
#include "StaticBPValueNormal.h"


INT CStaticBPValueNormal::m_i32NormalWidth = 340, CStaticBPValueNormal::m_i32NormalHeight = 140;
INT CStaticBPValueNormal::m_i32BigWidth = 868, CStaticBPValueNormal::m_i32BigHeight = 203;

INT CStaticBPValueNormal::m_i32NormalX = 940, CStaticBPValueNormal::m_i32NormalY = 222;
INT CStaticBPValueNormal::m_i32BigX = 0, CStaticBPValueNormal::m_i32BigY = 540;

COLORREF CStaticBPValueNormal::m_rgbBlink = RGB(255, 140, 64);
COLORREF CStaticBPValueNormal::m_rgbText = RGB(210, 210, 210);

// CStaticBPValueNormal

IMPLEMENT_DYNAMIC(CStaticBPValueNormal, CStatic)

CStaticBPValueNormal::CStaticBPValueNormal()
{

}

CStaticBPValueNormal::~CStaticBPValueNormal()
{
	font.DeleteObject();
	fontBig.DeleteObject();
	font1.DeleteObject();
	fontResult.DeleteObject();
	fontResultBig.DeleteObject();
	fontList.DeleteObject();
	dcMem.DeleteDC();
	bmp.DeleteObject();
	m_stNIBPResult.clear();

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

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

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


	dcMem.FillSolidRect(0, 0, 2, CStaticBPValueNormal::m_i32NormalHeight, RGB(0, 0, 0));
	//dcMem.FillSolidRect(1200, 0, 2, 160, RGB(30, 200, 160));
	dcMem.FillSolidRect(70, 0, CStaticBPValueNormal::m_i32NormalWidth, 2, RGB(0, 0, 0));
	dcMem.FillSolidRect(0, CStaticBPValueNormal::m_i32NormalHeight - 2, CStaticBPValueNormal::m_i32NormalWidth, CStaticBPValueNormal::m_i32NormalHeight, RGB(0, 0, 0));
	

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

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

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

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

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

	fontList.CreateFont(16,                                          //   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(210, 210, 210));
	dcMem.TextOut(5, 0, _T("血压"));

	dcMem.FillSolidRect(220, 5, 71, 31, RGB(0, 0, 0));
	dcMem.SelectObject(&font);
	dcMem.SetTextColor(RGB(0xDD, 0xDD, 0xEE));
	dcMem.TextOut(220, 5, _T("mmHg"));


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


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

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



// CStaticBPValueNormal 消息处理程序


// 设置时间
int CStaticBPValueNormal::set_time(int h, int m, int s, UINT8 redraw)
{
	CString a_cs;
	
	if ((m_i16Hour != h) || (m_i16Min != m) || (m_i16Sec != s) || (redraw))
	{
		// 设置血压时间
		m_i16Hour = h, m_i16Min = m, m_i16Sec = s;

		a_cs.Format(_T("%2d:%2d:%2d"), h, m, s);

		if (m_i32Style == 0)
		{
			dcMem.FillSolidRect(100, 5, 121, 28, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.SetTextColor(RGB(0xDD, 0xDD, 0xEE));
			dcMem.TextOut(100, 5, a_cs);
		}
		else
		{
			dcMem.FillSolidRect(100, 5, 160, 40, RGB(0, 0, 0));
			dcMem.SelectObject(&fontBig);
			dcMem.SetTextColor(RGB(0xDD, 0xDD, 0xEE));
			dcMem.TextOut(100, 5, a_cs);
		}
		m_i32ReDraw = 1;
	}
	
	return 0;
}

// 设置血压结果
int CStaticBPValueNormal::set_result(int SBP, int DBP, UINT8 redraw)
{
	CString a_cs;

	if ((m_i16SBP != SBP) || (m_i16DBP != DBP) || (redraw))
	{
		// 设置血压结果
		m_i16SBP = SBP, m_i16DBP = DBP;


		if (SBP < 0)
			SBP = 0;
		else if (SBP>300)
			SBP = 300;

		if (DBP < 0)
			DBP = 0;
		else if (DBP>SBP)
			DBP = SBP;

		a_cs.Format(_T("%d/%d"), SBP, DBP);

		if (m_i32Style == 0)
		{
			dcMem.FillSolidRect(4, 70, 210, 56, RGB(0, 0, 0));
			dcMem.SelectObject(&fontResult);
			dcMem.SetTextColor(RGB(0xDD, 0xDD, 0xEE));
			dcMem.TextOut(4, 70, a_cs);
		}
		else
		{
			dcMem.FillSolidRect(4, 100, 270, 80, RGB(0, 0, 0));
			dcMem.SelectObject(&fontResultBig);
			dcMem.SetTextColor(RGB(0xDD, 0xDD, 0xEE));
			dcMem.TextOut(4, 100, a_cs);
		}

		m_i32ReDraw = 1;
	}
	
	return 0;
}

// 设置报警上限
int CStaticBPValueNormal::set_alm_H(int SBP, int DBP, UINT8 redraw)
{
	CString a_cs;

	if ((m_i16SBPAlmH != SBP) || (m_i16DBPAlmH != DBP) || (redraw))
	{
		// 设置血压高阈值
		m_i16SBPAlmH = SBP, m_i16DBPAlmH = DBP;

		if (SBP < 0)
			SBP = 0;
		else if (SBP>300)
			SBP = 300;

		if (DBP < 0)
			DBP = 0;
		else if (DBP>SBP)
			DBP = SBP;

		a_cs.Format(_T("%d/%d"), SBP, DBP);

		if (m_i32Style == 0)
		{
			dcMem.FillSolidRect(220, 40, 101, 26, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.SetTextColor(RGB(0xDD, 0xDD, 0xEE));
			dcMem.TextOut(220, 40, a_cs);
		}
		else
		{
			dcMem.FillSolidRect(370, 100, 101, 26, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.SetTextColor(RGB(0xDD, 0xDD, 0xEE));
			dcMem.TextOut(370, 100, a_cs);
		}

		m_i32ReDraw = 1;
	}
	return 0;
}

// 设置报警下限
int CStaticBPValueNormal::set_alm_L(int SBP, int DBP, UINT8 redraw)
{
	CString a_cs;

	if ((m_i16SBPAlmL != SBP) || (m_i16DBPAlmL != DBP) || (redraw))
	{
		// 设置血压低阈值
		m_i16SBPAlmL = SBP, m_i16DBPAlmL = DBP;

		if (SBP < 0)
			SBP = 0;
		else if (SBP>300)
			SBP = 300;

		if (DBP < 0)
			DBP = 0;
		else if (DBP>SBP)
			DBP = SBP;

		a_cs.Format(_T("%d/%d"), SBP, DBP);

		if (m_i32Style == 0)
		{
			dcMem.FillSolidRect(220, 103, 101, 26, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.SetTextColor(RGB(0xDD, 0xDD, 0xEE));
			dcMem.TextOut(220, 103, a_cs);
		}
		else
		{
			dcMem.FillSolidRect(370, 163, 101, 26, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.SetTextColor(RGB(0xDD, 0xDD, 0xEE));
			dcMem.TextOut(370, 163, a_cs);
		}

		m_i32ReDraw = 1;
	}
	return 0;
}

// 设置静压
int CStaticBPValueNormal::set_press(int p, UINT8 redraw)
{
	CString a_cs;
	if ((m_i16Press != p) || (redraw))
	{
		// 设置袖带压
		m_i16Press = m_i16Press;

		if (p < 0)
			p = 0;
		else if (p>300)
			p = 300;

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

		if (m_i32Style == 0)
		{
			dcMem.FillSolidRect(220, 70, 58, 25, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.SetTextColor(RGB(0xDD, 0xDD, 0xEE));
			dcMem.TextOut(220, 70, a_cs);
		}
		else
		{
			dcMem.FillSolidRect(370, 135, 58, 25, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.SetTextColor(RGB(0xDD, 0xDD, 0xEE));
			dcMem.TextOut(370, 135, a_cs);
		}

		m_i32ReDraw = 1;
	}
	return 0;
}


BOOL CStaticBPValueNormal::OnEraseBkgnd(CDC* pDC)
{

	return TRUE;
}


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

	pDC.BitBlt(0, 0, m_i32Width, m_i32Height, &dcMem, 0, 0, SRCCOPY);
	//	CStatic::OnPaint();
}

// 闪烁
void CStaticBPValueNormal::Blink()
{
	if (m_i32Blink)
	{
		m_i32BlinkNum++;
		//if (m_i32Style == 0)
		{
			if (m_i32BlinkNum & 1)
			{
				dcMem.FillSolidRect(0, 0, 2, m_i32Height, CStaticBPValueNormal::m_rgbText);
				//dcMem.FillSolidRect(1200, 0, 2, 160, CStaticValueECGNormal::m_rgbText);
				dcMem.FillSolidRect(70, 0, m_i32Width, 2, CStaticBPValueNormal::m_rgbText);
				dcMem.FillSolidRect(0, m_i32Height - 2, m_i32Width, m_i32Height, CStaticBPValueNormal::m_rgbText);
			}
			else
			{
				dcMem.FillSolidRect(0, 0, 2, m_i32Height, CStaticBPValueNormal::m_rgbBlink);
				//dcMem.FillSolidRect(1200, 0, 2, 160, CStaticValueECGNormal::m_rgbText);
				dcMem.FillSolidRect(70, 0, m_i32Width, 2, CStaticBPValueNormal::m_rgbBlink);
				dcMem.FillSolidRect(0, m_i32Height - 2, m_i32Width, m_i32Height, CStaticBPValueNormal::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 CStaticBPValueNormal::Redraw()
{
	if (m_i32ReDraw == 1)
	{
		m_i32ReDraw = 0;
		Invalidate(1);
	}
}

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


void CStaticBPValueNormal::Set_Style(INT style)
{
	m_i32Style = style;

	if (m_i32Style == 0)
	{
		dcMem.FillSolidRect(0, 0, CStaticBPValueNormal::m_i32NormalWidth, CStaticBPValueNormal::m_i32NormalHeight, RGB(0, 0, 0));
		dcMem.FillSolidRect(5, 0, 40, 20, RGB(0, 0, 0));
		dcMem.SelectObject(&font);
		dcMem.SetTextColor(RGB(210, 210, 210));
		dcMem.TextOut(5, 0, _T("血压"));

		dcMem.FillSolidRect(220, 5, 71, 31, RGB(0, 0, 0));
		dcMem.SelectObject(&font);
		dcMem.SetTextColor(RGB(0xDD, 0xDD, 0xEE));
		dcMem.TextOut(220, 5, _T("mmHg"));
		m_i32Width = CStaticBPValueNormal::m_i32NormalWidth;
		m_i32Height = CStaticBPValueNormal::m_i32NormalHeight;
	}
	else
	{
		dcMem.FillSolidRect(0, 0, CStaticBPValueNormal::m_i32BigWidth, CStaticBPValueNormal::m_i32BigHeight, RGB(0, 0, 0));
		dcMem.FillSolidRect(5, 0, 40, 20, RGB(0, 0, 0));
		dcMem.SelectObject(&font);
		dcMem.SetTextColor(RGB(210, 210, 210));
		dcMem.TextOut(5, 0, _T("血压"));

		dcMem.FillSolidRect(370, 15, 71, 31, RGB(0, 0, 0));
		dcMem.SelectObject(&font);
		dcMem.SetTextColor(RGB(0xDD, 0xDD, 0xEE));
		dcMem.TextOut(370, 15, _T("mmHg"));
		m_i32Width = CStaticBPValueNormal::m_i32BigWidth;
		m_i32Height = CStaticBPValueNormal::m_i32BigHeight;
		Show_ResultList();
	}

	// 设置时间
	set_time(m_i16Hour, m_i16Min, m_i16Sec,1);
	// 设置血压结果
	set_result(m_i16SBP, m_i16DBP, 1);
	// 设置报警上限
	set_alm_H(m_i16SBPAlmH, m_i16DBPAlmH, 1);
	// 设置报警下限
	set_alm_L(m_i16SBPAlmL, m_i16DBPAlmL, 1);
	// 设置静压
	set_press(m_i16Press, 1);
	Set_AlmFlag(m_i32AlmFlag, 1);
	UpdateWindow();
}

void CStaticBPValueNormal::Add_Result(INT16 i16SBP, INT16 i16DBP, INT16 i16MBP, INT16 i16PR, INT16 i16Flag)
{
	INT i = 0, j = 0;
	struct RESULT a_stNIBP_RESULT;
	CString cs1;
	COleDateTime tm = COleDateTime::GetCurrentTime();
	cs1.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"), tm.GetYear(), tm.GetMonth(), tm.GetDay(), tm.GetHour(), tm.GetMinute(), tm.GetSecond());

	a_stNIBP_RESULT.i16SBP = i16SBP;
	a_stNIBP_RESULT.i16DBP = i16DBP;
	a_stNIBP_RESULT.i16MBP = i16MBP;
	a_stNIBP_RESULT.i16PR = i16PR;
	a_stNIBP_RESULT.i16Flag = i16Flag;
	wcscpy_s(a_stNIBP_RESULT.cTime, 20, cs1);

	if (m_stNIBPResult.size()<9)
	{
		
		m_stNIBPResult.push_front(a_stNIBP_RESULT);
	}
	else
	{
		m_stNIBPResult.pop_back();
		m_stNIBPResult.push_front(a_stNIBP_RESULT);
	}
}

void CStaticBPValueNormal::Show_ResultList()
{
	INT i = 0,j = 0;
	list<struct RESULT>::iterator index;
	CString a_cs;

	if (m_i32Style == 1)
	{
		//大字体界面
		if (m_stNIBPResult.size())
		{
			for (index = m_stNIBPResult.begin(); (index != m_stNIBPResult.end())&&(j<9); index++,j++)
			{
				if (index->i16Flag)
				{
					a_cs.Format(_T("%s          "), _T("无结果"));
				}
				else
				{
					a_cs.Format(_T("%3d/%3d (%3d)   "), index->i16SBP, index->i16DBP, index->i16MBP);
				}
				a_cs += index->cTime;
				dcMem.SelectObject(&fontList);
				dcMem.SetTextColor(RGB(0xDD, 0xDD, 0xEE));
				dcMem.FillSolidRect(480, 10 + 20 * j, 200, 10 + 20 * (j + 1), RGB(0, 0, 0));
				dcMem.TextOut(480, 10 + 20 * j, a_cs);
			}
		}
	}
}

//设置、绘制报警开关标识
void CStaticBPValueNormal::Set_AlmFlag(INT a, UINT8 redraw)
{

	if ((m_i32AlmFlag != a) || (redraw))
	{
		m_i32AlmFlag = a;
		if (m_i32Style == 0)
		{
			if (a)
				dcMem.FillSolidRect(70, 0, 28, 25, RGB(0, 0, 0));
			else
				dcMem.BitBlt(70, 0, 28, 25, &dcMemAlm, 0, 0, SRCCOPY);
		}
		else
		{
			if (a)
				dcMem.FillSolidRect(70, 0, 28, 25, RGB(0, 0, 0));
			else
				dcMem.BitBlt(70, 0, 28, 25, &dcMemAlm, 0, 0, SRCCOPY);
		}

		m_i32ReDraw = 1;
	}
}