📄 oscopectrl.cpp
字号:
// OScopeCtrl.cpp : implementation file//
#include "stdafx.h"
#include "math.h"
#include "OScopeCtrl.h"
#include "resource.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__ ;
#endif
/////////////////////////////////////////////////////////////////////////////
// COScopeCtrl
COScopeCtrl::COScopeCtrl()
{
//初始化起始点
m_PrePos=0;
//初始化数据范围
m_XLower=0;
m_XUpper=8000;
m_Y1Lower=0;
m_Y1Upper=150;
m_Y2Lower=-180;
m_Y2Upper=180;
//设置背景颜色,网格颜色和数据颜色
m_crBackColor = RGB( 0, 0, 0);
m_crGridColor = RGB( 0,255,255);
m_crLableColor = RGB(255, 0, 0);
//数据笔,背景刷
m_PenGrid.CreatePen(PS_SOLID,0,m_crGridColor);
m_PenWave1.CreatePen(PS_SOLID,0,RGB(0,255,0));;
m_PenWave2.CreatePen(PS_SOLID,0,RGB(0,0,255));;
//坐标显示
m_Title.Format("Y:电压(V) X:时间(s)");
//用于恢复内存DC的CBitmap指针
m_pbitmapOldGrid = NULL ;
m_pbitmapOldWave = NULL ;
m_pbitmapOldWindow=NULL;
m_bXLable=0;
m_bYLable=0;
m_XPos=0;
m_YPos=0;
m_bCover=TRUE;
m_bIndication=TRUE;
m_bMagnitude=TRUE;
m_bPhase=TRUE;
}
/////////////////////////////////////////////////////////////////////////////
COScopeCtrl::~COScopeCtrl()
{
//恢复内存DC
if (m_pbitmapOldGrid != NULL)
m_dcGrid.SelectObject(m_pbitmapOldGrid) ;
if (m_pbitmapOldWave != NULL)
m_dcWave.SelectObject(m_pbitmapOldWave) ;
if (m_pbitmapOldWindow != NULL)
m_dcWindow.SelectObject(m_pbitmapOldWindow) ;
}
BEGIN_MESSAGE_MAP(COScopeCtrl, CWnd)
//{{AFX_MSG_MAP(COScopeCtrl)
ON_WM_PAINT()
ON_WM_SIZE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// COScopeCtrl message handlers
/////////////////////////////////////////////////////////////////////////////
BOOL COScopeCtrl::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 ;
}
void COScopeCtrl::SetTitle(CString title)
{
m_Title=title;
InvalidateCtrl();
}
/////////////////////////////////////////////////////////////////////////////
//设置网格颜色
void COScopeCtrl::SetGridColor(COLORREF color)
{
m_crGridColor=color;
m_PenGrid.DeleteObject();
m_PenGrid.CreatePen(PS_SOLID,0,m_crGridColor);
InvalidateCtrl() ;
}
/////////////////////////////////////////////////////////////////////////////
//设置背景颜色
void COScopeCtrl::SetBackColor(COLORREF color)
{
m_crBackColor=color;
InvalidateCtrl();
}
void COScopeCtrl::SetRangeX(int nMin,int nMax)
{
if(nMax>nMin)
{
m_XLower=nMin;
m_XUpper=nMax;
InvalidateCtrl();
}
}
void COScopeCtrl::SetRangeYA(int nMin,int nMax)
{
if(nMax>nMin)
{
m_Y1Lower=nMin;
m_Y1Upper=nMax;
InvalidateCtrl();
}
}
void COScopeCtrl::SetRangeYB(int nMin,int nMax)
{
if(nMax>nMin)
{
m_Y2Lower=nMin;
m_Y2Upper=nMax;
InvalidateCtrl();
}
}
/////////////////////////////////////////////////////////////////////////////
void COScopeCtrl::InvalidateCtrl()
{
//刷新控件,注意只刷新一次
int i,j;
CPen *oldPen;
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.FillSolidRect(m_rectClient,m_crBackColor);
m_dcGrid.FillSolidRect(m_rectWave,RGB(0,0,0));
//画波形方框
oldPen = m_dcGrid.SelectObject (&m_PenGrid);
m_dcGrid.MoveTo (m_rectWave.left-1,m_rectWave.top-1);
m_dcGrid.LineTo (m_rectWave.right+1,m_rectWave.top-1);
m_dcGrid.LineTo (m_rectWave.right+1,m_rectWave.bottom+1);
m_dcGrid.LineTo (m_rectWave.left-1,m_rectWave.bottom+1);
m_dcGrid.LineTo (m_rectWave.left-1,m_rectWave.top-1);
m_dcGrid.SelectObject (oldPen);
//画点线
for(i=0;i<7;i++)
for(j=m_rectWave.left;j<m_rectWave.right;j+=2)
{
m_dcGrid.SetPixel (j,m_rectWave.top+i*40,m_crGridColor) ;
}
for(i=0;i<9;i++)
for(j=m_rectWave.top;j<m_rectWave.bottom;j+=2)
{
m_dcGrid.SetPixel(m_rectWave.left+i*40,j,m_crGridColor) ;
}
// 创建字体(水平和垂直)
// height:14 weight:300
axisFont.CreateFont (14,0,0,0,300,
FALSE,FALSE,0,DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH|FF_SWISS, "Arial") ;
yUnitFont.CreateFont (12,0,0,0,300,
FALSE,FALSE,0,DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH|FF_SWISS,"宋体") ;
//字体更换
oldFont = m_dcGrid.SelectObject(&axisFont);
m_dcGrid.SetBkColor (m_crBackColor);
m_dcGrid.SetTextColor (m_crGridColor);
//Y坐标
m_dcGrid.SetTextAlign (TA_RIGHT|TA_BASELINE);
for(i=0;i<=6;i++)
{
strTemp.Format ("%d",m_Y1Upper-(6-i)*(m_Y1Upper-m_Y1Lower)/6);
strTemp+="%";
m_dcGrid.TextOut (m_rectWave.left-4,m_rectWave.bottom-i*(m_rectWave.bottom-m_rectWave.top)/6,strTemp);
}
m_dcGrid.SetTextAlign (TA_LEFT|TA_BASELINE);
for(i=0;i<=6;i++)
{
strTemp.Format("%d°",m_Y2Lower+i*(m_Y2Upper-m_Y2Lower)/6);
m_dcGrid.TextOut (m_rectWave.right+4,m_rectWave.bottom-i*(m_rectWave.bottom-m_rectWave.top)/6,strTemp);
}
//X坐标
m_dcGrid.SetTextAlign (TA_CENTER|TA_TOP);
for(i=0;i<=8;i++)
{
strTemp.Format (" %.2f",i*(m_XUpper-m_XLower)/8000.0+m_XLower);
m_dcGrid.TextOut (m_rectWave.left+i*(m_rectWave.right-m_rectWave.left)/8,
m_rectWave.bottom+4,strTemp);
}
//字体更换
m_dcGrid.SelectObject(oldFont);
yUnitFont.DeleteObject();
if (m_dcWave.GetSafeHdc() == NULL)
{
m_dcWave.CreateCompatibleDC(&dc) ;
m_bitmapWave.CreateCompatibleBitmap(&dc, m_nClientWidth, m_nClientHeight) ;
m_pbitmapOldWave=m_dcWave.SelectObject(&m_bitmapWave) ;
}
//确信数据位图已清除
m_dcWave.FillSolidRect(m_rectClient,RGB(0,0,76));
if(m_XPos<m_rectWave.left) m_XPos=m_rectWave.left;
if(m_XPos>m_rectWave.right) m_XPos=m_rectWave.right;
if(m_YPos<m_rectWave.top) m_YPos=m_rectWave.top;
if(m_YPos>m_rectWave.bottom) m_YPos=m_rectWave.bottom;
//强制刷新波形区域
InvalidateRect(m_rectClient);
DrawPoint();
}
////////////////////////////////////////////////////////////////////////////
void COScopeCtrl::OnPaint()
{
CPaintDC dc(this);
if (m_dcWindow.GetSafeHdc() == NULL)
{
m_dcWindow.CreateCompatibleDC(&dc);
m_bitmapWindow.CreateCompatibleBitmap(&dc, m_nClientWidth,m_nClientHeight);
m_pbitmapOldWindow= m_dcWindow.SelectObject(&m_bitmapWindow);
}
if (m_dcWindow.GetSafeHdc() != NULL)
{
//画网格
m_dcWindow.BitBlt(0,0,m_nClientWidth,m_nClientHeight,
&m_dcGrid, 0, 0, SRCCOPY) ;
//画波形
m_dcWindow.BitBlt(0,0,m_nClientWidth,m_nClientHeight,&m_dcWave,0,0,SRCPAINT);
//m_dcWindow.BitBlt(m_rectWave.left,m_rectWave.top,
// m_rectWave.Width(),m_rectWave.Height(),
// &m_dcWave,m_rectWave.left,m_rectWave.top,SRCPAINT);
if(m_bIndication==TRUE)
{
//画标尺
int i;
//画点线
for(i=m_rectWave.left+1;i<m_rectWave.right;i+=2)
{
m_dcWindow.SetPixel (i,m_YPos,m_crLableColor) ;
}
for(i=m_rectWave.top+1;i<m_rectWave.bottom;i+=2)
{
m_dcWindow.SetPixel (m_XPos,i,m_crLableColor) ;
}
}
//显示坐标数据
CFont yUnitFont,*oldFont;
yUnitFont.CreateFont (12,0,0,0,300,
FALSE,FALSE,0,DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH|FF_SWISS,"宋体") ;
//字体更换
CString strTemp1,strTemp2;
oldFont = m_dcWindow.SelectObject(&yUnitFont);
m_dcWindow.SetTextColor (m_crGridColor);
m_dcWindow.SetBkColor(m_crBackColor);
m_dcWindow.SetTextAlign (TA_LEFT|TA_TOP);
strTemp2.Format("频率:%dHz",(m_XPos-m_rectWave.left)*(m_XUpper-m_XLower)/320+m_XLower);
strTemp1.Format(" 幅度:%.2f",m_Y1Upper-(float)(m_YPos-m_rectWave.top)*(m_Y1Upper-m_Y1Lower)/240);
strTemp2+=strTemp1;
strTemp2+="%";
strTemp1.Format(" 相位:%.2f°",(float)(m_YPos-m_rectWave.bottom)*(m_Y2Lower-m_Y2Upper)/240+m_Y2Lower);
strTemp2+=strTemp1;
m_dcWindow.TextOut(m_rectWave.left,m_rectWave.bottom+20,strTemp2);
m_dcWindow.SelectObject(oldFont);
//将结果送到显示区域
dc.BitBlt(0, 0, m_nClientWidth, m_nClientHeight,
&m_dcWindow, 0, 0, SRCCOPY) ;
}
}
/////////////////////////////////////////////////////////////////////////////
void COScopeCtrl::SetData(int *data1,int *data2)
{
//向波形窗口追加一帧数据
int i;
for(i=0;i<320;i++)
{
m_DataA[i]=data1[i];
m_DataB[i]=data2[i];
}
DrawPoint();
Invalidate();
}
/////////////////////////////////////////////////////////////////////////////
void COScopeCtrl::DrawPoint()
{
int i;
//数据调整
int m,n;
double nr;
int dat1[320],dat2[320];
m=(m_Y1Lower+m_Y1Upper)*120/(m_Y1Lower-m_Y1Upper)+120-1;
n=(m_Y2Lower+m_Y2Upper)*120/(m_Y2Lower-m_Y2Upper)+120-1;
nr=240.0/(m_Y2Upper-m_Y2Lower);
for(i=0;i<320;i++)
{
dat1[i]=m_DataA[i]+m;
if(dat1[i]<0)
dat1[i]=0;
else if(dat1[i]>240)
dat1[i]=240;
dat2[i]=(int)(m_DataB[i]*nr+n);
if(dat2[i]<0)
dat2[i]=0;
else if(dat2[i]>240)
dat2[i]=240;
}
if (m_dcWave.GetSafeHdc() != NULL)
{
// 选择画笔
// 移到当前位置,画数据
CPen *oldPen;
if(m_bCover==TRUE)
m_dcWave.FillSolidRect(m_rectWave,m_crBackColor);
if(m_bMagnitude==TRUE)
{
oldPen=m_dcWave.SelectObject (&m_PenWave1);
m_dcWave.MoveTo(m_rectWave.left,m_rectWave.bottom-dat1[0]-1);
for(i=1;i<320;i++)
{
m_dcWave.LineTo(m_rectWave.left+i,m_rectWave.bottom-dat1[i]-1);
}
m_dcWave.SelectObject (oldPen);
}
if(m_bPhase==TRUE)
{
oldPen=m_dcWave.SelectObject (&m_PenWave2);
m_dcWave.MoveTo(m_rectWave.left,m_rectWave.bottom-dat2[0]-1);
for(i=1;i<320;i++)
{
m_dcWave.LineTo(m_rectWave.left+i,m_rectWave.bottom-dat2[i]-1);
}
m_dcWave.SelectObject (oldPen);
}
}
}
/////////////////////////////////////////////////////////////////////////////
void COScopeCtrl::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_rectWave.top=m_rectClient.top+15;
m_rectWave.bottom=m_rectWave.top+240;
m_rectWave.left=m_rectClient.left+(m_nClientWidth-320)/2;
m_rectWave.right=m_rectWave.left+320;
}
/////////////////////////////////////////////////////////////////////////////
void COScopeCtrl::Reset()
{
InvalidateCtrl() ;
}
void COScopeCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_XPos-5<point.x&&m_XPos+5>point.x)
m_bXLable=1;
else
m_bXLable=0;
if(m_YPos-5<point.y&&m_YPos+5>point.y)
m_bYLable=1;
else
m_bYLable=0;
CWnd::OnLButtonDown(nFlags, point);
}
void COScopeCtrl::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_bXLable=0;
m_bYLable=0;
CWnd::OnLButtonUp(nFlags, point);
}
void COScopeCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
int f=0;
if(m_bXLable==1)
{
m_XPos=point.x-1;
if(m_XPos<m_rectWave.left) m_XPos=m_rectWave.left;
if(m_XPos>=m_rectWave.right)
m_XPos=m_rectWave.right;
f=1;
}
if(m_bYLable==1)
{
m_YPos=point.y-1;
if(m_YPos<m_rectWave.top) m_YPos=m_rectWave.top;
if(m_YPos>=m_rectWave.bottom) m_YPos=m_rectWave.bottom;
f=1;
}
if(f==1) InvalidateRect(m_rectClient);
CWnd::OnMouseMove(nFlags, point);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -