⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 quxian.cpp

📁 凌阳单片机的上位机程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:

  }

  //创建一些字体(水平和垂直的)
  //
  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") ;
  
  //夺取水平字体
  oldFont = m_dcGrid.SelectObject(&axisFont) ;
  
  //设置Y轴最大值
  m_dcGrid.SetTextColor (m_crGridColor) ;
  m_dcGrid.SetTextAlign (TA_RIGHT|TA_TOP) ;
  strTemp.Format ("%.*lf", m_nYDecimals, m_dUpperLimit) ;
  m_dcGrid.TextOut (m_rectPlot.left, m_rectPlot.top-3, strTemp) ;

  // 设置y轴最小值
  m_dcGrid.SetTextAlign (TA_RIGHT|TA_BASELINE) ;
  strTemp.Format ("%.*lf", m_nYDecimals, m_dLowerLimit) ;
  m_dcGrid.TextOut (m_rectPlot.left, m_rectPlot.bottom+3, strTemp) ;

  //以下分别设置y轴坐标
  m_dcGrid.SetTextAlign (TA_RIGHT|TA_BASELINE) ;
  m_dcGrid.TextOut (m_rectPlot.left,nBottomGridPix2+3, "25") ;

  m_dcGrid.SetTextAlign (TA_RIGHT|TA_BASELINE) ;
  m_dcGrid.TextOut (m_rectPlot.left,nBottomGridPix+3, "50") ;
  

  m_dcGrid.SetTextAlign (TA_RIGHT|TA_BASELINE) ;
  m_dcGrid.TextOut (m_rectPlot.left,nBottomGridPix1+3, "75") ;

  m_dcGrid.SetTextAlign (TA_RIGHT|TA_BASELINE) ;
  m_dcGrid.TextOut (m_rectPlot.left,nMidGridPix+3, "100") ;

  m_dcGrid.SetTextAlign (TA_RIGHT|TA_BASELINE) ;
  m_dcGrid.TextOut (m_rectPlot.left,nTopGridPix1+3, "125") ;


  m_dcGrid.SetTextAlign (TA_RIGHT|TA_BASELINE) ;
  m_dcGrid.TextOut (m_rectPlot.left,nTopGridPix+3, "150") ;
  

  m_dcGrid.SetTextAlign (TA_RIGHT|TA_BASELINE) ;
  m_dcGrid.TextOut (m_rectPlot.left,nTopGridPix2+3, "175") ;

  // x min
  m_dcGrid.SetTextAlign (TA_LEFT|TA_TOP) ;
  m_dcGrid.TextOut (m_rectPlot.left, m_rectPlot.bottom+4, "") ;

  // x max
  m_dcGrid.SetTextAlign (TA_RIGHT|TA_TOP) ;
  strTemp.Format ("%d", m_nPlotWidth/m_nShiftPixels) ; 
  m_dcGrid.TextOut (m_rectPlot.right, m_rectPlot.bottom+4,"") ;

  // 设置x轴标题
  m_dcGrid.SetTextAlign (TA_CENTER|TA_TOP) ;
  m_dcGrid.TextOut ((m_rectPlot.left+m_rectPlot.right)/2, 
                    m_rectPlot.bottom+4, m_strXUnitsString) ;

  // 重建字体
  m_dcGrid.SelectObject(oldFont) ;

  //设置 y轴标题 
  oldFont = m_dcGrid.SelectObject(&yUnitFont) ;
  m_dcGrid.SetTextAlign (TA_CENTER|TA_BASELINE) ;
  m_dcGrid.TextOut ((m_rectClient.left+m_rectPlot.left)/2-5, 
                    (m_rectPlot.bottom+m_rectPlot.top)/2, m_strYUnitsString) ;
  m_dcGrid.SelectObject(oldFont) ;


  // 如果没有,创建内存设备
  if (m_dcPlot.GetSafeHdc() == NULL)
  {
    m_dcPlot.CreateCompatibleDC(&dc) ;
    m_bitmapPlot.CreateCompatibleBitmap(&dc, m_nClientWidth, m_nClientHeight) ;
    m_pbitmapOldPlot = m_dcPlot.SelectObject(&m_bitmapPlot) ;
  }

  // 确定位图被清除
  m_dcPlot.SetBkColor (m_crBackColor) ;
  m_dcPlot.FillRect(m_rectClient, &m_brushBack) ;

  // 刷新屏幕
  InvalidateRect(m_rectClient) ;

} 

/////////////////////////////////////////////////////////
///////////////////////为图置新的点///////////////////////
//////////////////////////////////////////////////////////
double Cquxian::AppendPoint(double dNewPoint)
{
  double dPrevious ; 
  dPrevious = m_dCurrentPosition ;
  m_dCurrentPosition = dNewPoint ;
  DrawPoint() ;     //添加新的数据点
  Invalidate() ;
  return dPrevious ;//返回先前一个点
} 
 
////////////////////////////////////////////////////
/////////////////////画图////////////////////////////
/////////////////////////////////////////////////////
void Cquxian::OnPaint() 
{
  CPaintDC dc(this) ;  // 设置画图前后关系
  CDC memDC ;
  CBitmap memBitmap ;
  CBitmap* oldBitmap ; // 在CMemDC创建最初的位图

 
  memDC.CreateCompatibleDC(&dc) ;
  memBitmap.CreateCompatibleBitmap(&dc, m_nClientWidth, m_nClientHeight) ;
  oldBitmap = (CBitmap *)memDC.SelectObject(&memBitmap) ;

  if (memDC.GetSafeHdc() != NULL)
  {
   
	  //首先停止在内存设备的格子
    memDC.BitBlt(0, 0, m_nClientWidth, m_nClientHeight, 
                 &m_dcGrid, 0, 0, SRCCOPY) ;
  
	//在暗背景加入亮的图
    memDC.BitBlt(0, 0, m_nClientWidth, m_nClientHeight, 
                 &m_dcPlot, 0, 0, SRCPAINT) ; 
   
	//显示
    dc.BitBlt(0, 0, m_nClientWidth, m_nClientHeight, 
              &memDC, 0, 0, SRCCOPY) ;
  }

  memDC.SelectObject(oldBitmap) ;

} 
///////////////////////////////////////////////
///////////////////画曲线//////////////////////
///////////////////////////////////////////////
void Cquxian::DrawPoint()
{  
  int currX, prevX, currY, prevY ;
  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_nShiftPixels, m_rectPlot.top+1, 
                    SRCCOPY) ;
	

    
	//在右边建立一个矩形区域以便加入新的数据点
    rectCleanUp = m_rectPlot ;
    rectCleanUp.left  = rectCleanUp.right - m_nShiftPixels ;

    
	//在新的区域填充背景
    m_dcPlot.FillRect(rectCleanUp, &m_brushBack) ;


    // 夺取画笔
    oldPen = m_dcPlot.SelectObject(&m_penPlot) ;

    // 移动到先前的一个点
    prevX = m_rectPlot.right-m_nPlotShiftPixels ;
    prevY = m_rectPlot.bottom - 
            (long)((m_dPreviousPosition - m_dLowerLimit) * m_dVerticalFactor) ;
    m_dcPlot.MoveTo (prevX, prevY) ;

    // 在先前的点和现在的点之间连线
    currX = m_rectPlot.right-m_nHalfShiftPixels ;	
    currY = m_rectPlot.bottom -
            (long)((m_dCurrentPosition - m_dLowerLimit) * m_dVerticalFactor) ;
    m_dcPlot.LineTo (currX, currY) ;

    // 交还画笔 
    m_dcPlot.SelectObject(oldPen) ;

	//如果数据超出范围,以背景填充
    if ((prevY <= m_rectPlot.top) || (currY <= m_rectPlot.top))
      m_dcPlot.FillRect(CRect(prevX, m_rectClient.top, currX+1, m_rectPlot.top+1), &m_brushBack) ;
    if ((prevY >= m_rectPlot.bottom) || (currY >= m_rectPlot.bottom))
      m_dcPlot.FillRect(CRect(prevX, m_rectPlot.bottom+1, currX+1, m_rectClient.bottom+1), &m_brushBack) ;

    // 存储当前的点以便下一次画图
    m_dPreviousPosition = m_dCurrentPosition ;

  }

} 


///////////////////////////////////////////////////////////
////////////////////设置画图区域大小///////////////////////
///////////////////////////////////////////////////////////
void Cquxian::OnSize(UINT nType, int cx, int cy) 
{
  CWnd::OnSize(nType, cx, cy) ;

  // 在创建的时候自动调整大小
  
  GetClientRect(m_rectClient) ;

  
  m_nClientHeight = m_rectClient.Height() ;
  m_nClientWidth  = m_rectClient.Width() ;


  m_rectPlot.left   = 20 ;  
  m_rectPlot.top    = 10 ;
  m_rectPlot.right  = m_rectClient.right-10 ;
  m_rectPlot.bottom = m_rectClient.bottom-25 ;

  
  m_nPlotHeight = m_rectPlot.Height() ;
  m_nPlotWidth  = m_rectPlot.Width() ;

  m_dVerticalFactor = (double)m_nPlotHeight / m_dRange ; 

} 

/////////////////////////////////////////
/////////////////重置////////////////////
//////////////////////////////////////////
void Cquxian::Reset()
{
  InvalidateCtrl() ;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -