true); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); hdc = GetDC(hWnd); mdc = CreateCompatibleDC(hdc); bufdc = CreateCompatibleDC(hdc); bmp = CreateCompatibleBitmap(hdc,640,480); SelectObject(mdc,bmp); bg = (HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,640,480,LR_LOADFROMFILE); bird = (HBITMAP)LoadImage(NULL,"angrybird.bmp",IMAGE_BITMAP,120,60,LR_LOADFROMFILE); GetClientRect(hWnd,&rect); //取得内部窗口区域的大小 MyPaint(hdc); return TRUE; } //****自定义绘图函数********************************* // 1.进行窗口贴图 // 2.计算小鸟贴图坐标并判断小鸟是否碰到窗口边沿 void MyPaint(HDC hdc) { SelectObject(bufdc,bg); BitBlt(mdc,0,0,640,480,bufdc,0,0,SRCCOPY); SelectObject(bufdc,bird); BitBlt(mdc,x,y,60,60,bufdc,60,0,SRCAND); BitBlt(mdc,x,y,60,60,bufdc,0,0,SRCPAINT); BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY); //计算X轴贴图坐标与速度 x += vx; if(x <= 0) { x = 0; vx = -vx; } else if(x >= rect.right-60) { x = rect.right - 60; vx = -vx; } //计算Y轴贴图坐标与速度 y += vy; if(y<=0) { y = 0; vy = -vy; } else if(y >= rect.bottom-60) { y = rect.bottom - 60; vy = -vy; } tPre = GetTickCount(); //记录此次绘图时间 } ////****消息处理函数*********************************** LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_KEYDOWN: //按键消息 if(wParam==VK_ESCAPE) //按下【Esc】键 PostQuitMessage(0); break; case WM_DESTROY: //窗口结束消息 DeleteDC(mdc); DeleteDC(bufdc); DeleteObject(bg); DeleteObject(bird); ReleaseDC(hWnd,hdc); PostQuitMessage(0); break; default: //其他消息 return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } #include "stdafx.h" #include
//全局变量声明 HINSTANCE hInst; HBITMAP bg,bird; HDC hdc,mdc,bufdc; HWND hWnd; DWORD tPre,tNow,tCheck; RECT rect; //定义一个RECT结构体,用于储存内部窗口区域的坐标 int x=50,y=50,vx=15,vy=15; //x与y是小鸟在窗口中的贴图坐标,vx与vy为小鸟在x与y轴运动的速度分量
//全局函数声明 ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void MyPaint(HDC hdc);
//****WinMain函数,程序入口点函数************************************** int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg;
MyRegisterClass(hInstance);
//初始化 if (!InitInstance (hInstance, nCmdShow)) { return FALSE; }
//消息循环 GetMessage(&msg,NULL,NULL,NULL); //初始化msg while( msg.message!=WM_QUIT ) { if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } else { tNow = GetTickCount(); if(tNow-tPre >= 40) MyPaint(hdc); } }
return msg.wParam; }
//****设计一个窗口类,类似填空题,使用窗口结构体********************* ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEX |