[游戏模版14] Win32 键盘控制

2020-12-12 22:23

阅读:514

标签:des   style   blog   class   code   c   

 

>_<:>compared with the previous article,this one only adds key-message listener.

>_<:up down left right control the roal movement.>

soscw.com,搜素材
 1 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
 2 {
 3     int wmId, wmEvent;
 4     PAINTSTRUCT ps;
 5 
 6     switch (message)
 7     {
 8     case WM_KEYDOWN:
 9         switch(wParam)
10         {
11              case VK_UP:
12                  y-=20;
13                  if(y0)y+=480;
14                  break;
15              case VK_DOWN:
16                  y=(y+20)%480;
17                  break;
18              case VK_LEFT:
19                  speed-=10;
20                  if(speed0)speed=0;
21                  SetTimer(hWnd,1,speed,NULL);
22                  break;
23              case VK_RIGHT:
24                  speed+=10;
25                  if(speed>300)speed=300;
26                  SetTimer(hWnd,1,speed,NULL);
27                  break;
28         }
29         break;
30     case WM_TIMER:
31         MyPaint(hdc);
32         break;
33     case WM_COMMAND:
34         wmId    = LOWORD(wParam);
35         wmEvent = HIWORD(wParam);
36         // 分析菜单选择:
37         switch (wmId)
38         {
39         case IDM_EXIT:
40             DestroyWindow(hWnd);
41             break;
42         default:
43             return DefWindowProc(hWnd, message, wParam, lParam);
44         }
45         break;
46     case WM_PAINT:
47         hdc = BeginPaint(hWnd, &ps);
48         // TODO: 在此添加任意绘图代码...
49         EndPaint(hWnd, &ps);
50         break;
51     case WM_DESTROY:
52         DeleteDC(mdc);
53         ReleaseDC(hWnd,hdc);
54         DeleteObject(bg);
55         DeleteObject(dra);
56         KillTimer(hWnd,1);
57         PostQuitMessage(0);
58         break;
59     default:
60         return DefWindowProc(hWnd, message, wParam, lParam);
61     }
62     return 0;
63 }
soscw.com,搜素材

soscw.com,搜素材

soscw.com,搜素材soscw.com,搜素材
 1 // stdafx.h : include file for standard system include files,
 2 //  or project specific include files that are used frequently, but
 3 //      are changed infrequently
 4 //
 5 
 6 #if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
 7 #define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_
 8 
 9 #if _MSC_VER > 1000
10 #pragma once
11 #endif // _MSC_VER > 1000
12 
13 #define WIN32_LEAN_AND_MEAN        // Exclude rarely-used stuff from Windows headers
14 
15 
16 // Windows Header Files:
17 #include 18 
19 // C RunTime Header Files
20 #include 21 #include 22 #include 23 #include 24 
25 // Local Header Files
26 
27 // TODO: reference additional headers your program requires here
28 
29 //{{AFX_INSERT_LOCATION}}
30 // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
31 
32 #endif // !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
StdAfx.h
soscw.com,搜素材soscw.com,搜素材
 1 //{{NO_DEPENDENCIES}}
 2 // Microsoft Visual C++ generated include file.
 3 // Used by FE.RC
 4 //
 5 #define IDR_MAINFRAME                    128
 6 #define IDD_FE_DIALOG        102
 7 #define IDD_ABOUTBOX                    103
 8 #define IDS_APP_TITLE                    103
 9 #define IDM_ABOUT                        104
10 #define IDM_EXIT                        105
11 #define IDS_HELLO                        106
12 #define IDI_FE                107
13 #define IDI_SMALL                        108
14 #define IDC_FE                109
15 #define IDC_MYICON                        2
16 #define IDC_STATIC                        -1
17 // Next default values for new objects
18 //
19 #ifdef APSTUDIO_INVOKED
20 #ifndef APSTUDIO_READONLY_SYMBOLS
21 
22 #define _APS_NEXT_RESOURCE_VALUE        129
23 #define _APS_NEXT_COMMAND_VALUE         32771
24 #define _APS_NEXT_CONTROL_VALUE         1000
25 #define _APS_NEXT_SYMED_VALUE           110
26 #endif
27 #endif
resourse.h
soscw.com,搜素材
#include "stdafx.h"
#include "resourse.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;                                // current instance
TCHAR szTitle[MAX_LOADSTRING];                                // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];                                // The title bar text
HBITMAP dra,bg;//背景和恐龙
HDC hdc,mdc,bufdc;//窗口DC.存储贴到窗口上的内容并透明处理.选取位图用的DC
HWND hWnd;
DWORD tPre,tNow,tCheck;
int x,y,num,speed=200;//恐龙坐标

// Foward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
void                MyPaint(HDC hdc);
//========================================================================================
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
     // TODO: Place code here.
    MSG msg;

    MyRegisterClass(hInstance);//调用函数向系统注册窗口类别,输入参数hInstance是目前运行程序的对象代码;

    // 调用InitInstance函数,进行初始化操作;
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    // 消息循环(通过消息循环来获取信息,
    //进行必要的键盘信息转换而后将控制权交给操作系统,
    //有操作系统决定哪个程序的消息处理函数处理消息
    while (msg.message!=WM_QUIT) //不是窗口结束消息WM_QUIT,则继续循环
    {
        if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))//PeekMessage()函数来检测目前是否有消息处理(检测到0,否则1)
        {                                       //------------|不能用GetMassage()换,因为它只有取得WM_QUIT时才返回0,其他返回非0,错误返回-1
            TranslateMessage(&msg);//转换伪码及字符
            DispatchMessage(&msg);//将控制权交给系统,再有系统决定负责处理消息的程序;

        }
        else//当此次循环运行与上次绘图时间相差0.1秒时再进行重绘操作
        {
            tNow=GetTickCount();//取得从开始到现在运行的时间百万分之一秒
            if(tNow-tPre>=200)MyPaint(hdc);    //tPre前次绘图的时间;计算上次绘图到这次循环之间的时间
        }                                   //------------|若相差100个单位进行一次绘图操作,通过这
    }                                       //------------|个操作可以调整运行快慢

    return msg.wParam;
}
//=====================================================================================



//=============================================================================================
//在建立程序窗口实体之前,必须先定义一个窗口类别,其中包含所要建立窗口的信息,
//并向系统注册,这里的MyRegisterClass函数就是进行定义及注册窗口类别的函数。
//==============================================================================================
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;            //申请一个窗口类别“WNDCLASSEX”和结构”wcex“
                                //--------------------------------------------------------------
                                //定义vcex结构的各项信息,其中设定信息处理函数(lpfnWndProc)
                                //为WNDPROC,类别名称为(lpszClassName)为”fe";
                                //--------------------------------------------------------------
    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    = "fe";
    wcex.hIconSm        = NULL;

    return RegisterClassEx(&wcex);//调用RegisterClassEx函数注册类别,返回一个“ATOM"形态的字符串
                                  //此字符串即为类别名称”fe";
}
//============================================================================================


//============================================================================================
//按照前面所定义的窗口类别来建立并显示实际的程序窗口
//============================================================================================
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   char filename[20]="";
   HBITMAP bmp;
   hInst = hInstance; // 把instance handle 储存在全局变量中;

   hWnd = CreateWindow("fe","绘图窗口",WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
                      //-----------------------------------------------
                      //调用CreateWindow函数来建立一个窗口对象
                      //第一个参数就是窗口建立依据的类别名称
                      //-----------------------------------------------
   if (!hWnd)
   {
      return FALSE;
   }
   //------------------------------------------------
   //设定窗口的位置及窗口的大小,然后绘制显示在设备上
   //-------------------------------------------------
   MoveWindow(hWnd,10,10,640,480,true);//位置及大小
   ShowWindow(hWnd, nCmdShow);//改定窗口显示时的状态
   UpdateWindow(hWnd);//将窗口绘制在显示设备上

   hdc=GetDC(hWnd);
   mdc=CreateCompatibleDC(hdc);
   bufdc=CreateCompatibleDC(hdc);

   bmp=CreateCompatibleBitmap(hdc,640,480);//后面的位图处理是在bmp中进行
   SelectObject(mdc,bmp);

   dra=(HBITMAP)LoadImage(NULL,"dra.bmp",IMAGE_BITMAP,760,200,LR_LOADFROMFILE);
   bg=(HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,640,480,LR_LOADFROMFILE);

   num=0;
   x=500;//起始坐标
   y=300;
   SetTimer(hWnd,1,200,NULL);
   MyPaint(hdc);

   return TRUE;
}
//============================================================================================


//============================================================================================
//
//============================================================================================
void MyPaint(HDC hdc)
{
    if(num==8)num=0;
    if(tNow-tCheck>=200)//判断此次绘图时间由前一秒算起是否已经达到1秒钟的时间间隔
    {
        tCheck=tNow;//并重设下一次起始时间
    }

    SelectObject(bufdc,bg);//在mdc上贴上背景图
    BitBlt(mdc,0,0,640,480,bufdc,0,0,SRCCOPY);

    SelectObject(bufdc,dra);//在mdc上透明处理
    BitBlt(mdc,x,y,100,100,bufdc,num*100,100,SRCAND);
    BitBlt(mdc,x,y,100,100,bufdc,num*100,0,SRCPAINT);

    BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY);//将最后的画面显示在窗口上

    tPre=GetTickCount();//记录此次绘图的时间

    num++;

    x-=20;//记录下次贴图的坐标
    if(x95)x=640;
}
//============================================================================================


//============================================================================================
//在前面定义类别的时候把WndProc定义为消息处理函数(当某些外部消息发生时,会按消息的类型
//来决定该如何进行处理。此外该函数也是一个回叫函数(CALLBACK)(windows系统函数)每一个
//程序都会接收信息,选择性接受、处理;
//============================================================================================
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;

    switch (message)
    {
    case WM_KEYDOWN:
        switch(wParam)
        {
             case VK_UP:
                 y-=20;
                 if(y0)y+=480;
                 break;
             case VK_DOWN:
                 y=(y+20)%480;
                 break;
             case VK_LEFT:
                 speed-=10;
                 if(speed0)speed=0;
                 SetTimer(hWnd,1,speed,NULL);
                 break;
             case VK_RIGHT:
                 speed+=10;
                 if(speed>300)speed=300;
                 SetTimer(hWnd,1,speed,NULL);
                 break;
        }
        break;
    case WM_TIMER:
        MyPaint(hdc);
        break;
    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // 分析菜单选择:
        switch (wmId)
        {
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        // TODO: 在此添加任意绘图代码...
        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        DeleteDC(mdc);
        ReleaseDC(hWnd,hdc);
        DeleteObject(bg);
        DeleteObject(dra);
        KillTimer(hWnd,1);
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

//============================================================================================
soscw.com,搜素材

 

 

[游戏模版14] Win32 键盘控制,搜素材,soscw.com

[游戏模版14] Win32 键盘控制

标签:des   style   blog   class   code   c   

原文地址:http://www.cnblogs.com/zjutlitao/p/3734058.html


评论


亲,登录后才可以留言!