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

#include "stdafx.h"
#include "Verbpo.h"
#include "StaticValueSPO2Normal.h"


INT CStaticValueSPO2Normal::m_i32NormalWidth = 340, CStaticValueSPO2Normal::m_i32NormalHeight = 115;
INT CStaticValueSPO2Normal::m_i32BigWidth = 412, CStaticValueSPO2Normal::m_i32BigHeight = 157;

INT CStaticValueSPO2Normal::m_i32NormalX = 940, CStaticValueSPO2Normal::m_i32NormalY = 481;
INT CStaticValueSPO2Normal::m_i32BigX = 868, CStaticValueSPO2Normal::m_i32BigY = 402;


COLORREF CStaticValueSPO2Normal::m_rgbBlink = RGB(255, 140, 64);
COLORREF CStaticValueSPO2Normal::m_rgbText = RGB(6, 200, 230);

// CStaticValueSPO2Normal

IMPLEMENT_DYNAMIC(CStaticValueSPO2Normal, CStatic)



CStaticValueSPO2Normal::CStaticValueSPO2Normal()
{
	m_i32Unit = 0;
}

CStaticValueSPO2Normal::~CStaticValueSPO2Normal()
{
	font.DeleteObject();
	font.DeleteObject();
	fontResultBig.DeleteObject();
	fontPR.DeleteObject();
	fontPRBig.DeleteObject();
	dcMem.DeleteDC();
	bmp.DeleteObject();

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

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

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

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

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

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

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

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

	fontPRBig.CreateFont(50,                                          //   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(CStaticValueSPO2Normal::m_rgbText);
	dcMem.TextOut(5, 0, _T("血氧"));

	dcMem.FillSolidRect(130, 5, 21, 24, RGB(0, 0, 0));
	dcMem.SelectObject(&font);
	dcMem.SetTextColor(RGB(0x80, 0xCD, 0xFF));
	dcMem.TextOut(130, 5, _T("%"));

	dcMem.FillSolidRect(212, 5, 60, 24, RGB(0, 0, 0));
	dcMem.SelectObject(&font);
	dcMem.SetTextColor(RGB(0x80, 0xCD, 0xFF));
	dcMem.TextOut(212, 5, _T("PR(BPM)"));


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



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



// CStaticValueSPO2Normal 消息处理程序

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

BOOL CStaticValueSPO2Normal::OnEraseBkgnd(CDC* pDC)
{

	return TRUE;
}

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

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

// CStaticValueSPO2Normal 消息处理程序



// 设置血氧饱和度
int CStaticValueSPO2Normal::set_spo2(INT16 s, UINT8 redraw)
{
	CString a_cs;
	if ((m_i16Spo2 != s) || (redraw))
	{
		// 设置血氧饱和度
		m_i16Spo2 = s;

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

		if (m_i32Style == 0)
		{
			dcMem.FillSolidRect(4, 30, 120, 71, RGB(0, 0, 0));
			dcMem.SelectObject(&fontResult);
			dcMem.SetTextColor(RGB(0x80, 0xCD, 0xFF));
			dcMem.TextOut(4, 30, a_cs);
		}
		else
		{
			dcMem.FillSolidRect(4, 45, 160, 100, RGB(0, 0, 0));
			dcMem.SelectObject(&fontResultBig);
			dcMem.SetTextColor(RGB(0x80, 0xCD, 0xFF));
			dcMem.TextOut(4, 45, a_cs);
		}

		m_i32ReDraw = 1;
	}
	

	return 0;
}


// 设置脉率和度
int CStaticValueSPO2Normal::set_PR(INT16 pr, UINT8 redraw)
{
	CString a_cs;
	if ((m_i16PR != pr) || (redraw))
	{
		// 设置血氧饱和度
		m_i16PR = pr;

		if (pr == CData::m_i16PRInvalidValue)
		{
			//-100代表无效值
			a_cs.Format(_T("%s"), _T("---"));
		}
		else
		{
			if (pr < 0)
				pr = 0;
			else if (pr>300)
				pr = 300;
			a_cs.Format(_T("%d"), pr);
		}

		if (m_i32Style == 0)
		{
			dcMem.FillSolidRect(208, 42, 70, 24, RGB(0, 0, 0));
			dcMem.SelectObject(&fontPR);
			dcMem.SetTextColor(RGB(0x80, 0xCD, 0xFF));
			dcMem.TextOut(208, 42, a_cs);
		}
		else
		{
			dcMem.FillSolidRect(242, 68, 80, 30, RGB(0, 0, 0));
			dcMem.SelectObject(&fontPRBig);
			dcMem.SetTextColor(RGB(0x80, 0xCD, 0xFF));
			dcMem.TextOut(242, 68, a_cs);
		}

		m_i32ReDraw = 1;
	}


	return 0;
}

// 设置血氧饱和度上限
int CStaticValueSPO2Normal::set_spo2_alm_H(INT16 h, UINT8 redraw)
{
	CString a_cs;
	// 设置血氧饱和度上限
	if( (m_i16Spo2H != h) || (redraw))
	{
		m_i16Spo2H = h;

		if (h < 0)
			h = 0;
		else if (h>100)
			h = 100;

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

		if (m_i32Style == 0)
		{
			dcMem.FillSolidRect(130, 40, 50, 20, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.SetTextColor(RGB(0x80, 0xCD, 0xFF));
			dcMem.TextOut(130, 40, a_cs);
		}
		else
		{
			dcMem.FillSolidRect(166, 70, 61, 20, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.SetTextColor(RGB(0x80, 0xCD, 0xFF));
			dcMem.TextOut(166, 70, a_cs);
		}

		m_i32ReDraw = 1;
	}
	

	return 0;
}


// 设置血氧饱和度下限
int CStaticValueSPO2Normal::set_spo2_alm_L(INT16 l, UINT8 redraw)
{
	CString a_cs;

	if ((m_i16Spo2L != l) || (redraw))
	{
		// 设置血氧饱和度下限
		m_i16Spo2L = l;

		if (l < 0)
			l = 0;
		else if (l>100)
			l = 100;

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

		if (m_i32Style == 0)
		{
			dcMem.FillSolidRect(130, 70, 45, 20, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.SetTextColor(RGB(0x80, 0xCD, 0xFF));
			dcMem.TextOut(130, 70, a_cs);
		}
		else
		{
			dcMem.FillSolidRect(166, 105, 45, 20, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.SetTextColor(RGB(0x80, 0xCD, 0xFF));
			dcMem.TextOut(166, 105, a_cs);
		}
		m_i32ReDraw = 1;
	}
	
	return 0;
}


// 设置脉率上限
int CStaticValueSPO2Normal::set_PR_alm_H(INT16 h, UINT8 redraw)
{
	CString a_cs;
	if ((m_i16PRH != h) || (redraw))
	{
		// 设置血氧饱和度
		m_i16PRH = h;

		if (h < 0)
			h = 0;
		else if (h>300)
			h = 300;
		a_cs.Format(_T("%d"), h);
		
		if (m_i32Style == 0)
		{
			dcMem.FillSolidRect(285, 35, 35, 20, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.SetTextColor(RGB(0x80, 0xCD, 0xFF));
			dcMem.TextOut(285, 35, a_cs);
		}
		else
		{
			dcMem.FillSolidRect(330, 60, 35, 25, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.SetTextColor(RGB(0x80, 0xCD, 0xFF));
			dcMem.TextOut(330, 60, a_cs);
		}

		m_i32ReDraw = 1;
	}

	return 0;
}


// 设置脉率下限
int CStaticValueSPO2Normal::set_PR_alm_L(INT16 l, UINT8 redraw)
{
	CString a_cs;
	if ((m_i16PRL != l) || (redraw))
	{
		// 设置血氧饱和度
		m_i16PRL = l;

		if (l < 0)
			l = 0;
		else if (l>300)
			l = 300;
		a_cs.Format(_T("%d"), l);

		if (m_i32Style == 0)
		{
			dcMem.FillSolidRect(285, 65, 35, 20, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.SetTextColor(RGB(0x80, 0xCD, 0xFF));
			dcMem.TextOut(285, 65, a_cs);
		}
		else
		{
			dcMem.FillSolidRect(330, 95, 35, 25, RGB(0, 0, 0));
			dcMem.SelectObject(&font);
			dcMem.SetTextColor(RGB(0x80, 0xCD, 0xFF));
			dcMem.TextOut(330, 95, a_cs);
		}
		m_i32ReDraw = 1;
	}

	return 0;
}



//设置血氧棒图
void CStaticValueSPO2Normal::Set_Bar(UINT8 data, UINT8 redraw)
{
	INT i;
	if ((data != m_u8Bar) || (redraw))
	{
		m_u8Bar = data;
		if (m_i32Style == 0)
		{
			dcMem.FillSolidRect(182, CStaticValueSPO2Normal::m_i32NormalHeight-30-48, 12, 50, RGB(0, 0, 0));
			for (i = 0; i < data; i++)
			{
				dcMem.FillSolidRect(182, CStaticValueSPO2Normal::m_i32NormalHeight - 30 - 3 * (i), 12, 2, CStaticValueSPO2Normal::m_rgbText);
			}
		}
		else
		{
			dcMem.FillSolidRect(215, 15, 18, CStaticValueSPO2Normal::m_i32BigHeight - 20, RGB(0, 0, 0));
			for (i = 0; i < data; i++)
			{
				dcMem.FillSolidRect(215, CStaticValueSPO2Normal::m_i32BigHeight - 40 - 4 * (i), 15, 2, CStaticValueSPO2Normal::m_rgbText);
			}
		}
		m_i32ReDraw = 1;
	}
}

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

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


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

	if (m_i32Style == 0)
	{

		dcMem.FillSolidRect(0, 0, CStaticValueSPO2Normal::m_i32NormalWidth, CStaticValueSPO2Normal::m_i32NormalHeight, RGB(0, 0, 0));
		dcMem.FillSolidRect(5, 0, 40, 20, RGB(0, 0, 0));
		dcMem.SelectObject(&font);
		dcMem.SetTextColor(CStaticValueSPO2Normal::m_rgbText);
		dcMem.TextOut(5, 0, _T("血氧"));

		dcMem.FillSolidRect(130, 5, 21, 24, RGB(0, 0, 0));
		dcMem.SelectObject(&font);
		dcMem.SetTextColor(RGB(0x80, 0xCD, 0xFF));
		dcMem.TextOut(130, 5, _T("%"));

		dcMem.FillSolidRect(212, 5, 70, 24, RGB(0, 0, 0));
		dcMem.SelectObject(&font);
		dcMem.SetTextColor(RGB(0x80, 0xCD, 0xFF));
		dcMem.TextOut(212, 5, _T("PR(BPM)"));


		m_i32Width = CStaticValueSPO2Normal::m_i32NormalWidth;
		m_i32Height = CStaticValueSPO2Normal::m_i32NormalHeight;
	}
	else
	{
		dcMem.FillSolidRect(0, 0, CStaticValueSPO2Normal::m_i32BigWidth, CStaticValueSPO2Normal::m_i32BigHeight, RGB(0, 0, 0));
		dcMem.FillSolidRect(5, 0, 40, 20, RGB(0, 0, 0));
		dcMem.SelectObject(&font);
		dcMem.SetTextColor(CStaticValueSPO2Normal::m_rgbText);
		dcMem.TextOut(5, 0, _T("血氧"));

		dcMem.FillSolidRect(166, 20, 21, 32, RGB(0, 0, 0));
		dcMem.SelectObject(&font);
		dcMem.SetTextColor(RGB(0x80, 0xCD, 0xFF));
		dcMem.TextOut(166, 20, _T("%"));

		dcMem.FillSolidRect(252, 20, 70, 24, RGB(0, 0, 0));
		dcMem.SelectObject(&font);
		dcMem.SetTextColor(RGB(0x80, 0xCD, 0xFF));
		dcMem.TextOut(252, 20, _T("PR(BPM)"));


		m_i32Width = CStaticValueSPO2Normal::m_i32BigWidth;
		m_i32Height = CStaticValueSPO2Normal::m_i32BigHeight;
	}
	
	// 设置血氧饱和度
	set_spo2(m_i16Spo2,1);
	set_PR(m_i16PR, 1);
	// 设置血氧饱和度上限
	set_spo2_alm_H(m_i16Spo2H, 1);
	// 设置血氧饱和度下限
	set_spo2_alm_L(m_i16Spo2L, 1);
	// 设置脉率上限
	set_PR_alm_H(m_i16PRH, 1);
	// 设置脉率下限
	set_PR_alm_L(m_i16PRL, 1);
	//设置、绘制报警开关标识
	Set_AlmFlag(m_i32AlmFlag, 1);
	UpdateWindow();
}

//设置、绘制报警开关标识
void CStaticValueSPO2Normal::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;
	}
}