?? test2view.cpp
字號:
// test2View.cpp : implementation of the CTest2View class
//
#include "stdafx.h"
#include "test2.h"
#include "test2Doc.h"
#include "test2View.h"
#include <math.h>
#define PI 3.1415926
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
float a = 300;
float xCircle, yCircle, xPoint, yPoint, w;
POINT polypts[250];
int nCounts = 0;
bool pause = false;
// CTest2View
IMPLEMENT_DYNCREATE(CTest2View, CView)
BEGIN_MESSAGE_MAP(CTest2View, CView)
//{{AFX_MSG_MAP(CTest2View)
ON_WM_KEYDOWN()
ON_WM_TIMER()
ON_WM_CREATE()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CTest2View construction/destruction
CTest2View::CTest2View()
{
// TODO: add construction code here
}
CTest2View::~CTest2View()
{
}
BOOL CTest2View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CTest2View drawing
void CTest2View::OnDraw(CDC* pDC)
{
CTest2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CRect cRect; //客戶區矩形
CPen *cOldPen,
cRedPen1(PS_SOLID, 5, RGB(255,0,0)),
cRedPen2(PS_SOLID, 10, RGB(255,0,0)),
cYellowPen(PS_SOLID, 5, RGB(255,255,0)),
cGreenPen(PS_SOLID,5,RGB(0,255,0)),
cBluePen(PS_SOLID, 5, 0x02ff0000);//筆聲明
CBrush *pOldBrush, cNewBrush;//舊畫筆、新畫筆聲明
int OrgDC;
OrgDC = pDC->SaveDC(); //保存原始DC,供以后恢復它時使用
pDC->SetMapMode(MM_ISOTROPIC); //設定"各向同性UCS"映射方式
pDC->SetWindowExt(4000, 3000); //設定窗口范圍
AfxGetMainWnd()->GetClientRect(cRect);//獲取主窗口的客戶區矩形
pDC->SetViewportExt(cRect.Width(), -cRect.Height());//設定視口范圍
pDC->DPtoLP(cRect); //將設備坐標轉換成邏輯坐標
pDC->SetWindowOrg(-(cRect.Width()/2), -(cRect.Height()/2));//設定窗口原點為中心點
xCircle = 2*a*sin(w*PI);
yCircle = 2*a*cos(w*PI); //計算轉動的圓的圓心坐標
yPoint = a*(2*cos(w*PI) - cos(2*w*PI));
xPoint = a*(2*sin(w*PI) - sin(2*w*PI));//計算固定點的當前位置
polypts[nCounts].x = xPoint;
polypts[nCounts].y = yPoint; //將當前位置保存進數組
pDC->SelectObject(&cGreenPen);
pDC->Ellipse(xCircle-a, yCircle+a, xCircle+a, yCircle-a); //畫外面轉動的綠色的圓
pDC->SelectObject(&cRedPen1);
cNewBrush.CreateSolidBrush(RGB(255,0,0));
pOldBrush = pDC->SelectObject(&cNewBrush);
pDC->Ellipse(xPoint-15, yPoint+15, xPoint+15, yPoint-15);
pDC->SelectObject(pOldBrush);
cNewBrush.DeleteObject(); //畫綠色圓上的那個參考紅點
pDC->SelectObject(&cRedPen2);
pDC->Polyline(polypts,nCounts); //畫心形線
pDC->SelectObject(&cRedPen1);
pDC->Ellipse(-a,a,a,-a); //畫坐標原點的固定不動的紅圓
pDC->SelectObject(&cYellowPen);
pDC->MoveTo(0,0);
pDC->LineTo(xCircle, yCircle); //畫兩個大圓之間的連心線
pDC->SelectObject(&cBluePen);
pDC->MoveTo(0,0);
pDC->LineTo(0,(cRect.Height()/2));
pDC->MoveTo(0,0);
pDC->LineTo(0,-(cRect.Height()/2));
pDC->MoveTo(0,0);
pDC->LineTo((cRect.Width()/2),0);
pDC->MoveTo(0,0);
pDC->LineTo(-(cRect.Width()/2),0); //畫坐標軸
DrawText(pDC);
pDC->RestoreDC(OrgDC);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CTest2View printing
BOOL CTest2View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CTest2View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CTest2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CTest2View diagnostics
#ifdef _DEBUG
void CTest2View::AssertValid() const
{
CView::AssertValid();
}
void CTest2View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTest2Doc* CTest2View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTest2Doc)));
return (CTest2Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTest2View message handlers
void CTest2View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
int aOld = a;
int i;
switch( nChar )
{
case ' ':
if(pause == false)
pause = true;
else
pause = false;
Invalidate();
break;
case VK_UP:
if ( a >= 430 )
a = 430;
else
a += 10;
for(i=0; i<=nCounts; i++)
{
polypts[i].x = polypts[i].x*a/aOld;
polypts[i].y = polypts[i].y*a/aOld;
}
Invalidate();
break;
case VK_DOWN:
if ( a <= 150 )
a = 150;
else
a -= 10;
for(i=0; i<=nCounts; i++)
{
polypts[i].x = polypts[i].x*a/aOld;
polypts[i].y = polypts[i].y*a/aOld;
}
Invalidate();
break;
default:break;
}
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CTest2View::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
int i;
if(!pause)
{
w += 0.01;
nCounts++;
if( w >= 2 )
{
w = 0;
nCounts = 0;
for( i=0; i<=250; i++)
{
polypts[i].x = 0;
polypts[i].y = 0;
}
}
}
Invalidate();
CView::OnTimer(nIDEvent);
}
int CTest2View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
SetTimer(1,100,NULL); //時間間隔為0.1秒
return 0;
}
void CTest2View::DrawText(CDC *pDC)
{
CFont *pOldFont,cNewFont;
//cNewFont.CreateFont(20,15,450,450,FW_BOLD,FALSE,FALSE,FALSE,OEM_CHARSET,
//OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,VARIABLE_PITCH|FF_ROMAN,"Roman");
cNewFont.CreateStockObject(SYSTEM_FIXED_FONT);
pOldFont=pDC->SelectObject(&cNewFont);
pDC->SetTextColor(0x0000);
pDC->TextOut(-2100, 1400,"周曉波 空間中心 200728007329058");
pDC->TextOut(-350, 1400,"心形線動態生成演示");
pDC->TextOut(-2100, 1200,"控制鍵:");
pDC->TextOut(-2100, 1000,"UP: 放大");
pDC->TextOut(-2100, 800,"DOWN: 縮小");
pDC->TextOut(-2100, 600,"SPACE: 暫停/繼續");
if(a == 430)
{
pDC->SetTextColor(0x00ff);
pDC->TextOut(-2100, 400,"尺寸已為最大,無法再增大!");
}
if(a == 150)
{
pDC->SetTextColor(0x00ff);
pDC->TextOut(-2100, 400,"尺寸已為最小,無法再縮小!");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -