当前位置:编程学习 > 网站相关 >>

游戏基础013---处理键盘消息

代码下载 /2012/0118/20120118081418571.rar

/************************************************************************/ 
/* 
 
*/ 
/************************************************************************// 
#include "stdafx.h" 
 
//全局变量声明 
HINSTANCE hInst; 
HBITMAP girl[4],bg;//girl[4]四个方向贴图 
HDC     hdc,mdc,bufdc; 
HWND    hWnd; 
DWORD   tPre,tNow; 
int     num,dir,x,y; //xy为人物贴图坐标,num为连续图中的小图编号 
 
 
//全局函数  www.zzzyk.com
void MyPaint(HDC); 
ATOM                MyRegisterClass(HINSTANCE hInstance); 
BOOL                InitInstance(HINSTANCE, int); 
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM); 
 
//****程序入口************************************** 
int APIENTRY WinMain(HINSTANCE hInstance, 
                     HINSTANCE hPrevInstance, 
                     LPSTR     lpCmdLine, 
                     int       nCmdShow) 

    MSG msg; 
 
    MyRegisterClass(hInstance); 
 
    //运行初始化函数 
    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 >= 40) 
            { 
                MyPaint(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); 

 
//****初始化************************************* 
// 1.存储instance handle与全局变量中 
// 2.建立并显示主窗口 
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 

    HBITMAP bmp; 
    hInst = hInstance; 
 
    hInst = hInstance; 
 
    hWnd = CreateWindow("canvas", "绘图窗口" , WS_OVERLAPPEDWINDOW, 
        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); 
 
    if (!hWnd) 
    { 
        return FALSE; 
    } 
 
    MoveWindow(hWnd,10,10,600,450,true); 
    ShowWindow(hWnd, nCmdShow); 
    UpdateWindow(hWnd); 
 
    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,"girl0.bmp",IMAGE_BITMAP,440,148,LR_LOADFROMFILE); 
    girl[1] = (HBITMAP)LoadImage(NULL,"girl1.bmp",IMAGE_BITMAP,424,154,LR_LOADFROMFILE); 
    girl[2] = (HBITMAP)LoadImage(NULL,"girl2.bmp",IMAGE_BITMAP,480,148,LR_LOADFROMFILE); 
    girl[3] = (HBITMAP)LoadImage(NULL,"girl3.bmp",IMAGE_BITMAP,480,148,LR_LOADFROMFILE); 
    bg = (HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,640,480,LR_LOADFROMFILE); 
 
    MyPaint(hdc); 
 
    return TRUE; 

 
/************************************************************************/ 
/*
人物贴图坐标修正及窗口贴图
*/ 
/************************************************************************/ 
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: 补充:综合编程 , 其他综合 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,