这一章 实际上 是对 前几章 知识的回顾,和举例。 用一个 输出文字的例子,讲解,windows程序的结构。如果弄明白这个例子,就可以说,前几章的知识已经 弄明白了。
其中,有几个比较难理解的地方:
1、 回调函数
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
LRESULT 返回值类型,你看他的定义,就知道他实际是long 型的。
CALLBACK 用来标示这个函数是一个回调函数。回调函数,从使用的角度看,就是,函数由你定义,但是不由你调用。那么它由谁来调用
呢,它 实际由操作系统来调用。那操作系统又如何知道 这个函数的存在呢? 这就是我们在
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc= WndProc ;
。。。
从c语言的角度,这就是一个函数指针,不是么?lpfnWndProc的类型是一个函数指针,指向我们定义的WndProc 函数。
2、消息 message
消息是什么呢,不用 把它看得太神秘,我们来看看他的定义 ,比如
WM_CREATE ,他是如下定义的:
#define WM_CREATE 0x0001
原来 ,WM_CREATE 就是一个16进制数。如果还不好理解。那我就举个例子:
现在吃,喝,玩,乐,四种动作。为了便于计算机存储和传输。我们定义如下:
#define CHI 0x0001
#define HE 0x0002
#define WAN 0x0003
#define LE 0x0004
贾君鹏 ,你妈叫你回家吃饭。 我们在消息处理函数
WndProc (Message msg)
{
switch(msg)
{
case CHI :
贾君鹏回家吃饭;
break;
case HE:
贾君鹏回家喝水;
。。。。
}
}
Message 就是用来传递,操作系统 告诉 应用程序 发生了 什么事情的, 应用程序 自定义 消息处理函数,对这些消息来进行处理。道理就这么简单,当然 这些行为事先 在操作系统里都定义好了,可以在MSDN 里 查找不同消息的用途,以及在什么情况下,系统会发送这些消息。
例如 我们搜索 WM_CREATE from MSDN
WM_CREATE Notification
The WM_CREATE message is sent when an application requests that a window be created by calling the CreateWindowEx or CreateWindow function. (The message is sent before the function returns.) The window procedure of the new window receives this message after the window is created, but before the window becomes visible.
A window receives this message through its WindowProc function.