📄 graphctrl.cpp
字号:
// x units
//m_dcGrid.SetTextAlign (TA_CENTER|TA_TOP) ;
m_dcGrid.ExtTextOut (((m_rectPlot.left+m_rectPlot.right)/2)-30,
m_rectPlot.bottom+12, ETO_CLIPPED,NULL,m_strXUnitsString,m_strXUnitsString.GetLength() ,NULL) ;
// restore the font
m_dcGrid.SelectObject(oldFont) ;
// y units
oldFont = m_dcGrid.SelectObject(&yUnitFont) ;
m_dcGrid.ExtTextOut (((m_rectClient.left+m_rectPlot.left)/2)-20,
((m_rectPlot.bottom+m_rectPlot.top)/2)+38, ETO_CLIPPED,NULL,m_strYUnitsString,m_strYUnitsString.GetLength() ,NULL) ;
m_dcGrid.SelectObject(oldFont) ;
// at this point we are done filling the the grid bitmap,
// no more drawing to this bitmap is needed until the setting are changed
// if we don't have one yet, set up a memory dc for the plot
if (m_dcPlot.GetSafeHdc() == NULL)
{
m_dcPlot.CreateCompatibleDC(&dc) ;
m_bitmapPlot.CreateCompatibleBitmap(&dc, m_nClientWidth, m_nClientHeight) ;
m_pbitmapOldPlot = m_dcPlot.SelectObject(&m_bitmapPlot) ;
}
// make sure the plot bitmap is cleared
m_dcPlot.SetBkColor (m_crBackColor) ;
m_dcPlot.FillRect(m_rectClient, &m_brushBack) ;
// finally, force the plot area to redraw
InvalidateRect(m_rectClient) ;
}
/************************************************************************************
Function Name : Create
Class Name : MECGraphCtrl
Description : It creates the Graph control
Argument : DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID
Return : BOOL
*************************************************************************************/
BOOL MECGraphCtrl::Create(DWORD dwStyle, const RECT& rect,
CWnd* pParentWnd, UINT nID)
{
BOOL result ;
static CString className = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW) ;
result = CWnd::CreateEx(WS_EX_CLIENTEDGE | WS_EX_STATICEDGE,
className, NULL, dwStyle,
rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
pParentWnd->GetSafeHwnd(), (HMENU)nID) ;
if (result != 0)
InvalidateCtrl() ;
return result ;
} // Create
/************************************************************************************
Function Name : OnSize
Class Name : MECGraphCtrl
Description : Actually it is a message handle, the window size is being initialized from this handler
Argument : UINT nType, int cx, int cy
Return : void
*************************************************************************************/
void MECGraphCtrl::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
GetClientRect(m_rectClient) ;
// set some member variables to avoid multiple function calls
m_nClientHeight = m_rectClient.Height() ;
m_nClientWidth = m_rectClient.Width() ;
// the "left" coordinate and "width" will be modified in
// InvalidateCtrl to be based on the width of the y axis scaling
m_rectPlot.left = 20 ;
m_rectPlot.top = 10 ;
m_rectPlot.right = m_rectClient.right-10 ;
m_rectPlot.bottom = m_rectClient.bottom-25 ;
// set some member variables to avoid multiple function calls
m_nPlotHeight = m_rectPlot.Height() ;
m_nPlotWidth = m_rectPlot.Width() ;
// set the scaling factor for now, this can be adjusted
// in the SetRange functions
m_dVerticalFactor = (double)m_nPlotHeight / m_dRange ;
}
void MECGraphCtrl::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CDC memDC ;
CBitmap memBitmap ;
CBitmap* oldBitmap ; // bitmap originally found in CMemDC
memDC.CreateCompatibleDC(&dc) ;
memBitmap.CreateCompatibleBitmap(&dc, m_nClientWidth, m_nClientHeight) ;
oldBitmap = (CBitmap *)memDC.SelectObject(&memBitmap) ;
if (memDC.GetSafeHdc() != NULL)
{
// first drop the grid on the memory dc
memDC.BitBlt(0, 0, m_nClientWidth, m_nClientHeight,
&m_dcGrid, 0, 0, SRCCOPY) ;
// now add the plot on top as a "pattern" via SRCPAINT.
// works well with dark background and a light plot
memDC.BitBlt(0, 0, m_nClientWidth, m_nClientHeight,
&m_dcPlot, 0, 0, SRCPAINT) ; //SRCPAINT
// finally send the result to the display
dc.BitBlt(0, 0, m_nClientWidth, m_nClientHeight,
&memDC, 0, 0, SRCCOPY) ;
}
memDC.SelectObject(oldBitmap) ;
// Do not call CWnd::OnPaint() for painting messages
}
/************************************************************************************
Function Name : SetRange
Class Name : MECGraphCtrl
Description : It sets the minimum, maximum and decimal ratios to the Y Axis
Argument : double dLower, double dUpper, int nDecimalPlaces
Return : void
*************************************************************************************/
void MECGraphCtrl::SetRange(double dLower, double dUpper, int nDecimalPlaces)
{
ASSERT(dUpper > dLower) ;
m_dLowerLimit = dLower ;
m_dUpperLimit = dUpper ;
m_nYDecimals = nDecimalPlaces ;
m_dRange = m_dUpperLimit - m_dLowerLimit ;
m_dVerticalFactor = (double)m_nPlotHeight / m_dRange ;
// clear out the existing garbage, re-start with a clean plot
InvalidateCtrl() ;
}
/************************************************************************************
Function Name : SetXUnits
Class Name : MECGraphCtrl
Description : It sets the X axis unit as "Time in second"
Argument : CString string
Return : void
*************************************************************************************/
void MECGraphCtrl::SetXUnits(CString string)
{
m_strXUnitsString = string ;
// clear out the existing garbage, re-start with a clean plot
InvalidateCtrl() ;
} // SetXUnits
/************************************************************************************
Function Name : SetYUnits
Class Name : MECGraphCtrl
Description : It sets the Y axis unit as "Volume in ml"
Argument : CString string
Return : void
*************************************************************************************/
void MECGraphCtrl::SetYUnits(CString string)
{
m_strYUnitsString = string ;
// clear out the existing garbage, re-start with a clean plot
InvalidateCtrl() ;
}
/************************************************************************************
Function Name : SetGridColor
Class Name : MECGraphCtrl
Description : It sets the Grid or X and Y axis colors
Argument : COLORREF color
Return : void
*************************************************************************************/
void MECGraphCtrl::SetGridColor(COLORREF color)
{
m_crGridColor = color ;
// clear out the existing garbage, re-start with a clean plot
InvalidateCtrl() ;
} // SetGridColor
/************************************************************************************
Function Name : SetPlotColor
Class Name : MECGraphCtrl
Description : It sets the drawing color of the curve
Argument : COLORREF color
Return : void
*************************************************************************************/
void MECGraphCtrl::SetPlotColor(COLORREF color)
{
m_crPlotColor = color ;
m_penPlot.DeleteObject() ;
m_penPlot.CreatePen(PS_SOLID, 2, m_crPlotColor) ;
// clear out the existing garbage, re-start with a clean plot
InvalidateCtrl() ;
} // SetPlotColor
/************************************************************************************
Function Name : SetBackgroundColor
Class Name : MECGraphCtrl
Description : It sets the background color of the window
Argument : COLORREF color
Return : void
*************************************************************************************/
void MECGraphCtrl::SetBackgroundColor(COLORREF color)
{
m_crBackColor = color ;
m_brushBack.DeleteObject() ;
m_brushBack.CreateSolidBrush(m_crBackColor) ;
// clear out the existing garbage, re-start with a clean plot
InvalidateCtrl() ;
} // SetBackgroundColor
/************************************************************************************
Function Name : SetXRange
Class Name : MECGraphCtrl
Description : It sets the X axis description values
Argument : double dLower, double dUpper, int nDecimalPlaces
Return : void
*************************************************************************************/
void MECGraphCtrl::SetXRange(double dLower, double dUpper, int nDecimalPlaces)
{
ASSERT(dUpper > dLower) ;
m_dXLower = dLower ;
m_dXUpper = dUpper ;
m_nXDecimals = nDecimalPlaces ;
m_XRange = m_dXUpper - m_dXLower ;
m_dHorizontalFactor = (double)m_nPlotWidth / m_XRange ;
// clear out the existing garbage, re-start with a clean plot
InvalidateCtrl() ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -