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

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

 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-=;
if(y<)y+=;
break;
case VK_DOWN:
y=(y+)%;
break;
case VK_LEFT:
speed-=;
if(speed<)speed=;
SetTimer(hWnd,,speed,NULL);
break;
case VK_RIGHT:
speed+=;
if(speed>)speed=;
SetTimer(hWnd,,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,);
PostQuitMessage();
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return ;
}

 // stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
// #if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
#define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_ #if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers // Windows Header Files:
#include <windows.h> // C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h> // Local Header Files // TODO: reference additional headers your program requires here //{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)

StdAfx.h

 //{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by FE.RC
//
#define IDR_MAINFRAME 128
#define IDD_FE_DIALOG 102
#define IDD_ABOUTBOX 103
#define IDS_APP_TITLE 103
#define IDM_ABOUT 104
#define IDM_EXIT 105
#define IDS_HELLO 106
#define IDI_FE 107
#define IDI_SMALL 108
#define IDC_FE 109
#define IDC_MYICON 2
#define IDC_STATIC -1
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE 129
#define _APS_NEXT_COMMAND_VALUE 32771
#define _APS_NEXT_CONTROL_VALUE 1000
#define _APS_NEXT_SYMED_VALUE 110
#endif
#endif

resourse.h

#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=;//恐龙坐标 // 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,,,PM_REMOVE))//PeekMessage()函数来检测目前是否有消息处理(检测到0,否则1)
{ //------------|不能用GetMassage()换,因为它只有取得WM_QUIT时才返回0,其他返回非0,错误返回-1
TranslateMessage(&msg);//转换伪码及字符
DispatchMessage(&msg);//将控制权交给系统,再有系统决定负责处理消息的程序; }
else//当此次循环运行与上次绘图时间相差0.1秒时再进行重绘操作
{
tNow=GetTickCount();//取得从开始到现在运行的时间百万分之一秒
if(tNow-tPre>=)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 = ;
wcex.cbWndExtra = ;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = NULL;
wcex.hCursor = LoadCursor(NULL,IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "fe";
wcex.hIconSm = NULL; return RegisterClassEx(&wcex);//调用RegisterClassEx函数注册类别,返回一个“ATOM"形态的字符串
//此字符串即为类别名称”fe";
}
//============================================================================================ //============================================================================================
//按照前面所定义的窗口类别来建立并显示实际的程序窗口
//============================================================================================
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
char filename[]="";
HBITMAP bmp;
hInst = hInstance; // 把instance handle 储存在全局变量中; hWnd = CreateWindow("fe","绘图窗口",WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, , CW_USEDEFAULT, , NULL, NULL, hInstance, NULL);
//-----------------------------------------------
//调用CreateWindow函数来建立一个窗口对象
//第一个参数就是窗口建立依据的类别名称
//-----------------------------------------------
if (!hWnd)
{
return FALSE;
}
//------------------------------------------------
//设定窗口的位置及窗口的大小,然后绘制显示在设备上
//-------------------------------------------------
MoveWindow(hWnd,,,,,true);//位置及大小
ShowWindow(hWnd, nCmdShow);//改定窗口显示时的状态
UpdateWindow(hWnd);//将窗口绘制在显示设备上 hdc=GetDC(hWnd);
mdc=CreateCompatibleDC(hdc);
bufdc=CreateCompatibleDC(hdc); bmp=CreateCompatibleBitmap(hdc,,);//后面的位图处理是在bmp中进行
SelectObject(mdc,bmp); dra=(HBITMAP)LoadImage(NULL,"dra.bmp",IMAGE_BITMAP,,,LR_LOADFROMFILE);
bg=(HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,,,LR_LOADFROMFILE); num=;
x=;//起始坐标
y=;
SetTimer(hWnd,,,NULL);
MyPaint(hdc); return TRUE;
}
//============================================================================================ //============================================================================================
//
//============================================================================================
void MyPaint(HDC hdc)
{
if(num==)num=;
if(tNow-tCheck>=)//判断此次绘图时间由前一秒算起是否已经达到1秒钟的时间间隔
{
tCheck=tNow;//并重设下一次起始时间
} SelectObject(bufdc,bg);//在mdc上贴上背景图
BitBlt(mdc,,,,,bufdc,,,SRCCOPY); SelectObject(bufdc,dra);//在mdc上透明处理
BitBlt(mdc,x,y,,,bufdc,num*,,SRCAND);
BitBlt(mdc,x,y,,,bufdc,num*,,SRCPAINT); BitBlt(hdc,,,,,mdc,,,SRCCOPY);//将最后的画面显示在窗口上 tPre=GetTickCount();//记录此次绘图的时间 num++; x-=;//记录下次贴图的坐标
if(x<=-)x=;
}
//============================================================================================ //============================================================================================
//在前面定义类别的时候把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-=;
if(y<)y+=;
break;
case VK_DOWN:
y=(y+)%;
break;
case VK_LEFT:
speed-=;
if(speed<)speed=;
SetTimer(hWnd,,speed,NULL);
break;
case VK_RIGHT:
speed+=;
if(speed>)speed=;
SetTimer(hWnd,,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,);
PostQuitMessage();
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return ;
} //============================================================================================

[游戏模版14] Win32 键盘控制的更多相关文章

  1. [游戏模版2] Win32最小框架

    >_<:Just the minimum Win32  frame don't have any other special function. //{{NO_DEPENDENCIES}} ...

  2. [游戏模版3] Win32 画笔 画刷 图形

    >_<:introduce the functions of define\create\use pen and brush to draw all kinds of line and s ...

  3. [游戏模版4] Win32 显示鼠标位置

    >_<:use MOUSE_MOVE message refresh the position information. >_<:use LOWORD(lParam) get ...

  4. [游戏模版5] Win32 折线 弧线

    >_<:first build some points put in poly1[],poly2[] and poly3[] in the function of InitInstance ...

  5. [游戏模版6] Win32 graph

    >_<:there in the MyPaint(...) function respectively use Ellipse(...) draw ellipse, use RoundRe ...

  6. [游戏模版7] Win32 最简单贴图

    >_<:this is the first using mapping. >_<:There will be introducing how to do: First load ...

  7. [游戏模版9] Win32 半透明 图像处理

    >_<:Previous part we talk about how to map a transparent picture, and this time we will solve ...

  8. [游戏模版10] Win32 平面地图贴图 正

    >_<:picture resource >_<:If you master the ways of mapping picture,then this problem is ...

  9. [游戏模版11] Win32 动画 时间消息

    >_<:This time we will study a new way to operate your picture.That is running your picture by ...

随机推荐

  1. Linux_09------Linux上系统扫描和安全策略

    先谢慕课网/** * linux系统扫描技术 * * 主机扫描.路由扫描.批量服务扫描.系统安全策略(防SYN和ddos攻击) */ /** * 主机扫描 * ping fping hping * * ...

  2. Modbus协议

    总体上来说,发送命令格式如下:模块号(1字节)功能码 起始地址(2字节)   数据(X字节) CRC(2位) 模块号(1字节)功能码(要读取的寄存器,读 01,设置05,Coil Status/Inp ...

  3. SGU 319. Kalevich Strikes Back (线段树)

    319. Kalevich Strikes Back Time limit per test: 0.5 second(s)Memory limit: 65536 kilobytes input: st ...

  4. aps.net 图形验证码(转)

    参考文章: http://www.cnblogs.com/FayJack/articles/3063146.html 创建CreateCode.ashx文件: <%@ WebHandler La ...

  5. Ajax做分页

    Ajax做分页 用这种ajax做分页的方法比较简单,把代码直接复制就可以,然后根据实际更改一下里面的参数. .设置分页显示显示的样式,显示效果如下. 复制代码 <style type=" ...

  6. CSS3,JS可用于刷新按钮或者加载动画的动画

    html: <input type="button" id="zidong3" style="top: 12px;" /> cs ...

  7. 在Mac上安装IntelliJ IDEA

    这篇文章旨在介绍如何在Mac系统上安装IntelliJ IDEA,至于IntelliJ IDEA的介绍和使用方法,大家另行查阅,本篇的文章不再详细阐述. 简短解说,IntelliJ IDEA是可以用来 ...

  8. (引用 )自动化测试报告HTMLtestrunner

    1>下载HTMLTestRunner.py文件,地址为: http://tungwaiyip.info/software/HTMLTestRunner.html   Windows平台: 将下载 ...

  9. docker--wekan安装

    最近因为搭建开源看板网站的需要,玩了下docker 一开始在redhat上安装docker wget http://mirrors.163.com/.help/CentOS7-Base-163.rep ...

  10. CCKJ 笔试

    面向对象三个特性: 面向对象技术是目前流行的系统设计开发技术,它包括面向对象分析和面向对象程序设计.面向对象程序设计技术的提出,主要是为了解决传统程序设计方法--结构化程序设计所不能解决的代码重用问题 ...