1.是否所有的消息都是以WM_开头?
否,还有
2. 消息数字的范围i,以及自定义消息
Message-identifier values are used as follows:
- The system reserves message-identifier values in the range 0x0000 through 0x03FF (the value ofWM_USER – 1) for system-defined messages. Applications cannot use these values for private messages.
- Values in the range 0x0400 (the value of WM_USER) through 0x7FFF are available for message identifiers for private window classes.
- If your application is marked version 4.0, you can use message-identifier values in the range 0x8000 (WM_APP) through 0xBFFF for private messages.
- The system returns a message identifier in the range 0xC000 through 0xFFFF when an application calls the RegisterWindowMessage function to register a message. The message identifier returned by this function is guaranteed to be unique throughout the system. Use of this function prevents conflicts that can arise if other applications use the same message identifier for different purposes.
也就是:0x0000到0x03ff(WM_USER-1)是系统保留,应用程序不能使用
0x0400(WM_USER)到0x07FF,可以在私有窗口类内使用,也就是自己的应用程序内部用
对于Windows95和NT3.5以后(前面有个Windows3.1和NT3.1,有幸都用过几天,诶,我也够老了)0x0800(WM_APP)到0xBFF也给程序自己用。
0xC000到0xFFFF是全局自定义消息,可以在不用的应用程序间使用使用。这样的消息,用户不能自己定义消息的值,而是应该调用RegisterWindowMessage ,由系统分配一个数字。所以,程序两次运行的时候,注册同一个消息,得到的数字可能是不同的,这个要注意。
3. 消息队列是窗口所有还是线程、进程所有?
The system maintains a single system message queue and one thread-specific message queue for each GUI thread.
系统有一个系统队列,然后每个GUI线程有一个属于自己的队列。
4. 消息队列何时建立?
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 specific user functions; no GUI function calls result in the creation of a message queue.
所有线程刚生成时都是没有消息队列的。当线程第一次调用特定的函数时,消息队列就被创建了。
系统消息(如键盘,鼠标,WM_QUIT, WM_PAINT)消息,首先被送到系统消息队列,然后系统一个一个的把消息从队列拿出来,看看他要送到哪个窗口,就把这个消息放到这个窗口的创建线程的队列里面。
The system creates a thread's message queue when the thread makes its first call to one of the USER or GDI functions.
5. 所有消息都被放到消息队列?
不是。分Queued message和None queued message.前者包括鼠标,键盘事件,已经WM_PAINT, WM_QUIT,WM_TIMER等。后者包括如WM_ACTIVE, WM_SETFOCUS等。None queued message的发送是通过SendMessage,这些消息的发送就是直接调用对应窗口的处理函数(说法不严谨,只有同一个线程的窗口才能直接调用;否则就是等待目标窗口完成调用。跨线程的发送就要涉及到一个Marshalling 问题)
通知消息,SendNotifyMessage 发送的消息,也不放到消息队列。这个函数的行为,在目标窗口属于发送线程时,行为和SendMessage一样,等消息处理完才返回。当目标窗口在其他线程时,行为和PostMessage一样,不等消息处理完成就返回了。
6. 消息队列有多大?
There is a limit of 10,000 posted messages per message queue. This limit should be sufficiently large. If your application exceeds the limit, it should be redesigned to avoid consuming so many system resources. To adjust this limit, modify the following registry key.
HKEY_LOCAL_MACHINE SOFTWARE Microsoft Windows NT CurrentVersion Windows USERPostMessageLimit
缺省是10000
参考:
1. http://msdn.microsoft.com/en-us/library/ms644927(v=vs.85).aspx
2. MSDN 关于AttatchThreadInput的描述
3. 参考1中相关API的MSDN文档