📄 graphctrl.cpp
字号:
/************************************************************************************
File Name : GraphCtrl.cpp
Class Name : MECGraphCtrl
Description : This file contains the functionalites for the Graph
Developed by : B.Manivannan
Date : 24-10-2001
*************************************************************************************/
#include "stdafx.h"
#include "resource.h"
#include "GraphCtrl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/************************************************************************************
Function Name : MECGraphCtrl
Class Name : MECGraphCtrl
Description : This constructor initialises the attributes
Argument : Nothing
Return type : void
*************************************************************************************/
MECGraphCtrl::MECGraphCtrl()
{
m_nLastIndexPoint = 0;
m_nYDecimals = 0 ;
m_nXDecimals = 0 ;
m_dLowerLimit = 0 ;
m_dUpperLimit = 100 ;
m_dRange = m_dUpperLimit - m_dLowerLimit ; // protected member variable
m_dXLower = 0 ;
m_dXUpper = 100 ;
m_XRange = m_dXUpper - m_dXLower; // protected member variable
// background, grid and data colors
// these are public variables and can be set directly
m_crBackColor = RGB( 0, 0, 0) ; // see also SetBackgroundColor
m_crGridColor = RGB( 0, 255, 255) ; // see also SetGridColor
m_crPlotColor = RGB(255, 255, 255) ; // see also SetPlotColor
// protected variables
m_penPlot.CreatePen(PS_SOLID, 0, m_crPlotColor) ;
m_brushBack.CreateSolidBrush(m_crBackColor) ;
// public member variables, can be set directly
m_strXUnitsString.Format(_T("Time in seconds")) ; // can also be set with SetXUnits
// protected bitmaps to restore the memory DC's
m_pbitmapOldGrid = NULL ;
m_pbitmapOldPlot = NULL ;
}
MECGraphCtrl::~MECGraphCtrl()
{
}
BEGIN_MESSAGE_MAP(MECGraphCtrl, CWnd)
//{{AFX_MSG_MAP(MECGraphCtrl)
ON_WM_SIZE()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/************************************************************************************
Function Name : AppendPoint
Class Name : MECGraphCtrl
Description : It is appending the points to the graph. It is being called from the OnInitDialog
Argument : double dPointX, double dPointY
Return : Nothing
*************************************************************************************/
void MECGraphCtrl::AppendPoint(double dPointX, double dPointY)
{
m_oNewPoint.x = dPointX;
m_oNewPoint.y = dPointY;
m_oPoints[m_nLastIndexPoint].x = m_oNewPoint.x;
m_oPoints[m_nLastIndexPoint].y = m_oNewPoint.y;
DrawPoints();
m_nLastIndexPoint++;
Invalidate();
}
/************************************************************************************
Function Name : DrawPoints
Class Name : MECGraphCtrl
Description : It draws the curve to given points and identify the proper pixels
Argument : Nothing
Return : Nothing
*************************************************************************************/
void MECGraphCtrl::DrawPoints()
{
CPen *oldPen ;
CRect rectCleanUp ;
if (m_dcPlot.GetSafeHdc() != NULL)
{
m_dcPlot.BitBlt(m_rectPlot.left, m_rectPlot.top+1,
m_nPlotWidth, m_nPlotHeight, &m_dcPlot,
m_rectPlot.left, m_rectPlot.top+1,
SRCCOPY) ;
// fill the cleanup area with the background
m_dcPlot.FillRect(rectCleanUp, &m_brushBack) ;
// draw the next line segement
// grab the plotting pen
oldPen = m_dcPlot.SelectObject(&m_penPlot) ;
if(m_nLastIndexPoint == 0)
{
int nXOrigin = m_rectPlot.left ;
int nYOrigin = m_rectPlot.bottom ;
m_dcPlot.MoveTo (nXOrigin, nYOrigin) ;
m_dPoints[m_nLastIndexPoint].x = m_rectPlot.left+(long)((m_oPoints[m_nLastIndexPoint].x - m_dXLower) * m_dHorizontalFactor) ;
m_dPoints[m_nLastIndexPoint].y = m_rectPlot.bottom -
(long)((m_oPoints[m_nLastIndexPoint].y - m_dLowerLimit) * m_dVerticalFactor) ;
m_dcPlot.LineTo ((int)m_dPoints[m_nLastIndexPoint].x, (int)m_dPoints[m_nLastIndexPoint].y) ;
}
else
{
if (m_dPoints[m_nLastIndexPoint].x >= m_rectPlot.left)
m_dcPlot.MoveTo ((int)m_dPoints[m_nLastIndexPoint-1].x, (int)m_dPoints[m_nLastIndexPoint-1].y) ;
m_dPoints[m_nLastIndexPoint].x = m_rectPlot.left+(long)((m_oPoints[m_nLastIndexPoint-1].x - m_dXLower) * m_dHorizontalFactor) ;
m_dPoints[m_nLastIndexPoint].y = m_rectPlot.bottom -
(long)((m_oPoints[m_nLastIndexPoint-1].y - m_dLowerLimit) * m_dVerticalFactor) ;
if (m_dPoints[m_nLastIndexPoint].x >= m_rectPlot.left)
m_dcPlot.LineTo ((int)m_dPoints[m_nLastIndexPoint].x, (int)m_dPoints[m_nLastIndexPoint].y) ;
}
if ((m_dPoints[m_nLastIndexPoint-1].y <= m_rectPlot.top) || (m_dPoints[m_nLastIndexPoint].y <= m_rectPlot.top))
m_dcPlot.FillRect(CRect((int)m_dPoints[m_nLastIndexPoint].x, m_rectClient.top, (int)m_dPoints[m_nLastIndexPoint].x+1, m_rectPlot.top+1), &m_brushBack) ;
if ((m_dPoints[m_nLastIndexPoint-1].y >= m_rectPlot.bottom) || (m_dPoints[m_nLastIndexPoint].y >= m_rectPlot.bottom))
m_dcPlot.FillRect(CRect((int)m_dPoints[m_nLastIndexPoint].x, m_rectPlot.bottom+1, (int)m_dPoints[m_nLastIndexPoint].x+1, m_rectClient.bottom+1), &m_brushBack) ;
// restore the pen
m_dcPlot.SelectObject(oldPen) ;
}
}
/************************************************************************************
Function Name : InvalidateCtrl
Class Name : MECGraphCtrl
Description : Redraws all the points. It can be called from any function
Argument : Nothing
Return : Nothing
*************************************************************************************/
void MECGraphCtrl::InvalidateCtrl()
{
int nCharacters ;
CPen *oldPen ;
CPen solidPen(PS_SOLID, 0, m_crGridColor) ;
CFont axisFont, yUnitFont, *oldFont ;
CString strTemp ;
// in case we haven't established the memory dc's
CClientDC dc(this) ;
// if we don't have one yet, set up a memory dc for the grid
if (m_dcGrid.GetSafeHdc() == NULL)
{
m_dcGrid.CreateCompatibleDC(&dc) ;
m_bitmapGrid.CreateCompatibleBitmap(&dc, m_nClientWidth, m_nClientHeight) ;
m_pbitmapOldGrid = m_dcGrid.SelectObject(&m_bitmapGrid) ;
}
m_dcGrid.SetBkColor (m_crBackColor) ;
// fill the grid background
m_dcGrid.FillRect(m_rectClient, &m_brushBack) ;
// draw the plot rectangle:
// determine how wide the y axis scaling values are
nCharacters = abs((int)log10(fabs(m_dUpperLimit))) ;
nCharacters = max(nCharacters, abs((int)log10(fabs(m_dLowerLimit)))) ;
// add the units digit, decimal point and a minus sign, and an extra space
// as well as the number of decimal places to display
nCharacters = nCharacters + 4 + m_nYDecimals ;
// adjust the plot rectangle dimensions
// assume 6 pixels per character (this may need to be adjusted)
m_rectPlot.left = m_rectClient.left + 6*(nCharacters) ;
m_nPlotWidth = m_rectPlot.Width() ;
// draw the plot rectangle
oldPen = m_dcGrid.SelectObject (&solidPen) ;
m_dcGrid.MoveTo (m_rectPlot.left, m_rectPlot.top) ;
m_dcGrid.LineTo (m_rectPlot.right+1, m_rectPlot.top) ;
m_dcGrid.LineTo (m_rectPlot.right+1, m_rectPlot.bottom+1) ;
m_dcGrid.LineTo (m_rectPlot.left, m_rectPlot.bottom+1) ;
m_dcGrid.LineTo (m_rectPlot.left, m_rectPlot.top) ;
m_dcGrid.SelectObject (oldPen) ;
yUnitFont.CreateFont (14, 0, 900, 0, 300,
FALSE, FALSE, 0, ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH|FF_SWISS, _T("Arial")) ;
axisFont.CreateFont (14, 0, 0, 0, 300,
FALSE, FALSE, 0, ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH|FF_SWISS, _T("Arial")) ;
// grab the horizontal font
oldFont = m_dcGrid.SelectObject(&axisFont) ;
// y max
m_dcGrid.SetTextColor (m_crGridColor) ;
strTemp.Format (_T("%.*lf"), m_nYDecimals, m_dUpperLimit) ;
int nYMaxLength = strTemp.GetLength();
if(m_nYDecimals == 2)
m_dcGrid.ExtTextOut(m_rectPlot.left-30,m_rectPlot.top,ETO_CLIPPED|ETO_OPAQUE,NULL,strTemp,nYMaxLength,NULL);
// y min
strTemp.Format (_T("%.*lf"), m_nYDecimals, m_dLowerLimit) ;
nYMaxLength = strTemp.GetLength();
if(m_nYDecimals == 2)
m_dcGrid.ExtTextOut(m_rectPlot.left-30, m_rectPlot.bottom -10,ETO_CLIPPED|ETO_OPAQUE,NULL,strTemp,nYMaxLength,NULL);
strTemp.Format (_T("%.*lf"), m_nXDecimals, m_dXLower) ;
nYMaxLength = strTemp.GetLength();
m_dcGrid.ExtTextOut(m_rectPlot.left,m_rectPlot.bottom+4,ETO_CLIPPED,NULL,strTemp,nYMaxLength,NULL);
// x max
strTemp.Format (_T("%.*lf"), m_nXDecimals, m_dXUpper) ;
nYMaxLength = strTemp.GetLength();
m_dcGrid.ExtTextOut(m_rectPlot.right-25,m_rectPlot.bottom+4,ETO_CLIPPED,NULL,strTemp,nYMaxLength,NULL);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -