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

📄 starzview.cpp

📁 在mdi client窗口的客户区域显示移动的星星的程序
💻 CPP
字号:
// StarzView.cpp : implementation of the CStarzView class
//

#include "StdAfx.h"
#include "Starz.h"
#include "StarzDoc.h"
#include "StarzView.h"
#include "Star.h"
#include "StarDensityDialog.h"
#include "StarVelocityDialog.h"

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

static const unsigned int STAR_TIMER = 1;
static const unsigned int INITIAL_DENSITY = 500;
static const unsigned int INITIAL_VELOCITY = 200;

/////////////////////////////////////////////////////////////////////////////
// CStarzView

IMPLEMENT_DYNCREATE(CStarzView, CView)

BEGIN_MESSAGE_MAP(CStarzView, CView)
	//{{AFX_MSG_MAP(CStarzView)
	ON_WM_TIMER()
	ON_WM_ERASEBKGND()
	ON_COMMAND(ID_STARS_REVERSE, OnStarsReverse)
	ON_UPDATE_COMMAND_UI(ID_STARS_REVERSE, OnUpdateStarsReverse)
	ON_COMMAND(ID_STARS_COLOR_NONE, OnStarsColorNone)
	ON_UPDATE_COMMAND_UI(ID_STARS_COLOR_NONE, OnUpdateStarsColorNone)
	ON_COMMAND(ID_STARS_COLOR_RED, OnStarsColorRed)
	ON_UPDATE_COMMAND_UI(ID_STARS_COLOR_RED, OnUpdateStarsColorRed)
	ON_COMMAND(ID_STARS_COLOR_GREEN, OnStarsColorGreen)
	ON_UPDATE_COMMAND_UI(ID_STARS_COLOR_GREEN, OnUpdateStarsColorGreen)
	ON_COMMAND(ID_STARS_COLOR_BLUE, OnStarsColorBlue)
	ON_UPDATE_COMMAND_UI(ID_STARS_COLOR_BLUE, OnUpdateStarsColorBlue)
	ON_COMMAND(ID_STARS_DENSITY, OnStarsDensity)
	ON_COMMAND(ID_STARS_PAUSE, OnStarsPause)
	ON_UPDATE_COMMAND_UI(ID_STARS_PAUSE, OnUpdateStarsPause)
	ON_COMMAND(ID_STARS_VELOCITY, OnStarsVelocity)
	ON_COMMAND(ID_STARS_SMOOTH, OnStarsSmooth)
	ON_UPDATE_COMMAND_UI(ID_STARS_SMOOTH, OnUpdateStarsSmooth)
	ON_WM_LBUTTONDBLCLK()
	ON_WM_RBUTTONDBLCLK()
	ON_COMMAND(ID_STARS_VERTICAL, OnStarsVertical)
	ON_UPDATE_COMMAND_UI(ID_STARS_VERTICAL, OnUpdateStarsVertical)
	ON_COMMAND(ID_STARS_RESTART_HORIZONTAL, OnStarsRestartHorizontal)
	ON_COMMAND(ID_STARS_RESTART_VERTICAL, OnStarsRestartVertical)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStarzView construction/destruction

CStarzView::CStarzView() : density(INITIAL_DENSITY), velocity(INITIAL_VELOCITY), pause(false)
{
	// create an array of stars
	this->stars = new CStar[this->density];
}

CStarzView::~CStarzView()
{
	// clean up the stars
	delete [] this->stars;
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CStarzView drawing

void CStarzView::OnDraw(CDC* pDC)
{
	CStarzDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	CRect rect;
	this->GetClientRect(rect);

	// allow each existing star an update
	// this will get called when invalidate is called from ontimer
	for (unsigned int i = 0; i < this->density; i++)
	{
		this->stars[i].update(pDC, rect);
	}
}

/////////////////////////////////////////////////////////////////////////////
// CStarzView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CStarzView message handlers

void CStarzView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();

	this->SetTimer(STAR_TIMER, this->velocity, 0);
}

void CStarzView::OnTimer(UINT nIDEvent) 
{
	if (nIDEvent == STAR_TIMER)
	{
		if (!this->pause)
		{
			// this will cause OnDraw to be called every
			// time a STAR_TIMER happens
			this->Invalidate(FALSE);
		}
	}
	
	CView::OnTimer(nIDEvent);
}

BOOL CStarzView::OnEraseBkgnd(CDC* pDC) 
{
	// this is just to make the background black

	CBrush backgroundBrush(RGB(0, 0, 0));
	CBrush* pOldBrush = pDC->SelectObject(&backgroundBrush);
	
	CRect rect;
	pDC->GetClipBox(&rect);
	
	pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
	
	pDC->SelectObject(pOldBrush);

	return TRUE;
}

void CStarzView::OnStarsReverse() 
{
	this->stars[0].setReverse(!this->stars[0].getReverse());
	
	this->Invalidate(TRUE);
}

