就是按下能走动但是一松开就关闭整个窗口了,不太清楚是哪儿出了问题
源代码如下:
#include "framework.h"
#include "girl.h"
#include <stdio.h>
//全局变量声明
HINSTANCE hInst;
HBITMAP girl[4], bg,bmp;
HDC hdc, mdc, bufdc;
HWND hWnd;
DWORD tPre, tNow;
int num, dir, x, y; //x,y变量为人物贴图坐标,dir为人物移动方向,num为连续贴图中的小图编号
bool archKeyStatus[4];
//全局函数声明
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void CartoonPaint(HDC hdc);
//****WinMain函数,程序入口点函数***********************
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
ZeroMemory(archKeyStatus, 0);
MyRegisterClass(hInstance);
//初始化
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}
//贴图初始位置和移动方向
x = 300;
y = 250;
dir = 0;
num = 0;
//载入连续移动位图、背景图
girl[0] = (HBITMAP)LoadImage(NULL, "girl0.bmp", IMAGE_BITMAP, 440, 148, LR_LOADFROMFILE);
girl[1] = (HBITMAP)LoadImage(NULL, "girl1.bmp", IMAGE_BITMAP, 440, 148, LR_LOADFROMFILE);
girl[2] = (HBITMAP)LoadImage(NULL, "girl2.bmp", IMAGE_BITMAP, 440, 148, LR_LOADFROMFILE);
girl[3] = (HBITMAP)LoadImage(NULL, "girl3.bmp", IMAGE_BITMAP, 440, 148, LR_LOADFROMFILE);
bg = (HBITMAP)LoadImage(NULL, "bg.bmp", IMAGE_BITMAP, 640, 480, LR_LOADFROMFILE);
hdc = GetDC(hWnd);
mdc = CreateCompatibleDC(hdc);
bufdc = CreateCompatibleDC(hdc);
//建立空的位图并置入mdc中
bmp = CreateCompatibleBitmap(hdc, 640, 480);
SelectObject(mdc, bmp);
CartoonPaint(hdc);
PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
//消息循环
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
tNow = GetTickCount();
if (tNow - tPre >= 40)
CartoonPaint(hdc);
}
}
return msg.wParam;
}
//设计窗口类
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "canvas";
wcex.hIconSm = NULL;
return RegisterClassEx(&wcex);
}
//****初始化函数*************************************
// 加载位图并设定各种初始值
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance;
hWnd = CreateWindow("canvas", "girl", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
MoveWindow(hWnd, 10, 10, 640, 480, true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_KEYDOWN: //按下键盘消息
//判断按键的虚拟键码
switch (wParam)
{
case VK_ESCAPE: //按下Esc键
PostQuitMessage(0); //结束程序
break;
case VK_UP: //按下↑键
archKeyStatus[0] = true;
dir = 0;
break;
case VK_DOWN: //按下↓键
archKeyStatus[1] = true;
dir = 1;
break;
case VK_LEFT: //按下←键
archKeyStatus[2] = true;
dir = 2;
break;
case VK_RIGHT: //按下→键
archKeyStatus[3] = true;
dir = 3;
break;
}
break;
case WM_KEYUP: //按键释放的消息
switch (wParam) {
case VK_UP: //向上
archKeyStatus[0]=false;
break;
case VK_DOWN: //向下
archKeyStatus[1] = false;
break;
case VK_LEFT: //向左
archKeyStatus[2] = false;
break;
case VK_RIGHT: //向右
archKeyStatus[3] = false;
break;
}
break;
case WM_DESTROY: //窗口结束消息
int i;
DeleteDC(mdc);
DeleteDC(bufdc);
for (i = 0; i < 4; i++)
DeleteObject(girl[i]);
DeleteObject(bg);
ReleaseDC(hWnd, hdc);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
void CartoonPaint(HDC hdc)
{
int w, h;
SelectObject(bufdc, bg);
BitBlt(mdc, 0, 0, 640, 480, bufdc, 0, 0, SRCCOPY);
SelectObject(bufdc, girl[dir]);
w = 55;
h = 74;
if (archKeyStatus[0]) {
y -= 10;
if (y < 0) y = 0;
}
if (archKeyStatus[1]) {
y += 10;
if (y > 375) y = 375;
}
if (archKeyStatus[2]) {
x -= 10;
if (x < 0)
x = 0;
}
if (archKeyStatus[3]) {
x += 10;
if (x > 575)
x = 575;
}
BitBlt(mdc, x, y, w, h, bufdc, num*w, h, SRCAND);
BitBlt(mdc, x, y, w, h, bufdc, num*w, 0, SRCPAINT);
BitBlt(hdc, 0, 0, 640, 480, mdc, 0, 0, SRCCOPY);
tPre = GetTickCount();
num++;
if (num == 8) num = 0;
}