VC6新建一个单文档工程;
先添加一个小球类;
头文件和cpp文件如下;
#if !defined(AFX_SPHERE_H__835B2B85_5B12_4409_AEC0_9C5062625DDE__INCLUDED_)
#define AFX_SPHERE_H__835B2B85_5B12_4409_AEC0_9C5062625DDE__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CSphere
{
public:
CSphere(void);
virtual ~CSphere();
void SetParameter(int R,CPoint CenterPoint);
void Draw(CDC* pDC);
public:
int R;
CPoint CenterPoint;
};
#endif // !defined(AFX_SPHERE_H__835B2B85_5B12_4409_AEC0_9C5062625DDE__INCLUDED_)
#include "stdafx.h"
#include "dbf.h"
#include "Sphere.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//
// Construction/Destruction
//
CSphere::CSphere()
{
}
CSphere::~CSphere()
{
}
void CSphere::SetParameter(int R, CPoint CenterPoint) {
this->R = R;
this->CenterPoint = CenterPoint;
}
void CSphere::Draw(CDC* pDC) {
CPoint TopLet(CenterPoint.x - R, CenterPoint.y - R);
CPoint BottomRight(CenterPoint.x + R, CenterPoint.y + R);
CRect Square(TopLet, BottomRight);
pDC->Ellipse(Square);
}
在视类的头文件包含 #include "Sphere.h",
为视类添加如下成员变量,
protected:
int nWidth, nHeight;
CSphere sphere;
CPoint direction;
BOOL bPlay;
在视类的构造函数中初始化,
CDbfView::CDbfView()
{
// TODO: add construction code here
bPlay = TRUE;
sphere.R = GetSystemMetrics(SM_CXFULLSCREEN) / 20;
sphere.CenterPoint.x = 200, sphere.CenterPoint.y = 200;
direction.x = 1, direction.y = 1;
}
在类信息窗口右击视类名称,添加成员函数,
如下添加一个成员函数,
函数体如下,
void CDbfView::DrawObject(CDC *pDC)
{
sphere.Draw(pDC);
}
添加定时器;从类向导,类名定位到视类,Messages中选中WM_TIMER,
点击 Add Function 添加一个函数,再点击 Edit Code,编辑定时器执行函数如下,
void CDbfView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
sphere.CenterPoint += direction;
Invalidate(FALSE);
CView::OnTimer(nIDEvent);
}
再为视类添加如下一个成员函数,
void CDbfView::CollisionDetection()
{
if (sphere.CenterPoint.x - sphere.R < 0)
direction.x = 1;
if (sphere.CenterPoint.x + sphere.R > nWidth)
direction.x = -1;
if (sphere.CenterPoint.y - sphere.R < 0)
direction.y = 1;
if (sphere.CenterPoint.y + sphere.R > nHeight)
direction.y = -1;
}
再为视类添加如下一个成员函数,
void CDbfView::DoubleBuffer(CDC *pDC)
{
CRect rect;
GetClientRect(&rect);
nWidth = rect.Width(), nHeight = rect.Height();
CDC memDC;
memDC.CreateCompatibleDC(pDC);
CBitmap NewBitmap, *poldBitmap;
NewBitmap.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
memDC.FillSolidRect(rect, RGB(0, 0, 0));
poldBitmap = memDC.SelectObject(&NewBitmap);
DrawObject(&memDC);
CollisionDetection();
pDC->BitBlt(0, 0, nWidth, nHeight, &memDC, 0, 0, SRCCOPY);
memDC.SelectObject(poldBitmap);
NewBitmap.DeleteObject();
memDC.DeleteDC();
}
然后再OnDraw()函数中调用DoubleBuffer()函数,
void CDbfView::OnDraw(CDC* pDC)
{
CDbfDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
DoubleBuffer(pDC);
}
这个双缓冲绘图程序是来自一些教程,去掉它的界面部分,只演示动画小球;bPlay是控制是否开始动画,直接设为TRUE,一直动画;如果不使用双缓冲,动画的小球会闪烁,使用了就不会;
看一下到此启动程序;
只是画了小球,没有动画;
因为定时器还没启动;
在视类的构造函数或者PreCreateWindow成员函数中增加 SetTimer(1, 10, 0);这句是启动定时器的;定时器也没启动;
那么MFC视类不能在以上地方启动定时器;添加一个菜单,从菜单单击函数启动定时器,
void CDbfView::OnMenuitem32771()
{
// TODO: Add your command handler code here
SetTimer(1, 10, 0);
}
运行程序,单击菜单,然后小球开始不闪烁运动;由于加了CollisionDetection函数,碰到边会弹回;