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

📄 shaftencoderview.cpp

📁 电机的测速中需要使用到光电编码盘,如果是自己制作光电编码盘,就要该旋转编码盘打印程序
💻 CPP
字号:
// ShaftEncoderView.cpp : implementation of the CShaftEncoderView class
//

#include "stdafx.h"
#include "ShaftEncoder.h"
#include "math.h"
#include "ShaftEncoderDoc.h"
#include "ShaftEncoderView.h"
#include "SettingsDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define PI 3.1415926535897932384626
#define CM 100
/////////////////////////////////////////////////////////////////////////////
// CShaftEncoderView

IMPLEMENT_DYNCREATE(CShaftEncoderView, CView)

BEGIN_MESSAGE_MAP(CShaftEncoderView, CView)
	//{{AFX_MSG_MAP(CShaftEncoderView)
	ON_COMMAND(IDM_SETTINGS, OnSettings)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CShaftEncoderView construction/destruction

CShaftEncoderView::CShaftEncoderView()
{
	// TODO: add construction code here
	m_dRadius=10;
	m_dCutNum=36;
}

CShaftEncoderView::~CShaftEncoderView()
{
}

BOOL CShaftEncoderView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CShaftEncoderView drawing

void CShaftEncoderView::OnDraw(CDC* pDC)
{
	CShaftEncoderDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	double rdAngle;
	CRect rect;
	CRect rect_round(-100*m_dRadius,-100*m_dRadius,100*m_dRadius,100*m_dRadius);
	GetClientRect(&rect);
	pDC->SetMapMode(MM_LOMETRIC);
	pDC->SetViewportOrg(rect.Width()/2,rect.Height()/2);
	pDC->Ellipse(rect_round);
	CBrush brush(RGB(0,0,0));
	pDC->SelectObject(&brush);
	CPoint ptStart,ptEnd;
	rdAngle=2*PI/m_dCutNum;
	ptStart.x=100*m_dRadius;
	ptStart.y=0;
	for(int i=1;i<=m_dCutNum;i=i+2)
	{
		ptEnd.x=100*m_dRadius*cos(i*rdAngle);
		ptEnd.y=100*m_dRadius*sin(i*rdAngle);
		pDC->Pie(rect_round,ptStart,ptEnd);
		ptStart.x=100*m_dRadius*cos((i+1)*rdAngle);
		ptStart.y=100*m_dRadius*sin((i+1)*rdAngle);
	}	
	
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CShaftEncoderView printing

BOOL CShaftEncoderView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CShaftEncoderView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CShaftEncoderView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CShaftEncoderView diagnostics

#ifdef _DEBUG
void CShaftEncoderView::AssertValid() const
{
	CView::AssertValid();
}

void CShaftEncoderView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CShaftEncoderDoc* CShaftEncoderView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CShaftEncoderDoc)));
	return (CShaftEncoderDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CShaftEncoderView message handlers

void CShaftEncoderView::OnSettings() 
{
	// TODO: Add your command handler code here
	CSettingsDlg dlg;
	dlg.m_dCutNum=m_dCutNum;
	dlg.m_dRadius=m_dRadius;
	if(dlg.DoModal()==IDOK)
	{
		if(dlg.m_dCutNum%2!=0)
		{
			AfxMessageBox("请输入一个整型偶数!");
		}
		else
		{
			m_dCutNum=dlg.m_dCutNum;
			m_dRadius=dlg.m_dRadius;
			this->Invalidate();
		}
	}
	else
		this->Invalidate();

}

⌨️ 快捷键说明

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