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

📄 curvedlg.cpp

📁 正弦余弦波形发生器
💻 CPP
字号:
// CurveDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Curve.h"
#include "CurveDlg.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define PI 3.14

/////////////////////////////////////////////////////////////////////////////
// CCurveDlg dialog

CCurveDlg::CCurveDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCurveDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCurveDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCurveDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCurveDlg)
	DDX_Control(pDX, IDC_STAIMG, m_StaImg);
	DDX_Control(pDX, IDC_COMCURVE, m_ComCurve);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCurveDlg, CDialog)
	//{{AFX_MSG_MAP(CCurveDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTDRAW, OnButdraw)
	ON_BN_CLICKED(IDC_BUTEXIT, OnButexit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCurveDlg message handlers

BOOL CCurveDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	this->ShowWindow(SW_SHOWMAXIMIZED);
	m_ComCurve.SetCurSel(0);
	this->SetDlgItemText(IDC_EDTSWING,"1");
	this->SetDlgItemText(IDC_EDTMODULUS,"1");
	this->SetDlgItemText(IDC_EDTCURVE,"0");
	CRect rcWnd,rcClient;
	m_StaImg.GetWindowRect(&rcWnd);
	m_StaImg.GetClientRect(&rcClient);

	pDC=m_StaImg.GetDC();
	//建立画刷
	CBrush brush;
	brush.CreateSolidBrush(RGB(255,255,204));
	pDC->FillRect(&rcClient,&brush);
	//建立画笔
	CPen cpen,*pPen;
	cpen.CreatePen(PS_SOLID,2,RGB(0,0,255));
	pPen=pDC->SelectObject(&cpen);
	const int nOrgX=100;
	const int nOrgY=245;
	const int nWidth=650;
	const int nHeight=180;
	const int nPI=60;
	//指定原点
	pDC->SetViewportOrg(nOrgX,nOrgY);	
	pDC->SetTextColor(RGB(255,0,0));
	//绘制横坐标
	int n=-1,nTmp=0;
	CString sPIText[]={"-1/2π","","1/2π","π","3/2π","2π","5/2π","3π","7/2π","4π","9/2π"};
	while(true)
	{
		pDC->LineTo(nPI*n,0);
		pDC->LineTo(nPI*n,6);
		pDC->MoveTo(nPI*n,0);
		pDC->SetBkMode(TRANSPARENT);
		pDC->TextOut(nPI*n-sPIText[n+1].GetLength()*3,16,sPIText[n+1]);
		n++;
		nTmp=nTmp+nPI;
		if(nTmp>=nWidth)
		{
			pDC->LineTo(nPI*n,0);
			break;
		}
	}
	pDC->MoveTo(0,0);
	n=-4;
	nTmp=0;
	CString sTmp;
	//绘制纵坐标
	while(true)
	{
		pDC->LineTo(0,nPI*n);
		pDC->LineTo(6,nPI*n);
		pDC->MoveTo(0,nPI*n);
		pDC->SetBkMode(TRANSPARENT);
		sTmp.Format("%d",n);
		pDC->TextOut(10,nPI*n,sTmp);
		n++;
		nTmp=nPI*n;
		if(nTmp>nHeight)
		{
			pDC->LineTo(0,nPI*n);
			break;
		}
	}
	brush.DeleteObject();
	cpen.DeleteObject();
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCurveDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCurveDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
void CCurveDlg::OnButdraw() 
{
	const int nWidth=600;
	const int nHeight=180;
	const int nPI=60;  // 1/2π的宽度即曲线宽度
	CString A,w,B;
	CString sStyle;
	this->m_ComCurve.GetWindowText(sStyle);
	float y;
	this->GetDlgItemText(IDC_EDTSWING,A);
	this->GetDlgItemText(IDC_EDTMODULUS,w);
	this->GetDlgItemText(IDC_EDTCURVE,B);
	COLORREF color;
	color=RGB(0,0,0);
	CPen cpen,*pPen;
	cpen.CreatePen(PS_SOLID,3,color);	
	pPen=pDC->SelectObject(&cpen);
	float radian ;

	for(int x=-60;x<nWidth;x++)
	{
		//弧度=X坐标/曲线宽度*角系数*π+相位角*π
		//Y坐标=振幅*曲线宽度*sin(弧度)
		radian =((float)x/((float)nPI*2))*1/atof(w)*PI+atof(B)*PI;
		if(sStyle=="正弦曲线")
			y=sin(radian);
		else
			y=cos(radian);
		y=y*atof(A)*nPI;
		
		pDC->MoveTo(x,y);
		pDC->LineTo(x,y);

	}
}

void CCurveDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	//CDialog::OnOK();
}

void CCurveDlg::OnButexit() 
{
	this->OnCancel();
}

⌨️ 快捷键说明

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