|
|
我用鼠标的 WM_LBUTTONDOWN消息 可以写了那个按下左键 做人物移动的动画 ,但是放在WM_KEYDOWN就不行了
具体思路是 按下左键的时候设定速度 V,启动TIMER ,显示动画,同时完成移动和动画。松开左键时,停止TIMER,设定V=0;但放在WM_KEYDOWN下只能移动一小段距离
应为WM_KEYDOWN和WM_KEYUP总时成对出现的,怎么才能用键盘消息业完成这样的移动啊
代码如下:
// 动画测试2.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include <wingdi.h> //头文件
#pragma comment (lib,"msimg32.lib") //连接库
#define MAX_LOADSTRING 100
// 全局变量
HINSTANCE hInst; // current instance
DWORD tPre,tNow;
DWORD tKeyPre,tKeyNow;
HDC hdc,mdc,buffdc;
HBITMAP bmp,dra,htmp;
int i;
int x,V;
// 函数声明
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
void MainPaint(HDC hdc);
void fun();
int count;
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
// 消息循环
while (msg.message!=WM_QUIT)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
tNow=GetTickCount();
if (tNow-tPre>=60)
MainPaint(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 = LoadIcon(hInstance, NULL);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "动画测试2";
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_ICON1);
return RegisterClassEx(&wcex);
}
//
//初始化函数
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow("动画测试2", "测试", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
i=1;
MoveWindow(hWnd,100,100,600,450,true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
V=0;
x=0;
hdc=GetDC(hWnd);
mdc=CreateCompatibleDC(hdc);
buffdc=CreateCompatibleDC(hdc);
htmp=CreateCompatibleBitmap(hdc,650,450);
SelectObject(mdc,htmp);
bmp=(HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,650,450,LR_LOADFROMFILE);
dra=(HBITMAP)LoadImage(NULL,"dra.bmp",IMAGE_BITMAP,760,99,LR_LOADFROMFILE);
MainPaint(hdc);
return TRUE;
}
//
//
//消息处理函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_LBUTTONDOWN:
V=5;
SetTimer(hWnd,1,100,NULL);
break;
case WM_LBUTTONUP:
i=2;
V=0;
KillTimer(hWnd,1);
break;
case WM_RBUTTONDOWN:
break;
case WM_TIMER:
fun();
break;
case WM_KEYDOWN:
//按下键的响应
switch(wParam)
{
case VK_UP:
break;
case VK_DOWN:
break;
case VK_LEFT:
break;
case VK_RIGHT:
V=5;
tKeyPre=GetTickCount();
SetTimer(hWnd,1,100,NULL);
break;
case VK_ESCAPE:
PostQuitMessage(0);
break;
}
break;
case WM_KEYUP:
tKeyNow=GetTickCount();
if (tKeyNow-tKeyPre>=500)
{
i=2;
V=0;
KillTimer(hWnd,1);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// 窗口重绘
MainPaint(hdc);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
KillTimer(hWnd,1);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
//主绘图,刷新函数
void MainPaint(HDC hdc)
{
SelectObject(buffdc,bmp);
BitBlt(mdc,0,0,650,450,buffdc,0,0,SRCCOPY);
SelectObject(buffdc,dra);
TransparentBlt(mdc,x,300,95,99,buffdc,i*95,0,95,99,RGB(255,0,255));
BitBlt(hdc,0,0,650,450,mdc,0,0,SRCCOPY);
tPre=GetTickCount();
}
void fun()
{
x+=V;
if (x>=650) x=0;
//MessageBox(NULL,"JJJ","JJJ",1);
i++;
if (i>=8)
i=1;
}
|
|