直接上源码
// ChildView.h : CChildView 类的接口
//
#pragma once
// CChildView 窗口
class CChildView : public CWnd
{
// 构造
public:
CChildView();
void TransparentPNG(CImage *png);
void Draw();
// 特性
public:
// 操作
public:
struct shero
{
CImage hero;
int x;
int y;
int frame;
int direct;
}MyHero;
CRect m_client;
CImage m_bg;
CDC m_dcMemory;
CBitmap bmp;
// 重写
protected:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
// 实现
public:
virtual ~CChildView();
// 生成的消息映射函数
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnTimer(UINT_PTR nIDEvent);
};
// ChildView.cpp : CChildView 类的实现
//
#include "stdafx.h"
#include "BkRun.h"
#include "ChildView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define TIMER_PAINT 1
#define TIMER_HEROMOVE 2
#define DOWN 0
#define UP 3
#define LEFT 1
#define RIGHT 2
// CChildView
CChildView::CChildView()
{
}
void CChildView::TransparentPNG(CImage * png)
{
for (int i = 0; i <png->GetWidth(); i++)
{
for (int j = 0; j <png->GetHeight(); j++)
{
unsigned char* pucColor = reinterpret_cast<unsigned char *>(png->GetPixelAddress(i, j));
pucColor[0] = pucColor[0] * pucColor[3] / 255;
pucColor[1] = pucColor[1] * pucColor[3] / 255;
pucColor[2] = pucColor[2] * pucColor[3] / 255;
}
}
}
CChildView::~CChildView()
{
}
BEGIN_MESSAGE_MAP(CChildView, CWnd)
ON_WM_LBUTTONDOWN()
ON_WM_KEYDOWN()
ON_WM_CREATE()
ON_WM_TIMER()
END_MESSAGE_MAP()
// CChildView 消息处理程序
BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs)
{
if (!CWnd::PreCreateWindow(cs))
return FALSE;
cs.dwExStyle |= WS_EX_CLIENTEDGE;
cs.style &= ~WS_BORDER;
cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS,
::LoadCursor(NULL, IDC_ARROW), reinterpret_cast<HBRUSH>(COLOR_WINDOW+1), NULL);
m_bg.Load(L"bg.jpg");
MyHero.hero.Load(L"heroMove.jpg");
TransparentPNG(&MyHero.hero);
MyHero.direct = UP;
MyHero.frame = 0;
MyHero.x = 100;
MyHero.y = 400;
return TRUE;
}
void CChildView::Draw()
{
CDC* pDC = this->GetDC();// 用于绘制的设备上下文
GetClientRect(&m_client);
m_dcMemory.CreateCompatibleDC(pDC);
bmp.CreateCompatibleBitmap(pDC, m_client.Width(), m_client.Height());
m_dcMemory.SelectObject(&bmp);
m_bg.Draw(m_dcMemory, m_client);
MyHero.hero.Draw(m_dcMemory, MyHero.x, MyHero.y, 80, 80, MyHero.frame * 80, MyHero.direct * 80, 80, 80);
pDC->BitBlt(0, 0, m_client.Width(), m_client.Height(), &m_dcMemory, 0, 0, SRCCOPY);
m_dcMemory.DeleteDC();
bmp.DeleteObject();
ReleaseDC(pDC);
}
void CChildView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CWnd::OnLButtonDown(nFlags, point);
}
void CChildView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
switch (nChar)
{
case 'd':
case 'D':
MyHero.direct = RIGHT;
MyHero.x += 5;
break;
case 'a':
case 'A':
MyHero.direct = LEFT;
MyHero.x -= 5;
break;
case 'w':
case 'W':
MyHero.direct = UP;
MyHero.y -= 5;
break;
case 's':
case 'S':
MyHero.direct = DOWN;
MyHero.y += 5;
break;
}
CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}
int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: 在此添加您专用的创建代码
SetTimer(TIMER_PAINT, 10, NULL);
SetTimer(TIMER_HEROMOVE, 100, NULL);
return 0;
}
void CChildView::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
switch (nIDEvent)
{
case TIMER_PAINT:
Draw();
break;
case TIMER_HEROMOVE:
MyHero.frame++;
if (MyHero.frame == 4)
{
MyHero.frame = 0;
}
break;
}
CWnd::OnTimer(nIDEvent);
}