📄 diamondview.cpp
字号:
// DiamondView.cpp : implementation of the CDiamondView class
//
#include "stdafx.h"
#include "Diamond.h"
#include "DiamondDoc.h"
#include "DiamondView.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDiamondView
IMPLEMENT_DYNCREATE(CDiamondView, CView)
BEGIN_MESSAGE_MAP(CDiamondView, CView)
//{{AFX_MSG_MAP(CDiamondView)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_TIMER()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CDiamondView construction/destruction
CDiamondView::CDiamondView()
{
// TODO: add construction code here
}
CDiamondView::~CDiamondView()
{
}
BOOL CDiamondView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CDiamondView drawing
void CDiamondView::OnDraw(CDC* pDC)
{
CDiamondDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
// RotatePoints();
CPen pen,*pOldPen;
pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
pOldPen=pDC->SelectObject(&pen);
CRect rect;
this->GetClientRect(rect);
int CenterX=rect.Width()/2;
int CenterY=rect.Height()/2;
int radius=min(CenterX,CenterY);
int i,j;
for(i=0;i<MaxPoints;i++)
{
for(j=i+1;j<MaxPoints;j++)
{
int x=radius+Points[i].x*radius;
int y=radius+Points[i].y*radius;
pDC->MoveTo(x,y);
int m=radius+Points[j].x*radius;
int n=radius+Points[j].y*radius;
pDC->LineTo(m,n);
}
}
pDC->SelectObject(pOldPen);
}
/////////////////////////////////////////////////////////////////////////////
// CDiamondView printing
BOOL CDiamondView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDiamondView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CDiamondView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CDiamondView diagnostics
#ifdef _DEBUG
void CDiamondView::AssertValid() const
{
CView::AssertValid();
}
void CDiamondView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CDiamondDoc* CDiamondView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDiamondDoc)));
return (CDiamondDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDiamondView message handlers
void CDiamondView::RotatePoints()
{
const double StepAngle=2*PI/MaxPoints;//角点之间的圆心角
rotation+=PI/32;//设定每次旋转的角度
if(rotation>StepAngle)
rotation-=StepAngle;//保持每次旋转角度小于两点之间的圆心角
int i;//i表示角度的次序
double j;//j表示旋转后每个角点的角度
for(i=0,j=rotation;i<MaxPoints;i++,j+=StepAngle)
{
Points[i].x=cos(j);
Points[i].y=sin(j);
}
}
int CDiamondView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
SetTimer(1,50,NULL);
CDC *pDC=GetDC();
CPen pen;
pen.CreatePen(PS_SOLID,3,RGB(0,255,255));
pDC->SelectObject(&pen);
rotation=0;
// TODO: Add your specialized creation code here
return 0;
}
void CDiamondView::OnDestroy()
{
KillTimer(1);
CView::OnDestroy();
// TODO: Add your message handler code here
}
void CDiamondView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
RotatePoints();
Invalidate();
CView::OnTimer(nIDEvent);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -