📄 ballview.cpp
字号:
// ballView.cpp : implementation of the CBallView class
//
#include "stdafx.h"
#include "ball.h"
#include "ballDoc.h"
#include "ballView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CBallView
int x=100,y=0,vx=2,vy=8;
IMPLEMENT_DYNCREATE(CBallView, CView)
BEGIN_MESSAGE_MAP(CBallView, CView)
//{{AFX_MSG_MAP(CBallView)
ON_WM_TIMER()
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBallView construction/destruction
CBallView::CBallView()
{
// TODO: add construction code here
}
CBallView::~CBallView()
{
}
BOOL CBallView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CBallView drawing
void CBallView::OnDraw(CDC* pDC)
{
CBallDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//Create(NULL,"绘图窗口");
CClientDC dc(this);
GetClientRect(&rect);
mdc = new CDC;
mdc = new CDC;
mdc->CreateCompatibleDC(&dc);
ball1 = new CBitmap;
ball1->m_hObject = (HBITMAP)::LoadImage(NULL,"ball1.bmp",IMAGE_BITMAP,71,69,LR_LOADFROMFILE); //加载小球图
}
/////////////////////////////////////////////////////////////////////////////
// CBallView diagnostics
#ifdef _DEBUG
void CBallView::AssertValid() const
{
CView::AssertValid();
}
void CBallView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CBallDoc* CBallView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBallDoc)));
return (CBallDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CBallView message handlers
void CBallView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CView::OnTimer(nIDEvent);
CClientDC dc(this);
mdc->SelectObject(ball1); //选择位图到 mdc 中
dc.BitBlt(x,y,71,69,mdc,0,0,SRCCOPY); //贴上小球图
x += vx; //计算新的 X 坐标
y += vy; //计算新的 Y 坐标
if(y+60 > rect.bottom) //是否碰到下缘
{
vy = -vy;
y = rect.bottom -60;
}
if(x+60 > rect.right) //是否碰到右缘
{
vx = -vx;
x = rect.right -60;
}
if(y < -10) //是否碰到上缘
{
vy = -vy;
y = -2;
}
if(x < -15) //是否碰到左缘
{
vx = -vx;
x =- 2;
}
}
int CBallView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
SetTimer(1,10,NULL); //创建定是器,设置运动速度
// TODO: Add your specialized creation code here
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -