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

📄 oscilloscopedlg.cpp

📁 模拟了现实中的示波器
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	m_ctrlCurveFrame.SetCurveColor(m_Color, m_nCurChannel);
	//波形数据
	Signal[m_nCurChannel].SignalType = m_btnSignalType.GetCurSel();
	Signal[m_nCurChannel].Amplitude	 = m_btnAmplitude.GetData();
	Signal[m_nCurChannel].Frequency  = m_btnFreqSignal.GetData();
	Signal[m_nCurChannel].Offset     = m_btnOffset.GetData();
	Signal[m_nCurChannel].Phase      = m_btnPhase.GetData();

	//取得当前通道号,并显示当前通道的数据
	m_nCurChannel = m_btnChannel.GetCurSel();
	//波形显示数据
	UINT width;
	m_ctrlCurveFrame.GetCurveWidth(width, m_nCurChannel);
	m_btnWidth.SetData(double(width));

	m_ctrlCurveFrame.GetCurveColor(m_Color, m_nCurChannel);
	//波形数据
	m_btnChannel.SetCurSel(m_nCurChannel);
	m_btnSignalType.SetCurSel(Signal[m_nCurChannel].SignalType);

	m_btnAmplitude.SetData(Signal[m_nCurChannel].Amplitude);
	m_btnFreqSignal.SetData(Signal[m_nCurChannel].Frequency);
	m_btnOffset.SetData(Signal[m_nCurChannel].Offset);
	m_btnPhase.SetData(Signal[m_nCurChannel].Phase);

	UpdateData(FALSE);
	SendMessage(WM_PAINT);
}

/////////////////////////////////////////////////////////////////////////////
/*
函数介绍:	Sine波形产生函数,通过输入的设定参数值,输出离散的一维Sine序列值
输入参数:	int firstPos,		信号的第一个采样点位置
			int n,				本次采样点的总数
			double amp,			信号的幅值
			double freqSignal,	信号的频率,以周期/点数的形式给出
			double freqSample,	采样频率
			double phase,		信号的初始相位
			double data[],		信号的离散序列值,一维数组
输出参数:	data[]				信号的离散序列值
返回值  :	无
*/
/////////////////////////////////////////////////////////////////////////////
void CreateWave(UINT signaltype, 
				int firstPos,
				int n,
				double amp,
				double freqSignal,
				double freqSample,
				double phase,
				double offset,
				double data[])
{
	switch(signaltype)
	{
	case 0:
		SineWave(firstPos,
				n,
				amp,
				freqSignal,
				freqSample,
				phase,
				offset,
				data);
		break;
	case 1:
		SquareWave(firstPos,
				n,
				amp,
				freqSignal,
				freqSample,
				phase,
				offset,
				data);
		break;
	case 2:
		TriangleWave(firstPos,
				n,
				amp,
				freqSignal,
				freqSample,
				phase,
				offset,
				data);
		break;
	case 3:
		SawtoothWave(firstPos,
				n,
				amp,
				freqSignal,
				freqSample,
				phase,
				offset,
				data);
		break;
	case 4:
		RandWave(firstPos,
				n,
				amp,
				freqSignal,
				freqSample,
				phase,
				offset,
				data);
		break;
	default:
		break;
	}
}
void SineWave(int firstPos,
			  int n,
			  double amp,
			  double freqSignal,
			  double freqSample,
			  double phase,
			  double offset,
			  double data[])
{
	double tmp;
	tmp = freqSample / freqSignal;//可能就是时基的概念?
	double degree;

	int times = firstPos + n;
	for (int i=0; i<n; i++)
	{
		degree = phase + 360.0/tmp*(i+firstPos);//当前点的角度
		degree *= 0.0174532925;//把角度转换为幅度3.14/180 = 0.0174532925
		data[i] = (sin(degree) * amp) + offset;
	}
}
void RandWave(int firstPos,
			  int n,
			  double amp,
			  double freqSignal,
			  double freqSample,
			  double phase,
			  double offset,
			  double data[])
{
	double tmp;
	tmp = freqSample / freqSignal;//可能就是时基的概念?
	double degree;
//	srand((unsigned int)1);
	int times = firstPos + n;
	for (int i=0; i<n; i++)
	{
		degree = phase + 360.0/tmp*(i+firstPos);//当前点的角度
		degree *= 0.0174532925;//把角度转换为幅度3.14/180 = 0.0174532925
		data[i] = 2 * ((rand()/(double)RAND_MAX)-0.5) * amp + offset;
	}
}
void SquareWave(int firstPos,
				int n,
				double amp,
				double freqSignal,
				double freqSample,
				double phase,
				double offset,
				double data[])
{
	double tmp;
	tmp = freqSample / freqSignal;//可能就是时基的概念?
	double degree;

	int times = firstPos + n;
	for (int i=0; i<n; i++)
	{
		degree = phase + 360.0/tmp*(i+firstPos);//当前点的角度
		while(degree >=360)
			 degree -= 360.0;

		if(degree >= 0 && degree < 180)
			data[i] = (1.0 * amp) + offset;
		else if(degree >= 180 && degree < 360)
			data[i] = (-1.0 * amp) + offset;
	}
}
void TriangleWave(int firstPos,
				  int n,
				  double amp,
				  double freqSignal,
				  double freqSample,
				  double phase,
				  double offset,
				  double data[])
{
	double tmp;
	tmp = freqSample / freqSignal;//可能就是时基的概念?
	double degree;

	int times = firstPos + n;
	for (int i=0; i<n; i++)
	{
		degree = phase + 360.0/tmp*(i+firstPos);//当前点的角度
		while(degree >=360)
			 degree -= 360.0;
		
		if(degree >= 0 && degree < 90)
			data[i] = ((degree/90.0) * amp) + offset;//x/90
		else if(degree >= 90 && degree < 270)
			data[i] = ((2-degree/90.0) * amp) + offset;//2-x/90
		else if(degree >=270 && degree <360)
			data[i] = ((-4+degree/90)*amp)+offset;//-4+x/90
	}
}
void SawtoothWave(int firstPos,
				  int n,
				  double amp,
				  double freqSignal,
				  double freqSample,
				  double phase,
				  double offset,
				  double data[])
{
	double tmp;
	tmp = freqSample / freqSignal;//可能就是时基的概念?
	double degree;

	int times = firstPos + n;
	for (int i=0; i<n; i++)
	{
		degree = phase + 360.0/tmp*(i+firstPos);//当前点的角度
		while(degree >=360)
			 degree -= 360.0;
		
		data[i] = ((1.0 - degree/180.0) * amp) + offset;
	}
}
void COscilloscopeDlg::OnNcPaint()
{
	int i;
	//取得整个窗口的设备上下文,包含客户区,非客户区等
	CRect rcWindow;
	CDC *pDC = GetWindowDC();
	GetWindowRect(&rcWindow);

	CRect rcTitleBar;//hold the rect of titlebar 
	rcTitleBar.left   = GetSystemMetrics(SM_CXFRAME) - GetSystemMetrics(SM_CXBORDER);
	rcTitleBar.top	  = GetSystemMetrics(SM_CYFRAME) - GetSystemMetrics(SM_CYBORDER);
	rcTitleBar.right  = rcWindow.Width() - rcTitleBar.left;
	rcTitleBar.bottom = rcTitleBar.top + GetSystemMetrics( SM_CYSIZE ) + 1;

	CBrush brs;
	brs.CreateSolidBrush(RGB(1, 64, 100));
	pDC->FillRect(rcTitleBar, &brs);

	CPen penTitle[3];
	CPen *pPenOld;
	penTitle[0].CreatePen(PS_SOLID, 1, RGB(4,  72, 111));
	penTitle[1].CreatePen(PS_SOLID, 1, RGB(25, 101, 123));
	penTitle[2].CreatePen(PS_SOLID, 1, RGB(0,  68, 90));
	
	pPenOld = pDC->SelectObject(&penTitle[0]);
	for(i = 0; i < 3; i++)
	{//titlebar 的底线
		pDC->SelectObject(&penTitle[i]);
		pDC->MoveTo(rcTitleBar.left , rcTitleBar.bottom - 2 -i);
		pDC->LineTo(rcTitleBar.right , rcTitleBar.bottom - 2 - i);
	}

	CPen pen[5];
	pen[0].CreatePen(PS_SOLID, 1, RGB(28,91,121));
	pen[1].CreatePen(PS_SOLID, 1, RGB(7,79,98));
	pen[2].CreatePen(PS_SOLID, 1, RGB(00,65,84));
	pen[3].CreatePen(PS_SOLID, 1, RGB(0,42,64));
	pen[4].CreatePen(PS_SOLID, 1, RGB(0,49,82));
	//绘制上边线
	for(i = 0; i < 5; i++)
	{
		pDC->SelectObject(&pen[i]);
		pDC->MoveTo(i,i);
		pDC->LineTo(rcWindow.Width() - i,i);
	}
	for(i = 0; i < 3; i++)
	{
		//绘制左边线
		pDC->SelectObject(&pen[i+1]);
		pDC->MoveTo(i,i);
		pDC->LineTo(i,rcWindow.Height()-i);

		//绘制右边线
		pDC->SelectObject(&pen[3-i]);
		pDC->MoveTo(rcWindow.Width()-1 - i,0);
		pDC->LineTo(rcWindow.Width()-1 - i,rcWindow.Height());

		//绘制底边线
		pDC->SelectObject(&pen[2-i]);
		pDC->MoveTo(0,rcWindow.Height()- 1 - i);
		pDC->LineTo(rcWindow.Width() - i,rcWindow.Height()-1-i);
	}

	pDC->SelectObject(pPenOld);

	//绘制Caption
	CString s;
	GetWindowText(s);
	//确定文本绘制位置
	CSize szExtend = pDC->GetTextExtent(s);
	CPoint pt(rcTitleBar.left, rcTitleBar.CenterPoint().y-szExtend.cy/2);
	int nMode = pDC->SetBkMode(TRANSPARENT);
	pDC->SetTextColor(RGB(255,255,255));
	pDC->TextOut(pt.x, pt.y, s);
	pDC->SetBkMode(nMode);

	ReleaseDC(pDC);
}

