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

📄 magicl~3.cpp

📁 visual c++ 时尚编程百例 全部源代码
💻 CPP
字号:
// MagicLineView.cpp : implementation of the CMagicLineView class
//

#include "stdafx.h"
#include "MagicLine.h"

#include "MagicLineDoc.h"
#include "MagicLineView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMagicLineView

IMPLEMENT_DYNCREATE(CMagicLineView, CView)

BEGIN_MESSAGE_MAP(CMagicLineView, CView)
	//{{AFX_MSG_MAP(CMagicLineView)
	ON_COMMAND(ID_STOP, OnStop)
	ON_COMMAND(ID_DRAW, OnDrawLine)
	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()

/////////////////////////////////////////////////////////////////////////////
// CMagicLineView construction/destruction

CMagicLineView::CMagicLineView()
{
	begin[0].x=100;
	begin[0].y=100;
	begin[1].x=350;
	begin[1].y=400;
	rise[0].x=3;
	rise[0].y=5;
	rise[1].x=-5;
	rise[1].y=-3;
	// TODO: add construction code here

}

CMagicLineView::~CMagicLineView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMagicLineView drawing

void CMagicLineView::OnDraw(CDC* pDC)
{
	CMagicLineDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CMagicLineView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMagicLineView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMagicLineView message handlers

void CMagicLineView::OnStop() 
{
	KillTimer(1);
	// TODO: Add your command handler code here
	
}

void CMagicLineView::OnDrawLine() 
{
	SetTimer(1,100,NULL);
	// TODO: Add your command handler code here
	
}

void CMagicLineView::OnTimer(UINT nIDEvent) 
{
CClientDC dc(this);
DrawLine(&dc);
	
	CView::OnTimer(nIDEvent);
}
UINT CMagicLineView::LineRand(UINT max)
{
	int nRand=rand();
	float fLine=(float)max/RAND_MAX;
	float fVal=(float)fLine*nRand+0.5F;
	return (UINT)fVal;
}
COLORREF CMagicLineView::SetColor(int x,int y)
{
	long red,blue,green;
	int px,py;
	CRect rect;
	GetClientRect(&rect);
	px=rect.Width();
	py=rect.Height();
	red=y*255/py;
	blue=((py-y)*255/py+x*255/px)/2;
	green=((py-y)*255/py+(px-x)*255/px)/2;
	return RGB(red+LineRand(2),green+LineRand(2),blue+LineRand(2));
}
void CMagicLineView::DrawLine(CClientDC*pDC)
{
	CPen pen;
	pen.CreatePen(PS_SOLID,1,SetColor(begin[1].x,begin[1].y));
	CPen*pOldpen=pDC->SelectObject(&pen);
	pDC->MoveTo(begin[0].x,begin[0].y);
	pDC->LineTo(begin[1].x,begin[1].y);
	for(int i=0;i<2;i++)
	{
		begin[i].x=begin[i].x+rise[i].x;
		begin[i].y=begin[i].y+rise[i].y;
		if(begin[i].x<=5||begin[i].x>=630)rise[i].x=-rise[i].x;
		if(begin[i].y<=5||begin[i].y>=490)rise[i].y=-rise[i].y;
	}
	pDC->SelectObject(pOldpen);
}
		

⌨️ 快捷键说明

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