📄 quxian.cpp
字号:
// quxian.cpp : implementation file//
#include "stdafx.h"
#include "math.h"
#include "quxian.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__ ;
#endif
/////////////////////////////////////////////////////////////////////////////
// Cquxian
Cquxian::Cquxian()
{
// 画曲线是利用lineto函数连起每一个点
m_dPreviousPosition = 0.0 ;//出发点(先前的点)
//公共变量设置y轴小数位数
m_nYDecimals = 3 ;
//设置一些坐标初始值
m_dLowerLimit = -10.0 ;//y轴最下端
m_dUpperLimit = 10.0 ;//y轴最上端
m_dRange = m_dUpperLimit - m_dLowerLimit ; // y轴总长度
///m_nShiftPixels确定图移动的大小
m_nShiftPixels = 4 ;//每次移动四个象素
m_nHalfShiftPixels = m_nShiftPixels/2 ; // 一半移动范围
m_nPlotShiftPixels = m_nShiftPixels + m_nHalfShiftPixels ; // 3/2移动范围
m_crBackColor = RGB( 0, 0, 0) ; // 背景颜色
m_crGridColor = RGB( 0, 255, 255) ; // 网格颜色
m_crPlotColor = RGB(255, 255, 255) ; // 曲线颜色
m_penPlot.CreatePen(PS_SOLID, 0, m_crPlotColor) ;//创建画笔
m_brushBack.CreateSolidBrush(m_crBackColor) ;//创建画刷
m_strXUnitsString.Format("Samples") ; // 初始化x轴注释
m_strYUnitsString.Format("Y units") ; // 初始化y轴注释
//位图恢复
m_pbitmapOldGrid = NULL ;
m_pbitmapOldPlot = NULL ;
}
/////////////////////////////////////////////////////////////////////////////
Cquxian::~Cquxian()
{
if (m_pbitmapOldGrid != NULL)
m_dcGrid.SelectObject(m_pbitmapOldGrid) ;
if (m_pbitmapOldPlot != NULL)
m_dcPlot.SelectObject(m_pbitmapOldPlot) ;
}
BEGIN_MESSAGE_MAP(Cquxian, CWnd)
//{{AFX_MSG_MAP(Cquxian)
ON_WM_PAINT()
ON_WM_SIZE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Cquxian message handlers
///////////////////////////////////////////////////////////////
////////////////////////// 创建画图工具////////////////////////
///////////////////////////////////////////////////////////////
BOOL Cquxian::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 ;
}
/////////////////////////////////////////////////////////////
/////////////////////设置y轴坐标以及小数位数/////////////////
/////////////////////////////////////////////////////////////
void Cquxian::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 ; //y轴每隔的数值
InvalidateCtrl() ;//清除原先的旧图,用新图代替
}
/////////////////////////////////////////////////////
//////////////////////设置x轴标题////////////////////
//////////////////////////////////////////////////////
void Cquxian::SetXUnits(CString string)
{
m_strXUnitsString = string ;
InvalidateCtrl() ;//清除原先的旧图,用新图代替
}
/////////////////////////////////////////////////////
//////////////////////设置y轴标题////////////////////
//////////////////////////////////////////////////////
void Cquxian::SetYUnits(CString string)
{
m_strYUnitsString = string ;
InvalidateCtrl() ;//清除原先的旧图,用新图代替
}
///////////////////////////////////////////////////
//////////////////设置网格颜色/////////////////////
//////////////////////////////////////////////////
void Cquxian::SetGridColor(COLORREF color)
{
m_crGridColor = color ;//网格颜色
InvalidateCtrl() ;//清除原先的旧图,用新图代替
}
///////////////////////////////////////////////////
//////////////////设置曲线颜色/////////////////////
//////////////////////////////////////////////////
void Cquxian::SetPlotColor(COLORREF color)
{
m_crPlotColor = color ;
m_penPlot.DeleteObject() ;
m_penPlot.CreatePen(PS_SOLID, 0, m_crPlotColor) ;
InvalidateCtrl() ;//清除原先的旧图,用新图代替
}
///////////////////////////////////////////////////
//////////////////设置背景颜色/////////////////////
//////////////////////////////////////////////////
void Cquxian::SetBackgroundColor(COLORREF color)
{
m_crBackColor = color ;
m_brushBack.DeleteObject() ;
m_brushBack.CreateSolidBrush(m_crBackColor) ;
InvalidateCtrl() ;
}
////////////////////////////////////////////////////////
//////////////////清除原先的旧图,用新图代替////////////
////////////////////////////////////////////////////////
void Cquxian::InvalidateCtrl()
{
int i ;
int nCharacters ;
int nTopGridPix, nMidGridPix, nBottomGridPix,nTopGridPix1, nBottomGridPix1,nTopGridPix2,
nBottomGridPix2;
CPen *oldPen ;
CPen solidPen(PS_SOLID, 0, m_crGridColor) ;
CFont axisFont, yUnitFont, *oldFont ;
CString strTemp ;
// 创建画图设备
CClientDC dc(this) ;
//如果没有,就创建
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) ;//
// 填充格子背景
m_dcGrid.FillRect(m_rectClient, &m_brushBack) ;
// 画出矩形图
//确定y轴缩放比例
nCharacters = abs((int)log10(fabs(m_dUpperLimit))) ;
nCharacters = max(nCharacters, abs((int)log10(fabs(m_dLowerLimit)))) ;
//加入数字单位,小数点和一个额外的地方来存放
//
nCharacters = nCharacters + 4 + m_nYDecimals ;
//调整图形尺寸
m_rectPlot.left = m_rectClient.left + 6*(nCharacters) ;
m_nPlotWidth = m_rectPlot.Width() ;
//画出图形区域
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) ;
//画出点线
nMidGridPix = (m_rectPlot.top + m_rectPlot.bottom)/2 ;
nTopGridPix = nMidGridPix - m_nPlotHeight/4 ;
nTopGridPix1 = nMidGridPix - m_nPlotHeight/8 ;
nTopGridPix2 = nMidGridPix - 3*(m_nPlotHeight/8) ;
nBottomGridPix = nMidGridPix + m_nPlotHeight/4 ;
nBottomGridPix1 = nMidGridPix + m_nPlotHeight/8 ;
nBottomGridPix2 = nMidGridPix + 3*(m_nPlotHeight/8);
for (i=m_rectPlot.left; i<m_rectPlot.right; i+=4)
{
m_dcGrid.SetPixel (i, nTopGridPix, m_crGridColor) ;
m_dcGrid.SetPixel (i, nTopGridPix1, m_crGridColor) ;
m_dcGrid.SetPixel (i, nTopGridPix2, m_crGridColor) ;
m_dcGrid.SetPixel (i, nMidGridPix, m_crGridColor) ;
m_dcGrid.SetPixel (i, nBottomGridPix, m_crGridColor) ;
m_dcGrid.SetPixel (i, nBottomGridPix1, m_crGridColor) ;
m_dcGrid.SetPixel (i, nBottomGridPix2, m_crGridColor) ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -