2D游戏编程1--windows编程模型
一.创建一个windows程序步骤
1.创建一个windows类
2.创建一个事件处理程序
3.注册windows类
4.用之前创建的windows类创建一个窗口
5.创建一个主事件循环
二.存储windows类信息的数据结构:
typedef struct _WNDCLASSEX
{
UINT cbSize; // size of this structure
UINT style; // style flags
WNDPROC lpfnWndProc; // function pointer to handler
int cbClsExtra; // extra class info
int cbWndExtra; // extra window info
HANDLE hInstance; // the instance of the application
HICON hIcon; // the main icon
HCURSOR hCursor; // the cursor for the window
HBRUSH hbrBackground; // the background brush to paint the window
LPCTSTR lpszMenuName; // the name of the menu to attach
LPCTSTR lpszClassName; // the name of the class itself
HICON hIconSm; // the handle of the small icon
} WNDCLASSEX
cbSize=sizeof(WNDCLASSEX);
style值:
winclass.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC | CS_DBLCLICKS;
CS_HREDRAW
Redraws the entire window if a movement or size adjustment changes the width of the window.
CS_VREDRAW
Redraws the entire window if a movement or size adjustment changes the height of the window.
CS_OWNDC
Allocates a unique device context for each window in the class (more on this later in the chapter).
CS_DBLCLKS
Sends a double-click message to the window procedure when the user double-clicks the mouse while the cursor is in a window belonging to the class.
CS_PARENTDC
Sets the clipping region of the child window to that of the parent window so that the child can draw on the parent.
CS_SAVEBITS
Saves the client image in a window so you don't have to redraw it every time the window is obscured, moved, etc. However, this takes up more memory and is slower that doing it yourself.
CS_NOCLOSE
Disables the Close command on the system menu.
Note: The most commonly used flags are highlighted.
lpfnWndProc窗口事件处理函数指针:
完整定义:
WNDCLASSEX winclass; // this will hold the class we create
// first fill in the window class structure
inclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = "WINCLASS1";
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
三.注册windows类
RegisterClassEx(&winclass);
四.创建窗口
HWND CreateWindowEx(
DWORD dwExStyle, // extended window style
LPCTSTR lpClassName, // pointer to registered class name
LPCTSTR lpWindowName, // pointer to window name
DWORD dwStyle, // window style
int x, // horizontal position of window
int y, // vertical position of window
int nWidth, // window width
int nHeight, // window height
HWND hWndParent, // handle to parent or owner window
HMENU hMenu, // handle to menu, or child-window identifier
HINSTANCE hInstance, // handle to application instance
LPVOID lpParam); // pointer to window-creation data
HWND hwnd; // window handle // create the window, bail if problem
if (!(hwnd = CreateWindowEx(NULL, // extended style
"WINCLASS1", // class
"Your Basic Window", // title
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0,0, // initial x,y
400,400, // initial width, height
NULL, // handle to parent
NULL, // handle to menu
hinstance, // instance of this application
NULL))) // extra creation parms
return(0);
Once the window has been created, it may or may not be visible. However, in this case, we added the style flag WS_VISIBLE, which does this automatically. If this flag isn't added, use the following function call to manually display the window:
// this shows the window
ShowWindow(hwnd, ncmdshow);
五.事件处理程序
WinProc原型:
LRESULT CALLBACK WindowProc(
HWND hwnd, // window handle of sender
UINT msg, // the message id
WPARAM wparam, // further defines message
LPARAM lparam); // further defines message
实例:
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT ps; // used in WM_PAINT
HDC hdc; // handle to a device context // what is the message
switch(msg)
{
case WM_CREATE:
{
// do initialization stuff here // return success
return(0);
} break; case WM_COMMAND:
{
switch(LOWORD(wparam))
{
// handle the FILE menu
case MENU_FILE_ID_OPEN:
{
// do work here
} break;
case MENU_FILE_ID_CLOSE:
{
// do work here
} break;
case MENU_FILE_ID_SAVE:
{
// do work here
} break;
case MENU_FILE_ID_EXIT:
{
// do work here
} break; // handle the HELP menu
case MENU_HELP_ABOUT:
{
// do work here
} break;
default: break; } // end switch wparam } break; // end WM_COMMAND case WM_PAINT:
{
// simply validate the window
hdc = BeginPaint(hwnd,&ps);
// you would do all your painting here
EndPaint(hwnd,&ps);
// return success
return(0);
} break; case WM_DESTROY:
{
// kill the application, this sends a WM_QUIT message
PostQuitMessage(0); // return success
return(0);
} break; default:break; } // end switch // process any messages that we didn't take care of
return (DefWindowProc(hwnd, msg, wparam, lparam)); } // end WinProc
六.主事件循环
// enter main event loop
while(GetMessage(&msg,NULL,0,0))
{
// translate any accelerator keys
TranslateMessage(&msg);
// send the message to the window proc
DispatchMessage(&msg);
} // end while
事件循环处理消息机制流程图:
2D游戏编程1--windows编程模型的更多相关文章
- C++,C++编程,Windows编程,MFC
编程 我们日常生活中接触到的电子类产品中的应用都是由编程而来 为什么编程,偷懒 我们通过编程驱使(指挥,命令)的是电信号 为什么上面说编程是偷懒,电的发现,给人们带来了便利,人们在各个方面驱使(换成“ ...
- Windows编程
本文整理自百科.知乎与 科学家的世界 问题一:为什么开发windows应用程序不用c 而用.net,java,c++? 用 c+windows API 开发windows 应用程序 比用.net, ...
- 《Windows编程零基础学》第零节
首先很开心申请到了这一个专栏<Windows编程零基础学> 这是第一篇文章,在这里,我将讲述一些基础的知识. 什么是Windows编程 所谓Windows编程就是在Windows平台上开发 ...
- MFC-01-Chapter01:Hello,MFC---1.1 Windows 编程模型
1.1 Windows编程模型 为传统的操作系统编写的程序使用的是过程化模型,即程序从头到尾按顺序执行.例如C程序,从main函数入口开始执行,中间调用不同的函数一直到程序结束返回,这种过程是程序本身 ...
- Direct3D 10学习笔记(四)——Windows编程
本篇将简单整理基本的Windows应用程序的实现,并作为创建Direct3D 10应用程序的铺垫.具体内容参照< Introduction to 3D Game Programming with ...
- 原创教程“ActionScript3.0游戏中的图像编程”開始连载啦!
经过近两年的不懈努力,笔者的原创教程"ActionScript3游戏中的图像编程"最终在今日划上了完美的句号!这其中记录着笔者多年来在游戏制作,尤其是其中图像处理方 ...
- 有一定基础的 C++ 学习者该怎样学习 Windows 编程?
人的心理有个奇异的特性:一项知识一旦学会之后,学习过程中面临的困惑和不解非常快就会忘得干干净净,似乎一切都是自然而然,本来就该这种.因此,关于「怎样入门」这类问题,找顶尖高手来回答,未必能比一个刚入门 ...
- .NET “底层”异步编程模式——异步编程模型(Asynchronous Programming Model,APM)
本文内容 异步编程类型 异步编程模型(APM) 参考资料 首先澄清,异步编程模式(Asynchronous Programming Patterns)与异步编程模型(Asynchronous Prog ...
- windows编程经典书籍
本人是刚刚开始学习windows编程的,感觉看雪学院的大牛很NB.想找一些书籍来看学习学习,可是不知道看哪些书好.驱动,对菜鸟们来说真是一个很深奥的话题,所以 ,我找来了这篇文章供大家分享,以后大家发 ...
- windows编程入门最重要的
要入门 Windows 编程,最重要的不是阅读什么教材,使用什么工具,而是先必须把以下几个对于初学者来说非常容易困惑的重要概念搞清楚: 1. 文字的编码和字符集.这部分需要掌握 ANSI 模式和 Un ...
随机推荐
- SQL的定义与使用
一.SQL的定义 SQL(structured query language)即结构化查询语句,是关系数据库的标准语言. SQL的特点有: 1.综合统一 SQL集数据定义语言DDL.数据操作语言DML ...
- 使用Fiddler提高前端工作效率 (介绍篇)
1. Fiddler 是什么? Fiddler是用C#编写的一个免费的HTTP/HTTPS网络调试器.英语中Fiddler是小提琴的意思,Fiddler Web Debugger就像小提琴一样,可以让 ...
- 发测试邮件或垃圾邮件node脚本
npm install nodemailer 执行后,指定目录下会出现node_modules模块,再相同目录下,创建main.js,js代码如下: var nodemailer = require( ...
- 【bzoj】1026: [SCOI2009]windy数
1026: [SCOI2009]windy数 Description windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道,在A和B之间 ...
- glibc 安装( version `GLIBC_2.14' not found")
在ubuntu上编译的东西 拿到CentOS 下运行 提示 :“/lib64/libc.so.6: version `GLIBC_2.14' not found” 原因是ubuntu上用的libc 版 ...
- Windows命令行语法说明
摘自:http://lavasoft.blog.51cto.com/62575/1113234 Windows命令行语法说明 说来惭愧,用windows这么多年了,对其命令行语法看得似懂非懂, ...
- ySQL for mac使用记录
一.登录 打开终端,输入/usr/local/mysql/bin/mysql -u root -p 初次进入mysql,密码为空.当出现mysql>提示符时,表示你已经进入mysql中.键入ex ...
- shell脚本学习积累笔记(第一篇)
(1)首先,今天在执行shell脚本./test.sh时抛出“/bin/sh^M: bad interpreter: No such file or directory”的异常,百度后,才知道这是由于 ...
- EasyUI 树形菜单tree 定义图标
{ "id":1, "text":"Folder1", "iconCls":"icon-save", ...
- Java语言基础(六)char成员变量默认初始值 最简单的Java源文件 Java的main()方法
①char成员变量的初始值是:'\u0000' ②package用来指定该文件所处的包的名称,必须位于源文件的顶端. import java.util.*; package com.hyy.test; ...