📄 程序清单.txt
字号:
////(1)在视图类中定义宏.
#define UPCOLOR RGB(0,0,255) //上升泡泡颜色
#define DOWNCOLOR RGB(255,0,0) //下降泡泡颜色
#define MAX 30 //数组上限
#define ORIGINX 30 //矩形区坐标
#define ORIGINY 50
#define ENDX 630
#define ENDY 430
#define DR 1 //半径增量
#define DS 5 //移动增量
////(2)添加视图类的成员变量.
class CBubbleView:public CView
{
//此处略去若干行由系统生成的代码
private:
CRect m_rectUp[MAX]; //存储上升泡泡的数组
CRect m_rectDown[MAX]; //存储下降泡泡的数组
bool m_boolUp[MAX]; //上升泡泡状态
bool m_boolDown[MAX]; //下降泡泡状态
int m_intUp;
int m_intDown;
int m_intNewBubble; //0---没有新泡泡;1---下降泡泡;2---上升泡泡
CRect m_rectNew; //最新一个泡泡的位置
};
////(3)在视图类的构造函数中初始化变量.
CBubbleView:CBubbleView()
{
m_intUp=0;
m_intDown=0;
m_intNewBubble=0;
for(int i=0;i<MAX;i++)
{
m_boolDown[i]=false;
m_boolup[i]=false;
}
stand((unsigned)time(NULL));
}
////(4)程序运行后启动定时器.
void CBubbleView::OnInitialUpdate()
{
CView::OnInitialUpdate();
SetTimer(1,100,NULL);
}
////(5)程序退出时终止定时器.
void CBubbleView::OnDestory()
CView::OnDestory();
KillTimer(1);
////(6)为鼠标消息添加处理函数.
void CBubbleView::OnLButtonDown(UINT nFlags,CPoint point)
{
CRect rectBound(ORIGINX,ORIGINY,ENDX,ENDY);
if(rectBound.PtInRect(point)&&m_intDown<MAX) //在区域内
{
int intNewRadius=rand()%30+10;
m_rectNew=CRect(point.x-intNewRadius,point.y-intNewRadius,point.x+intNewRadius,point.y+intNewRadius);
m_intNewBubble=1; //设置标志
}
CView::OnLButtonDown(nFlags,point);
}
void CBubbleView::OnRButtonDown(UINT nFlags,CPoint point)
{
CRect rectBound(ORIGINX,ORIGINY,ENDX,ENDY);
if(rectBound.PtInRect(point)&&m_intUp<MAX)
{
int intNewRadius=rand()%30+10;
m_rectNew=CRect(point.x-intNewRadius,point.y-intNewRadius,point.x+intNewRadius,point.y+intNewRadius);
m_intNewBubble=2;
}
CView::OnRButtonDown(nFlags,point);
}
////(7)为定时器消息添加处理函数.
void CBubbleView::OnTimer(UNIT nINEvent)
{
for(int i=1;i<MAX;i++)
{
if(m_boolDown[i]) //该位置有泡泡
{
MyInvalidateRect(m_rectDown[i]);
m_rectDown[i].top=m_rectDown[i].top-DR+DS; //移动泡泡
m_rectDown[i].left=m_rectDown[i].left-DR;
m_rectDown[i].right=m_rectDown[i].right+DR;
m_rectDown[i].bottom=m_rectDown[i].bottom+DR+DS;
if(m_rectDown[i].top>ENDY) //泡泡消失
{
m_rectDown[i]=flase;
m_intDown--;
}
else MyInvalidateRect(m_rectDown[i]);
}
else //空位置
{
if(m_intNewBubble==1)
{
m_rectDown[i]=m_rectNew;
m_intDown++;
m_intNewBubble=0; //复位
m_boolDown[i]=true;
MyInvalidateRect(m_rectNew);
}
}
if(m_boolUp[i]) //上升的泡泡
{
MyInvalidateRect(m_rectUP[i]);
m_rectUp[i].top=m_rectUp[i].top-DR-DS; //移动泡泡
m_rectUp[i].left=m_rectUp[i].left-DR;
m_rectUp[i].right=m_rectUp[i].right+DR;
m_rectUp[i].bottom=m_rectUp[i].bottom+DR-DS;
if(m_rectUp[i].bottom<ORIGINY)
{
m_boolUp[i]=flase;
m_intUp--;
}
else MyInvalidateRect(m_rectUp[i]);
}
else
{
if(m_intNewBubble==2)
{
m_rectUp[i]=m_rectNew;
m_intUp++;
m_intNewBubble=0; //复位
m_boolDown[i]=true;
MyInvalidateRect(m_rectNew);
}
}
}
//文字区刷新
InvalidateRect(CRect(ENDX+100,ORIGINY+40,ENDX+250,ORIGINY+100));
CView::OnTimer(nIDEvent);
}
////(8)修改OnDraw函数显示泡泡.
void CBubbleView::OnDraw(CDC* pDC)
{
CBubbleDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
//画矩形框
pDC->MoveTo(ORIGINX,ORIGINY);
pDC->LineTo(ORIGINX,ENDY);
pDC->LineTo(ENDY,ENDY);
pDC->LineTo(ENDY,ORIGINX);
pDC->LineTo(ORIGINX,ORIGINX);
CBrush brushUp(UPCOLOR);
CBrush* pbrushOld=pDC->SelectObject(&brushUp);
CBrush brushDown(DOWNCOLOR);
//画泡泡
for(int i=1;i<MAX;i++)
{
if(m_boolUp[i])
{
pDC->SelectObject(&brushUp);
pDC->Ellipse(m_rectUp[i]);
}
if(m_boolDown[i])
{
pDC->SelectObject(&brushDown);
pDC->Ellipse(m_rectDown[i]);
}
}
CString strOut;
strOut.Format("上升的泡泡数为 %d",m_intUp);
pDC->TextOut(ENDX+100,ORIGINX+40,strOut);
strOut.Format("下降的泡泡数为 %d",m_intDown);
pDC->TextOut(ENDX+100,ORIGINX+60,strOut);
pDC->SelectObject(pbrushOld);
}
////(9)添加成员函数MyInvalidateRect.
void CBubbleView::MyInvalidateRect(CRect rect rectUpdate)
{
int intLeft=max(rectUpdate.left,ORIGINX);
int intTop=max(rectUpdate.top,ORIGINX);
int intRight=min(rectUpdate.right,ENDX);
int intBottom=min(rectUpdate.bottom,ENDY);
InvalidateRect(CRect (intLeft,intTop,intRight,intBottom));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -