控制方向键盘
HBITMAP girl[4],bg;
HDC hdc,mdc,bufdc;
HWND hWnd;
DWORD tPre,tNow=0;
int num,dir,x,y;
void GemeCycle();
void MyPaint(HDC hdc);
::ZeroMemory(&msg,sizeof(msg)); //指定的内存块清零。 使用结构前清零,而不让结构的成员数值具有不确定性
// 主消息循环:
while (msg.message!=WM_QUIT/*GetMessage(&msg, NULL, 0, 0)*/)
{
//if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
if(::PeekMessageA(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
if (tPre-tNow>0)
{
GemeCycle();
}
}
hdc=GetDC(hWnd);
mdc=CreateCompatibleDC(hdc);
bufdc=CreateCompatibleDC(hdc);
bmp=CreateCompatibleBitmap(hdc,640,480);
SelectObject(mdc,bmp);
x=300;
y=250;
dir=0;
num=0;
girl[0]=(HBITMAP)LoadImage(NULL,"0.bmp",IMAGE_BITMAP,120,91,
LR_LOADFROMFILE);
girl[1]=(HBITMAP)LoadImage(NULL,"1.bmp",IMAGE_BITMAP,120,91,
LR_LOADFROMFILE);
girl[2]=(HBITMAP)LoadImage(NULL,"2.bmp",IMAGE_BITMAP,120,91,
LR_LOADFROMFILE);
girl[3]=(HBITMAP)LoadImage(NULL,"3.bmp",IMAGE_BITMAP,120,91,
LR_LOADFROMFILE);
bg=(HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,640,480,LR_LOADFROMFILE);
MyPaint(hdc);
case WM_KEYDOWN:
switch(wParam)
{
case VK_UP:
switch(dir)
{
case 0:
y-=10;
break;
case 1:
y-=1;
y-=8;
break;
case 2:
x+=2;
y-=10;
break;
case 3:
x+=2;
y-=10;
break;
}
if(y<0)
y=0;
dir=0;
break;
case VK_DOWN:
switch(dir)
{
case 0:
x+=1;
y+=8;
break;
case 1:
y+=10;
break;
case 2:
x+=3;
y+=6;
break;
case 3:
x+=3;
y+=6;
break;
}
if(y>375)
y=375;
dir=1;
break;
case VK_LEFT:
switch(dir)
{
case 0:
x-=12;
break;
case 1:
x-=13;
y+=4;
break;
case 2:
x-=10;
break;
case 3:
x-=10;
break;
}
if(x<10)
x=0;
dir=2;
break;
case VK_RIGHT:
switch(dir)
{
case 0:
x+=8;
break;
case 1:
x+=7;
y+=4;
break;
case 2:
x+=10;
break;
case 3:
x+=10;
break;
}
if(x>575)
x=575;
dir=3;
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);
}
void MyPaint(HDC hdc)
{
int w,h;
SelectObject(bufdc,bg);
BitBlt(mdc,0,0,640,480,bufdc,0,0,SRCCOPY);
SelectObject(bufdc,girl[dir]);
switch(dir)
{
case 0:
w=60;
h=74;
break;
case 1:
w=60;
h=91;
break;
case 2:
w=60;
h=91;
break;
case 3:
w=60;
h=91;
break;
}
BitBlt(mdc,x,y,w,h,bufdc,60,0,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;
}
void GemeCycle()
{
int w,h;
SelectObject(bufdc,bg);
BitBlt(mdc,0,0,640,480,bufdc,0,0,SRCCOPY);
SelectObject(bufdc,girl[dir]);
switch(dir)
{
case 0:
w=60;
h=74;
break;
case 1:
w=60;
h=91;
break;
case 2:
w=60;
h=91;
break;
case 3:
w=60;
h=91;
break;
}
BitBlt(mdc,x,y,w,h,bufdc,60,0,SRCAND);
BitBlt(mdc,x,y,w,h,bufdc,0,0,SRCPAINT);
BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY);
//tPre=GetTickCount();
tNow =tPre+400;
num++;
if(num==8)
num=0;
}