📄 oscopectrl.cpp
字号:
int nLines = m_nRectWidth/nInterimH;
for (i=m_rectPlot.top; i<m_rectPlot.bottom; i+=4)
{
for ( int j = 1; j<=nLines;j++){
m_dcGrid.SetPixel (m_rectPlot.left+nInterimH*j, i,m_crGridColor) ;
}
}
// create some fonts (horizontal and vertical)
// use a height of 14 pixels and 300 weight
// (these may need to be adjusted depending on the display)
axisFont.CreateFont (14, 0, 0, 0, 300,
FALSE, FALSE, 0, ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH|FF_SWISS, "Arial") ;
yUnitFont.CreateFont (14, 0, 900, 0, 300,
FALSE, FALSE, 0, ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH|FF_SWISS, "Arial") ;
// grab the horizontal font
oldFont = m_dcGrid.SelectObject(&axisFont) ;
// y max
//m_dcGrid.SetTextColor (m_crGridColor) ;
m_dcGrid.SetTextAlign (TA_RIGHT|TA_TOP) ;
strTemp.Format ("%.*lf", m_nYDecimals, m_dUpperLimitV) ;
m_dcGrid.TextOut (m_rectPlot.left-4, m_rectPlot.top, strTemp) ;
m_dcGrid.SetTextColor(RGB(255,255,255));
// y min
m_dcGrid.SetTextAlign (TA_RIGHT|TA_BASELINE) ;
strTemp.Format ("%.*lf", m_nYDecimals, m_dLowerLimitV) ;
m_dcGrid.TextOut (m_rectPlot.left-4, m_rectPlot.bottom, strTemp) ;
// x min
m_dcGrid.SetTextAlign (TA_LEFT|TA_TOP) ;
strTemp.Format ("%.*lf", m_nYDecimals, m_dLowerLimitH) ;
m_dcGrid.TextOut (m_rectPlot.left, m_rectPlot.bottom+4, strTemp) ;
// x max
m_dcGrid.SetTextAlign (TA_RIGHT|TA_TOP) ;
strTemp.Format ("%.*lf", 1, m_dUpperLimitH) ;
if(m_nType == IMGDEFINE){
m_dcGrid.TextOut (m_rectPlot.right,
nMidGridPix+4, strTemp) ;
}else{
m_dcGrid.TextOut (m_rectPlot.right,
m_rectPlot.bottom+4, strTemp) ;
}
//m_dcGrid.TextOut (m_rectPlot.right, m_rectPlot.bottom+4, strTemp) ;
// x units
m_dcGrid.SetTextAlign (TA_CENTER|TA_TOP) ;
if(m_nType == IMGDEFAULT){
m_dcGrid.TextOut (m_rectPlot.right-10,
m_rectPlot.bottom-15, m_strXUnitsString) ;
} else {
m_dcGrid.TextOut (m_rectPlot.right-10,
nMidGridPix-16, m_strXUnitsString) ;
}
// restore the font
// m_dcGrid.SelectObject(oldFont) ;
// y units
//oldFont = m_dcGrid.SelectObject(&yUnitFont) ;
m_dcGrid.SetTextAlign (TA_CENTER|TA_TOP) ;
m_dcGrid.TextOut (m_rectPlot.left+18,
m_rectPlot.top-10, m_strYUnitsString) ;
m_dcGrid.SelectObject(oldFont) ;
// title
m_dcGrid.SetTextColor(RGB(255,0,0));
oldFont = m_dcGrid.SelectObject(&yUnitFont) ;
m_dcGrid.SetTextAlign (TA_CENTER|TA_BASELINE) ;
m_dcGrid.TextOut ((m_rectClient.left+m_rectPlot.left)/2,
(m_rectPlot.bottom+m_rectPlot.top)/2, m_sTopic) ;
m_dcGrid.SelectObject(oldFont) ;
m_dcGrid.SetTextColor(RGB(255,255,255));
// 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) ;
} // InvalidateCtrl
/////////////////////////////////////////////////////////////////////////////
double COScopeCtrl::AppendPoint(double dNewPointV,double dNewPointH,int nMode)
{
// append a data point to the plot
// return the previous poin
m_iCLC = nMode ;
m_dCurrentPositionV = dNewPointV ;
m_dCurrentPositionH = dNewPointH ;
DrawPoint() ;
Invalidate() ;
return 0.0 ;
} // AppendPoint
////////////////////////////////////////////////////////////////////////////
void COScopeCtrl::OnPaint()
{
CPaintDC dc(this) ; // device context for painting
CDC memDC ;
CBitmap memBitmap ;
CBitmap* oldBitmap ; // bitmap originally found in CMemDC
// no real plotting work is performed here,
// just putting the existing bitmaps on the client
// to avoid flicker, establish a memory dc, draw to it
// and then BitBlt it to the client
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) ;
} // OnPaint
/////////////////////////////////////////////////////////////////////////////
void COScopeCtrl::DrawPoint()
{
// this does the work of "scrolling" the plot to the left
// and appending a new data point all of the plotting is
// directed to the memory based bitmap associated with m_dcPlot
// the will subsequently be BitBlt'd to the client in OnPaint
int currX, prevX, currY, prevY ;
CPen *oldPen ;
CRect rectCleanUp ;
if (m_dcPlot.GetSafeHdc() != NULL)
{
if(m_iCLC == 0){
m_dPreviousPositionV = 0.0 ;
m_dPreviousPositionH = 0.0 ;
// establish a rectangle over the right side of plot
// which now needs to be cleaned up proir to adding the new point
//rectCleanUp.left = rectCleanUp.right - m_nShiftPixels ;
// 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) ;
// move to the previous point
//m_nPlotShiftPixels =4;//m_rectPlot.Width()/(m_dUpperLimitV - m_dLowerLimitV)*10;
prevX = m_rectPlot.left+
(long)((m_dPreviousPositionV - m_dLowerLimitH) * m_dHorizontalFactor) ;
prevY = m_rectPlot.bottom -
(long)((m_dPreviousPositionH - m_dLowerLimitV) * m_dVerticalFactor) ;
m_dcPlot.MoveTo (prevX, prevY) ;
// draw to the current point
currX = m_rectPlot.left+
(long)((m_dCurrentPositionV - m_dLowerLimitH) * m_dHorizontalFactor) ;
currY = m_rectPlot.bottom -
(long)((m_dCurrentPositionH - m_dLowerLimitV) * m_dVerticalFactor) ;
m_dcPlot.LineTo (currX, currY) ;
// restore the pen
m_dcPlot.SelectObject(oldPen) ;
// store the current point for connection to the next point
m_dPreviousPositionH = m_dCurrentPositionH ;
m_dPreviousPositionV = m_dCurrentPositionV ;
}
} // end DrawPoint
/////////////////////////////////////////////////////////////////////////////
void COScopeCtrl::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy) ;
// NOTE: OnSize automatically gets called during the setup of the control
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 = 10 ;
m_rectPlot.top = 10 ;
m_rectPlot.right = m_rectClient.right-10 ;
m_rectPlot.bottom = m_rectClient.bottom-20 ;
// 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_dRangeV ;
} // OnSize
/////////////////////////////////////////////////////////////////////////////
void COScopeCtrl::Reset()
{
// to clear the existing data (in the form of a bitmap)
// simply invalidate the entire control
InvalidateCtrl() ;
}
void COScopeCtrl::SetGridType(int nType)
{
m_nGridType = nType;
}
void COScopeCtrl::SetRangeH(double hLower, double hUpper)
{
ASSERT(hUpper > hLower) ;
m_dLowerLimitH = hLower ;
m_dUpperLimitH = hUpper ;
//m_nYDecimals = nDecimalPlaces ;
m_dRangeH = m_dUpperLimitH - m_dLowerLimitH ;
m_dHorizontalFactor = (double)m_nPlotWidth / m_dRangeH ;
// clear out the existing garbage, re-start with a clean plot
InvalidateCtrl() ;
}
void COScopeCtrl::SetType(int nType)
{
m_nType = nType ;
}
void COScopeCtrl::CLC()
{
//m_dcPlot.OffsetViewportOrg(1000,1000);
InvalidateCtrl() ;
}
void COScopeCtrl::SetTopic(CString s)
{
m_sTopic = s;
InvalidateCtrl() ;
}
void COScopeCtrl::SetGridN(int pGrid)
{
m_nGridN = pGrid;
InvalidateCtrl() ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -