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

#include "pch.h"

#include "MCA.h"
#include "MCADoc.h"
#include "DetailView.h"
#include "ControlView.h"


// CDetailView

IMPLEMENT_DYNCREATE(CDetailView, CView)

CDetailView::CDetailView()
{
}

CDetailView::~CDetailView()
{
}

BEGIN_MESSAGE_MAP(CDetailView, CView)
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONDOWN()
END_MESSAGE_MAP()


// CDetailView 绘图

void CDetailView::OnDraw(CDC* pDc)
{
	// 获取区域
	int L, R;
	if (pDoc == NULL)   pDoc = (CMCADoc*)GetDocument();
	if (pView == NULL)   pView = (CControlView*)pDoc->GetView(RUNTIME_CLASS(CControlView));
	L = pView->m_nCursor1;
	R = pView->m_nCursor2;
	// 缩放
	CRect rect;
	GetClientRect(&rect);
	ZF = rect.Width() / (R - L + 1.);

	// 背景颜色、笔颜色等设置
	CDC MemDC;
	CBitmap Bitmap, * OldBitmap;
	CPen pen(PS_SOLID, 2, RGB(255, 255, 255));
	CPen* oldpen = MemDC.SelectObject(&pen);

	GetClientRect(&rect);
	MemDC.CreateCompatibleDC(pDc);
	Bitmap.CreateCompatibleBitmap(pDc, rect.Width(), rect.Height());
	OldBitmap = MemDC.SelectObject(&Bitmap);
	MemDC.FillSolidRect(0, 0, rect.Width(), rect.Height(), RGB(119, 7, 243));

	// 绘图
	MemDC.MoveTo(1, rect.Height() - pDoc->m_nChannelCount[L]);
	for (int i = L; i <= R; i++)
		MemDC.LineTo((i - L) * ZF, rect.Height() - pDoc->m_nChannelCount[i]);

	//释放资源
	pDc->BitBlt(0, 0, rect.Width(), rect.Width(), &MemDC, 0, 0, SRCCOPY);
	MemDC.SelectObject(oldpen);
	MemDC.SelectObject(OldBitmap);
	pen.DeleteObject();
	Bitmap.DeleteObject();
	MemDC.DeleteDC();
}


// CDetailView 诊断

#ifdef _DEBUG
void CDetailView::AssertValid() const
{
	CView::AssertValid();
}

#ifndef _WIN32_WCE
void CDetailView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}
#endif
#endif //_DEBUG


// CDetailView 消息处理程序


void CDetailView::OnPaint()
{
	if (pDoc == NULL)   pDoc = (CMCADoc*)GetDocument();
	CPaintDC dc(this); // device context for painting
	OnPrepareDC(&dc);
	OnDraw(&dc);
}

BOOL CDetailView::OnEraseBkgnd(CDC* pDC)
{
	return TRUE;
}

void CDetailView::OnLButtonDown(UINT nFlags, CPoint point)
{
	if (pDoc == NULL)   pDoc = (CMCADoc*)GetDocument();
	if (pView == NULL)   pView = (CControlView*)pDoc->GetView(RUNTIME_CLASS(CControlView));
	int nX = point.x / ZF + pView->m_nCursor1;
	pView->m_nCursorROI = nX;
	pView->m_nCursorROICount = pDoc->m_nChannelCount[nX];

	CPen pen;
	CDC* pDC = GetDC();
	pen.CreatePen(PS_SOLID, 5, RGB(255, 180, 20));
	CPen* oldpen = pDC->SelectObject(&pen);
	CRect rect;
	GetClientRect(&rect);
	pDC->SetROP2(R2_XORPEN); //擦除
	pDC->MoveTo(point.x, 0); //绘制光标
	pDC->LineTo(point.x, rect.Height());
	pDC->SelectObject(oldpen);
	pen.DeleteObject();

	CView::OnLButtonDown(nFlags, point);
}


void CDetailView::OnRButtonDown(UINT nFlags, CPoint point)
{
	if (pDoc == NULL)   pDoc = (CMCADoc*)GetDocument();
	if (pView == NULL)   pView = (CControlView*)pDoc->GetView(RUNTIME_CLASS(CControlView));
	pView->m_nCursor1 = 0;
	pView->m_nCursor2 = 1023;

	CView::OnRButtonDown(nFlags, point);
}