void CStarzView::OnUpdateStarsReverse(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(this->stars[0].getReverse());
}

void CStarzView::OnStarsColorNone() 
{
	if (this->stars[0].getRed() || this->stars[0].getGreen() || this->stars[0].getBlue())
	{
		this->stars[0].setRed(false);
		this->stars[0].setGreen(false);
		this->stars[0].setBlue(false);
		
		this->Invalidate(TRUE);
	}
}

void CStarzView::OnUpdateStarsColorNone(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(!this->stars[0].getRed() && !this->stars[0].getGreen() && !this->stars[0].getBlue());
}

void CStarzView::OnStarsColorRed() 
{
	if (!this->stars[0].getRed())
	{
		this->stars[0].setRed(true);
		this->stars[0].setGreen(false);
		this->stars[0].setBlue(false);
		
		this->Invalidate(TRUE);
	}
	else
	{
		this->OnStarsColorNone();
	}
}

void CStarzView::OnUpdateStarsColorRed(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(this->stars[0].getRed() && !this->stars[0].getGreen() && !this->stars[0].getBlue());
}

void CStarzView::OnStarsColorGreen() 
{
	if (!this->stars[0].getGreen())
	{
		this->stars[0].setRed(false);
		this->stars[0].setGreen(true);
		this->stars[0].setBlue(false);
		
		this->Invalidate(TRUE);
	}
	else
	{
		this->OnStarsColorNone();
	}
}

void CStarzView::OnUpdateStarsColorGreen(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(!this->stars[0].getRed() && this->stars[0].getGreen() && !this->stars[0].getBlue());
}

void CStarzView::OnStarsColorBlue() 
{
	if (!this->stars[0].getBlue())
	{
		this->stars[0].setRed(false);
		this->stars[0].setGreen(false);
		this->stars[0].setBlue(true);

		this->Invalidate(TRUE);
	}
	else
	{
		this->OnStarsColorNone();
	}
}

void CStarzView::OnUpdateStarsColorBlue(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(!this->stars[0].getRed() && !this->stars[0].getGreen() && this->stars[0].getBlue());
}

void CStarzView::OnStarsDensity() 
{
	CStarDensityDialog starDensityDialog(this);
	if (starDensityDialog.DoModal() == IDOK)
	{
		this->density = starDensityDialog.density;
		
		delete [] this->stars;
		this->stars = new CStar[this->density];
	}

	this->Invalidate(TRUE);
}

void CStarzView::OnStarsPause() 
{
	this->pause = !this->pause;

	this->Invalidate(TRUE);
}

void CStarzView::OnUpdateStarsPause(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(this->pause);
}

void CStarzView::OnStarsVelocity() 
{
	CStarVelocityDialog starVelocityDialog(this);
	if (starVelocityDialog.DoModal() == IDOK)
	{
		this->velocity = starVelocityDialog.velocity;

		this->SetTimer(STAR_TIMER, this->velocity, 0);
	}

	this->Invalidate(TRUE);
}

void CStarzView::OnStarsSmooth() 
{
	// reset timing for optimum performance depending on which mode we are in
	// the user can change this from the velocity menu
	if (this->stars[0].getSmooth())
	{
		this->stars[0].setSmooth(false);
		this->velocity = 200;
		this->SetTimer(STAR_TIMER, this->velocity, 0);
	}
	else
	{
		this->stars[0].setSmooth(true);
		this->velocity = 50;
		this->SetTimer(STAR_TIMER, this->velocity, 0);
	}

	this->Invalidate(TRUE);
}

void CStarzView::OnUpdateStarsSmooth(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(this->stars[0].getSmooth());
}

void CStarzView::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	this->OnStarsPause();
	
	CView::OnLButtonDblClk(nFlags, point);
}

void CStarzView::OnRButtonDblClk(UINT nFlags, CPoint point) 
{
	this->OnStarsReverse();
	
	CView::OnRButtonDblClk(nFlags, point);
}

void CStarzView::OnStarsVertical() 
{
	this->stars[0].setVertical(!this->stars[0].getVertical());

	this->Invalidate(TRUE);
}

void CStarzView::OnUpdateStarsVertical(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(this->stars[0].getVertical());
}

void CStarzView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	if (lHint == HINT_CLOSE_DOCUMENT)
	{
		this->stars[0].reset();
	}
}

void CStarzView::OnStarsRestartHorizontal() 
{
	for (unsigned int i = 0; i < this->density; i++)
	{
		this->stars[i].restartHorizontal();
	}

	this->Invalidate(TRUE);
}

void CStarzView::OnStarsRestartVertical() 
{
	for (unsigned int i = 0; i < this->density; i++)
	{
		this->stars[i].restartVertical();
	}

	this->Invalidate(TRUE);
}

⌨️ 快捷键说明

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