Chapter3 Windows and Messages —— Programming Windows 笔记

通过一个简单的Windows程序HELLOWIN,简要地介绍了Windows编程的基本框架,其中包含窗口、窗口类、窗口过程、消息、消息循环、消息队列等概念,以及Windows如何通过相关的API对应用程序提供强大的支持。以下是我摘抄的若干段落,我觉得它们比较有思考价值。

In object-oriented programming, an object is a combination of code and data. A window is an object. The code is the window procedure. The data is information retained by the window procedure and information retained by Windows for each window and window class that exists in the system.

在面向对象编程里,一个对象是代码和数据的一个组合。一个窗口是一个对象。它的代码是窗口过程。它的数据是由窗口过程维护的的信息和操作系统为存在系统中的每个窗口和窗口类维护的信息。

The second parameter is the same as the message field in the MSG structure. It's a number that identifies the message. The last two parameters are 32-bit message parameters that provide more information about the message. What these parameters contain is specific to each type of message. Sometimes a message parameter is two 16-bit values stuck together, and sometimes a message parameter is a pointer to a text string or to a data structure.

第二个参数和MSG结构体里的message字段是一样的,它是一个标识消息的整数。最后两个参数都是32位的消息参数,它们提供关于消息的更多信息。这两个参数包含的内容由消息的类型来定,一个消息参数有时是两个16位数值拼凑的,有时是一个指向字符串或者结构体的指针。

(有助于更好地理解消息。)

Programs generally don't call window procedures directly. The window procedure is almost always called from Windows itself. A program can indirectly call its own window procedure by calling a function named SendMessage, which we'll examine in later chapters.

窗口过程一般不被应用程序直接调用,而总是由操作系统自己调用。不过,应用程序可以间接地通过一个叫做SendMessage的函数调用窗口过程,我们将在后续章节讲到。

The very first message that a window procedure receives—and the first that HELLOWIN's WndProc chooses to process—is WM_CREATE. WndProc receives this message while Windows is processing the CreateWindow function in WinMain. That is, when HELLOWIN calls CreateWindow, Windows does what it has to do and, in the process, Windows calls WndProc with the first argument set to the window handle and the second argument set to WM_CREATE (the value 1). WndProc processes the WM_CREATE message and returns controls back to Windows. Windows can then return to HELLOWIN from the CreateWindow call to continue further progress in WinMain.

一个窗口过程收到的第一个消息,也是HELLOWINWndProc选择要处理的第一个消息,就是WM_CREATEWndProc是在Windows处理WinMain中的CreateWindow函数的时候接收到这个消息的。那就是说,当HELLOWIN调用CreateWindow的时候,WindowsCreateWindow需要做的事情,而在这个过程中,Windows调用了WndProc,并将WndProc需要的第一个参数设置为窗口的句柄,第二个参数设置为WM_CREATE(值为1),WndProc处理完WM_CREATE消息之后返回并将控制权还给WindowsWindows然后从CreateWindow函数中返回到HELLOWIN来继续WinMain后续的事情。

(有助于理解Windows与应用程序的关系,以及窗口过程被调用的(非队列消息情况)过程。)

How does a client area become invalid? When the window is first created, the entire client area is invalid because the program has not yet drawn anything on the window. The first WM_PAINT message (which normally occurs when the program calls UpdateWindow in WinMain) directs the window procedure to draw something on the client area.

怎么使得一个客户区域变成无效的呢?当窗口刚被创建的时候,由于应用程序还没有在窗口里绘制任何东西,整个客户区域都是无效的。第一个WM_PAINT消息(一般在应用程序在WinMain中调用UpdateWindow的时候出现)指引窗口过程在客户区域绘制一些东西。

(由于这个时候还没有进入消息循环,所以即使在消息队列中存在WM_PAINT消息,也没有办法让WndProc处理,所以需要使用非队列消息的方式,即通过UpdateWindow,在UpdateWindow的执行过程中必然直接或者间接地调用了WndProc并传递WM_PAINT消息作为第二个参数。)

Windows sends a message to the window by calling the window procedure.

操作系统通过发送消息给窗口来调用窗口过程。

(注意这里的用词:Sendcalling,前面已经提高SendMessage里面直接调用了WndProc)

Messages can be either "queued" or "nonqueued." The queued messages are those that are placed in a program's message queue by Windows. In the program's message loop, the messages are retrieved and dispatched to the window procedure. The nonqueued messages are the results of calls by Windows directly to the window procedure. It is said that queued messages are "posted" to a message queue and that nonqueued messages are "sent" to the window procedure. In any case, the window procedure gets all the messages—both queued and nonqueued—for the window. The window procedure is "message central" for the window.

消息可以是“队列的”或者“非队列的”。队列消息是被Windows操作系统放到应用程序的消息队列中的。在应用程序的消息循环中,队列消息被取回并分发给窗口过程。非队列消息是Windows操作系统对窗口过程的直接调用结果。有人说,队列消息是被“投递”到一个消息队列里而非队列消息是被“传递”到窗口过程。不管怎么样,窗口过程获得所有发给窗口的消息,包括队列的和非队列的。窗口过程是窗口的“消息中心”。

(队列消息与非队列消息的区别就是对WndProc的调用过程不一样,前者是经过消息循环里的DispatchMessage处理并间接调用WndProc,而后者则不需要经过消息循环立即直接或者间接地调用WndProc)

When I say that messages come through in an orderly and synchronized manner, I mean first that messages are not like hardware interrupts. While processing one message in a window procedure, the program will not be suddenly interrupted by another message.

当我说消息是按照有序的和同步的方式处理的,我的意思首先是消息不像硬件中断。当在窗口过程中处理一个消息的时候不会被另一个消息突然中断。

Although Windows programs can have multiple threads of execution, each thread's message queue handles messages for only the windows whose window procedures are executed in that thread. In other words, the message loop and the window procedure do not run concurrently. When a message loop retrieves a message from its message queue and calls DispatchMessage to send the message off to the window procedure, DispatchMessage does not return until the window procedure has returned control back to Windows.

虽然Windows程序可以有多线程执行,每一个线程的消息队列只为窗口过程在这个线程中执行的窗口管理消息。换一句话讲,消息循环和窗口过程不会并行运行。当一个消息循环从它的消息队列取回一个消息,然后调用DispatchMessage传递消息给窗口过程,直到窗口过程已经将控制返还给Windows之后,DispatchMessage才返回。

(这个说法有一定的问题,相关的知识见下面摘抄的msdn相关内容。)

 

The system maintains a single system message queue and one thread-specific message queue for each graphical user interface (GUI) thread. To avoid the overhead of creating a message queue for non–GUI threads, all threads are created initially without a message queue. The system creates a thread-specific message queue only when the thread makes its first call to one of the User or Windows Graphics Device Interface (GDI) functions.

Whenever the user moves the mouse, clicks the mouse buttons, or types on the keyboard, the device driver for the mouse or keyboard converts the input into messages and places them in the system message queue. The system removes the messages, one at a time, from the system message queue, examines them to determine the destination window, and then posts them to the message queue of the thread that created the destination window. A thread's message queue receives all mouse and keyboard messages for the windows created by the thread. The thread removes messages from its queue and directs the system to send them to the appropriate window procedure for processing.

An application must remove and process messages posted to the message queues of its threads. A single-threaded application usually uses a message loop in its WinMain function to remove and send messages to the appropriate window procedures for processing. Applications with multiple threads can include a message loop in each thread that creates a window.

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值