#include "stdafx.h" #include #include "PlotCanvas.h" #include ".\PlotCanvas.h" ///////////////////////////////////////////////////////////////////////////// // CPlotCanvas CPlotCanvas::CPlotCanvas() { m_crDisplayBGColor = ctrlColWhite; m_crPlotColor = ctrlColRed; m_crCursorColor = ctrlColBlack; m_crTextFGColor = ctrlColBlack; m_crTextBGColor = ctrlColWhite; hasBackground = false; PersistenceMode = false; CursorPosition = -1; // don't plot if not set XPlotMAX = 0; YPlotMAX = 0; for(int i=0; i<8192;i++) { YPlotBuffer[i] = 0; } OptScaleType = false; //true = log,false=linear OptPlotType = false; //true = line,false=solid } CPlotCanvas::~CPlotCanvas() { } BEGIN_MESSAGE_MAP(CPlotCanvas, CStatic) //{{AFX_MSG_MAP(CPlotCanvas) ON_WM_ERASEBKGND() ON_WM_PAINT() ON_WM_LBUTTONDOWN() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CPlotCanvas message handlers /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// BOOL CPlotCanvas::OnEraseBkgnd(CDC* pDC) { return TRUE; // do not erase background } /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// void CPlotCanvas::OnRefreshBkgnd() { hasBackground = false; // redraw background } /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// void CPlotCanvas::OnPaint() { CPaintDC dc(this); // device context for painting CRect rect; GetClientRect(&rect); DrawPlotCanvas(&rect,&dc); } /////////////////////////////////////////////////////////////////////////////// // DrawPlotCanvas // The DrawPlotCanvas method will draw a compas with the heading centered. /////////////////////////////////////////////////////////////////////////////// void CPlotCanvas::DrawPlotCanvas(CRect *pRect, CDC *pDCa) { BOOL isPlotPen = TRUE; BOOL CurrentPen = TRUE; //true=plot pen, false =roi pen CDC *pDC = pDCa; // Get DC if no DC is passed if(pDCa == NULL) pDC = GetDC(); CDC dcMem; // Create temporary work DC (memory DC) and bitmap CBitmap bmTemp; dcMem.CreateCompatibleDC(pDC); bmTemp.CreateCompatibleBitmap( pDC,pRect->Width(),pRect->Height() ); CBitmap *pbmOld = dcMem.SelectObject(&bmTemp); CRect rect = *pRect; dcMem.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY) ; CString strRectInfo; strRectInfo.Format("Width: %d Height: %d", rect.Width(), rect.Height()); // Draw background rectangle CBrush WorkSpaceBrush( m_crDisplayBGColor ); CBrush *pOldBrush = dcMem.SelectObject(&WorkSpaceBrush); CPen *pOldPen = (CPen *)dcMem.SelectStockObject( NULL_PEN ); if (!hasBackground || !PersistenceMode) { dcMem.Rectangle(&rect); dcMem.Draw3dRect(&rect, (COLORREF)GetSysColor(COLOR_3DSHADOW), (COLORREF)GetSysColor(COLOR_3DHILIGHT)); hasBackground = true; } //dcMem.SelectStockObject( BLACK_PEN); dcMem.SelectStockObject(WHITE_PEN); CRgn rgnRect; // Create rgn rgnRect.CreateRectRgn(rect.left,rect.top,rect.right,rect.bottom); dcMem.SelectClipRgn(&rgnRect); rect = *pRect; // Create a font and set color CFont cFont,*cpOldFont; cFont.CreatePointFont(70,_T("Arial")); cpOldFont = (CFont *)dcMem.SelectObject(&cFont); COLORREF crOldBkColor = dcMem.SetBkColor( m_crDisplayBGColor ); TEXTMETRIC tm; dcMem.GetTextMetrics(&tm); // change color of plotting pen CPen PlottingPen( PS_SOLID, 1, RGB(255,0,0) ); CPen RoiPen( PS_SOLID, 1, RGB(0,0,255) ); dcMem.SelectObject( &PlottingPen ); dcMem.MoveTo(0, rect.bottom - YPlotBuffer[0]); double XScaleFactor; if (XPlotMAX > 0) { XScaleFactor = (double) rect.Width(); XScaleFactor /= (double)(XPlotMAX + 1); } else { XScaleFactor = (double) rect.Width(); } double YScaleFactor; if (YPlotMAX > 0) { YScaleFactor = (double) rect.Height(); YScaleFactor /= (double)(YPlotMAX + 1); } else { YScaleFactor = (double) rect.Height(); } for(int X=0; X<=XPlotMAX; X++) { //// ////if (X == 400) { //// isPlotPen = FALSE; //// dcMem.SelectObject( &RoiPen ); ////} ////if (X == 439) { //// isPlotPen = TRUE; //// dcMem.SelectObject( &PlottingPen ); ////} if (OptPlotType) { dcMem.LineTo((int)(X * XScaleFactor), (int)(rect.bottom - (YPlotBuffer[X]* YScaleFactor))); dcMem.LineTo((int)((X + 1) * XScaleFactor), (int)(rect.bottom - (YPlotBuffer[X + 1]* YScaleFactor))); } else { dcMem.LineTo((int)(X * XScaleFactor), (int)(rect.bottom - (YPlotBuffer[X]* YScaleFactor))); dcMem.LineTo((int)((X + 1) * XScaleFactor), (int)(rect.bottom - (YPlotBuffer[X + 1]* YScaleFactor))); dcMem.MoveTo((int)(X * XScaleFactor),rect.bottom); dcMem.LineTo((int)(X * XScaleFactor), (int)(rect.bottom - (YPlotBuffer[X]* YScaleFactor))); dcMem.MoveTo((int)((X + 1) * XScaleFactor), rect.bottom); dcMem.LineTo((int)((X + 1) * XScaleFactor), (int)(rect.bottom - (YPlotBuffer[X + 1]* YScaleFactor))); } } CPen MarkerPen( PS_SOLID, 1, RGB(0,0,255) ); dcMem.SelectObject( &MarkerPen ); DrawCursor( &dcMem, YPlotBuffer, false); dcMem.SelectObject( &MarkerPen ); // // finally draw to screen from temperary dc // pDC->BitBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,SRCCOPY); /////////////////////////////////////////////////////////////////////////// pDC->SelectClipRgn(NULL); rgnRect.DeleteObject(); dcMem.SelectObject(pOldBrush); WorkSpaceBrush.DeleteObject(); dcMem.SetBkColor( crOldBkColor ); dcMem.SelectObject(pbmOld); bmTemp.DeleteObject(); dcMem.SelectObject(cpOldFont); cFont.DeleteObject(); dcMem.SelectObject(pOldPen); dcMem.SelectObject(cpOldFont); /////////////////////////////////////////////////////////////////////////// if(pDCa == NULL) ReleaseDC(pDC); } void CPlotCanvas::DrawCursor(CDC *dcMem, long YPlotBufferIn[8192], bool EraseCursor = false) { CPen TriggerFGPen( PS_SOLID, 1, m_crCursorColor ); CPen TriggerBGPen( PS_SOLID, 1, m_crDisplayBGColor ); dcMem->SelectObject( &TriggerFGPen ); CRect rect; GetClientRect(&rect); dcMem->MoveTo(CursorPosition, 0); dcMem->LineTo(CursorPosition, rect.Height()); } /////////////////////////////////////////////////////////////////////////////// // Name: PlotTrace // Description: Plots trace from dpp scope data. /////////////////////////////////////////////////////////////////////////////// void CPlotCanvas::PlotTrace(long YPlotBufferIn[8192]) { CRect rect; GetClientRect(&rect); for(int i=0; i<8192;i++) { YPlotBuffer[i] = YPlotBufferIn[i]; } DrawPlotCanvas(&rect); } /////////////////////////////////////////////////////////////////////////////// // Name: PlotTrace // Description: Plots trace from dpp scope data. /////////////////////////////////////////////////////////////////////////////// void CPlotCanvas::ClearTrace() { CRect rect; GetClientRect(&rect); hasBackground = false; // redraw background for(int i=0; i<8192;i++) { YPlotBuffer[i] = 0; } DrawPlotCanvas(&rect); } /////////////////////////////////////////////////////////////////////////////// // Name: PlotTrace // Description: Plots trace from dpp scope data. /////////////////////////////////////////////////////////////////////////////// void CPlotCanvas::DrawCursor() { CRect rect; GetClientRect(&rect); hasBackground = false; // redraw background //for(int i=0; i<8192;i++) { // YPlotBuffer[i] = 0; //} DrawPlotCanvas(&rect); } /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// void CPlotCanvas::SetPersistenceMode(bool Persistence) { PersistenceMode = Persistence; OnRefreshBkgnd(); } /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// void CPlotCanvas::SetCursorPosition(int CursorPos) { CursorPosition = CursorPos; OnRefreshBkgnd(); } void CPlotCanvas::OnLButtonDown(UINT nFlags, CPoint point) { //AfxMessageBox("CPlotCanvas::OnLButtonDown"); SetCursorPosition(point.x); CRect rect; GetClientRect(&rect); DrawPlotCanvas(&rect); CStatic::OnLButtonDown(nFlags, point); } void CPlotCanvas::DataAcq(long DataBuffer[8192], int NumChan) { int i; long YMAX; double y1; XPlotMAX = NumChan - 1; if (NumChan > 0) { //plot data YMAX = 0; for(i = 0; i < NumChan; i++) { //get the largest value to be displayed if (OptScaleType) { if (log(1.0 + DataBuffer[i]) > YMAX) { YMAX = (long)(log(1.0 + DataBuffer[i])); } } else { if (DataBuffer[i] > YMAX) { YMAX = DataBuffer[i]; } } } if (YMAX < 1) { ClearTrace(); return; } //if no y scale exit sub if (OptScaleType) { //adjust plot to include entire high peak YMAX = (YMAX * 10) + 10; } else { if (YMAX < 20) { YMAX = YMAX + 2; } else { YMAX = YMAX + 10; } } for (i = 0; i < NumChan;i++ ) { //get the largest value to be displayed if (OptScaleType) { y1 = log(1.0 + DataBuffer[i]) * 10; } else { y1 = DataBuffer[i]; } YPlotBuffer[i] = (long)(y1); } YPlotMAX = YMAX; CRect rect; GetClientRect(&rect); hasBackground = false; // redraw background DrawPlotCanvas(&rect); } }