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

#include "stdafx.h"
#include "Meter.h"
#include "StaticLCD.h"
#include "MeterParam.h"

// CStaticLCD

IMPLEMENT_DYNAMIC(CStaticLCD, CStatic)

CStaticLCD::CStaticLCD()
{

}

CStaticLCD::~CStaticLCD()
{
	m_dcMem.DeleteDC();
	m_bmpMem.DeleteObject();
	CloseHandle(m_hDraw);
}


BEGIN_MESSAGE_MAP(CStaticLCD, CStatic)
	ON_WM_PAINT()
	ON_WM_CONTEXTMENU()
	ON_COMMAND(ID_POPUP_SAVEIMAGE, &CStaticLCD::OnPopupSaveimage)
	ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()



// CStaticLCD 消息处理程序

void CStaticLCD::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: 在此处添加消息处理程序代码
	// 不为绘图消息调用 CStatic::OnPaint()

	WaitForSingleObject(m_hDraw,INFINITE);
	ResetEvent(m_hDraw);
	dc.BitBlt(0,0,m_rcClient.Width(),m_rcClient.Height(),&m_dcMem,0,0,SRCCOPY);
	SetEvent(m_hDraw);
	if(m_pMeter->m_bSaveToBmp)
	{
		m_pMeter->m_bSaveToBmp = FALSE;
		SaveToBmp(m_hWnd,m_pMeter->m_strSaveFileName);
	}
}

void CStaticLCD::InitLCD()
{
	CPaintDC dc(this);

	GetClientRect(&m_rcClient);
	m_rcLCD = m_rcClient;
	m_rcLCD.DeflateRect(5,5,5,5);
	m_rcGrid = m_rcLCD;
	m_rcGrid.DeflateRect(20,230,70,40);

	m_dcMem.CreateCompatibleDC(&dc);
	m_bmpMem.CreateCompatibleBitmap(&dc,m_rcClient.Width(),m_rcClient.Height());
	m_dcMem.SelectObject(&m_bmpMem);
//
	m_hDraw = CreateEvent(NULL,FALSE,TRUE,NULL);
}

void CStaticLCD::DrawLCD()
{
	CDC dcMem;
	CBitmap bmpMem;

	dcMem.CreateCompatibleDC(&m_dcMem);
	bmpMem.CreateCompatibleBitmap(&m_dcMem,m_rcClient.Width(),m_rcClient.Height());
	dcMem.SelectObject(&bmpMem);

	dcMem.FillSolidRect(m_rcClient,RGB(151,183,245));
	dcMem.FillSolidRect(m_rcLCD,RGB(0,0,0));


	DrawMenu(&dcMem,CPoint(m_rcLCD.left,m_rcLCD.top));
	DrawDMMValue(&dcMem,CPoint(m_rcLCD.right - 160,m_rcLCD.CenterPoint().y - 100));
	DrawUnits(&dcMem,CPoint(m_rcLCD.right - 120,m_rcLCD.CenterPoint().y - 105));
	DrawDMMBar(&dcMem,CPoint(m_rcLCD.left + 20,m_rcLCD.bottom - 240));
	DrawGrid(&dcMem,m_rcGrid);
	DrawWave(&dcMem,m_rcGrid);

	WaitForSingleObject(m_hDraw,INFINITE);
	ResetEvent(m_hDraw);
	m_dcMem.BitBlt(0,0,m_rcClient.Width(),m_rcClient.Height(),&dcMem,0,0,SRCCOPY);
	SetEvent(m_hDraw);

	Invalidate();
	dcMem.DeleteDC();
	bmpMem.DeleteObject();
}


void CStaticLCD::DrawDMMValue(CDC* pDC,CPoint pt)
{
	dmmDrawValue(pDC->GetSafeHdc(),pt.x,pt.y,m_pMeter->m_DMMData.bOverflow,m_pMeter->m_DMMData.flValue,m_pMeter->m_DMMData.nDotPos,SKY_COLOR);
}

void CStaticLCD::DrawUnits(CDC* pDC,CPoint ptCenter)
{
	dmmDrawUnits(pDC->GetSafeHdc(),ptCenter.x,ptCenter.y,m_pMeter->m_DMMData.nUnit,m_pMeter->m_DMMData.nUnitType,SKY_COLOR);
}

void CStaticLCD::DrawDMMBar(CDC* pDC,CPoint point)
{
	dmmDrawBar(pDC->GetSafeHdc(),point.x, point.y, m_pMeter->m_DMMData.nBar,m_pMeter->m_DMMData.nBarSign,SKY_COLOR);
}