void COscilloscopeDlg::DrawClient(CWnd* pWnd)
{
	CDC *pDC = pWnd->GetDC();

	CRect rcClient;
	pWnd->GetClientRect(&rcClient);
	CPen penClient;
	CPen *pOldPenClient;
	CBrush brushClient;
	CBrush *pOldBrushClient;

	penClient.CreatePen(PS_SOLID, 1, RGB(78,63,42));
	pOldPenClient = pDC->SelectObject(&penClient);
	brushClient.CreateSolidBrush(RGB(78,63,42));
	pOldBrushClient = pDC->SelectObject(&brushClient);
	pDC->Rectangle(&rcClient);

	pDC->SelectObject(pOldPenClient);
	pDC->SelectObject(pOldBrushClient);

	CBitmap bmp;
	CBitmap *pOldBmp;
	CDC memDC;
	CPoint pt[4];
	pt[0].x = rcClient.left;
	pt[0].y = rcClient.top;
	pt[1].x = rcClient.left;
	pt[1].y = rcClient.top + rcClient.Height() - 25;
	pt[2].x = rcClient.left + rcClient.Width() - 25;
	pt[2].y = rcClient.top + rcClient.Height() - 25;
	pt[3].x = rcClient.left + rcClient.Width() - 25;
	pt[3].y = rcClient.top;

	memDC.CreateCompatibleDC(pDC);
	for (int i=0; i<4; i++)
	{
		if (i==0)
			bmp.LoadBitmap(IDB_CONER_LEFTTOP + i);
		else
		{
			bmp.DeleteObject();
			bmp.LoadBitmap(IDB_CONER_LEFTTOP + i);
		}
		pOldBmp = memDC.SelectObject(&bmp);
		pDC->BitBlt(pt[i].x, pt[i].y,
					25,25,
					&memDC,
					0,0,SRCCOPY);
	}
	pWnd->ReleaseDC(pDC);
}

⌨️ 快捷键说明

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