void CStaticLCD::DrawMenu(CDC* pDC,CPoint point)
{
	dmmDrawMenuAutoManual(pDC->GetSafeHdc(),point.x + 10,point.y + 10, m_pMeter->m_DMMData.nType, m_pMeter->m_DMMData.nMode,m_pMeter->m_DMMData.bAmA,SKY_COLOR);
	dmmDrawMenuRange(pDC->GetSafeHdc(),point.x + 75,point.y + 10,m_pMeter->m_DMMData.nType,m_pMeter->m_DMMData.nMode,m_pMeter->m_DMMData.nDCAC,m_pMeter->m_DMMData.bAmA,m_pMeter->m_DMMData.nRange,SKY_COLOR);
	dmmDrawMenuACDC(pDC->GetSafeHdc(),point.x + 165,point.y + 10,m_pMeter->m_DMMData.nDCAC,SKY_COLOR);
	dmmDrawRel(pDC->GetSafeHdc(),point.x + 220,point.y + 10,m_pMeter->m_DMMData.bRel,m_pMeter->m_DMMRelData.flValue,m_pMeter->m_DMMRelData.nDotPos,m_pMeter->m_DMMRelData.nUnit,m_pMeter->m_DMMRelData.nUnitType,SKY_COLOR);
	dmmDrawDiode(pDC->GetSafeHdc(),point.x + 350,point.y + 10,m_pMeter->m_DMMData.nType,SKY_COLOR);
	dmmDrawCAP(pDC->GetSafeHdc(),point.x + 350,point.y + 10,m_pMeter->m_DMMData.nType,SKY_COLOR);
	dmmDrawCONTI(pDC->GetSafeHdc(),point.x + 350,point.y + 10,m_pMeter->m_DMMData.nType,SKY_COLOR);
	dmmDrawRun(pDC->GetSafeHdc(),point.x + 420,point.y + 5,m_pMeter->m_bRun);
}

void CStaticLCD::DrawGrid(CDC* pDC,CRect rc)
{
	dmmDrawGrid(pDC->GetSafeHdc(),rc.left,rc.top,rc.right,rc.bottom,m_pMeter->m_nCurDIV,m_pMeter->m_nInterval,m_pMeter->m_DMMData.nType,m_pMeter->m_DMMData.nDCAC,RGB(0,100,0));
}

void CStaticLCD::DrawWave(CDC* pDC,CRect rc)
{
	double dbValue[MAX_DATA_LEN];
	double dbMax;
	if(m_pMeter->m_nDequeDataLen > 0)
	{
		dbMax = m_pMeter->m_dbMaxScale - m_pMeter->m_dbMinScale;
		if(dbMax != 0.0)
		{
			m_pMeter->GetDequeDataValues(dbValue);
			dmmDrawWave(pDC->GetSafeHdc(),rc.left,rc.top,rc.right,rc.bottom,m_pMeter->m_DMMData.nType,
						m_pMeter->m_DMMData.nDCAC,dbValue,m_pMeter->m_nDequeDataLen,
						dbMax,RGB(0,255,0));
		}
	}
}

void CStaticLCD::OnContextMenu(CWnd* pWnd, CPoint point)
{
	// TODO: 在此处添加消息处理程序代码
	CMenu menu;
	menu.LoadMenu(IDR_MENU_POPUP);
	CMenu* pSumMenu = menu.GetSubMenu(0);
	pSumMenu->TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON,point.x, point.y, this);
}


void CStaticLCD::OnPopupSaveimage()
{
	// TODO: 在此添加命令处理程序代码
	CFileDialog dlgSave(FALSE,_T(".bmp"),NULL,OFN_OVERWRITEPROMPT,_T("Bmp File(*.bmp)|*.bmp||"));
	dlgSave.m_ofn.lpstrTitle = _T("Save Data As Bmp...");
	if(IDOK==dlgSave.DoModal())
	{
		m_pMeter->m_strSaveFileName = dlgSave.GetPathName();
		m_pMeter->m_bSaveToBmp = TRUE;
	}
}


void CStaticLCD::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if(PtInRect(&m_rcLCD,point))
	{
		m_pMeter->m_nCurDataIndex = (int)(MAX_DATA_LEN * (1 - (point.x - m_rcLCD.left) * 1.0 / m_rcLCD.Width()));
	}
	CStatic::OnMouseMove(nFlags, point);
}
