关于Windows消息队列的几个问题

1.是否所有的消息都是以WM_开头?

   否,还有

Prefix Message category Documentation
ABM and ABN Application desktop toolbar Shell Messages and Notifications
ACM and ACN Animation control Animation Control Messages and Animation Control Notifications
BCM, BCN, BM, and BN Button control Button Control Messages and Button Control Notifications
CB and CBN ComboBox control ComboBox Control Messages and ComboBox Control Notifications
CBEM and CBEN ComboBoxEx control ComboBoxEx Messages and ComboBoxEx Notifications
CCM General control Control Messages
CDM Common dialog box Common Dialog Box Messages
DFM Default context menu Shell Messages and Notifications
DL Drag list box Drag List Box Notifications
DM Default push button control Dialog Box Messages
DTM and DTN Date and time picker control Date and Time Picker Messages and Date and Time Picker Notifications
EM and EN Edit control Edit Control MessagesEdit Control NotificationsRich Edit Messages, and Rich Edit Notifications
HDM and HDN Header control Header Control Messages and Header Control Notifications
HKM Hot key control Hot Key Control Messages
IPM and IPN IP address control IP Address Messages and IP Address Notifications
LB and LBN List box control List Box Messages and List Box Notifications
LM SysLink control SysLink Control Messages
LVM and LVN List view control List View Messages and List View Notifications
MCM and MCN Month calendar control Month Calendar Messages and Month Calendar Notifications
PBM Progress bar Progress Bar Messages
PGM and PGN Pager control Pager Control Messages and Pager Control Notifications
PSM and PSN Property sheet Property Sheet Messages and Property Sheet Notifications
RB and RBN Rebar control Rebar Control Messages and Rebar Control Notifications
SB and SBN Status bar window Status Bar Messages and Status Bar Notifications
SBM Scroll bar control Scroll Bar Messages
SMC Shell menu Shell Messages and Notifications
STM and STN Static control Static Control Messages and Static Control Notifications
TB and TBN Toolbar Toolbar Control Messages and Toolbar Control Notifications
TBM and TRBN Trackbar control Trackbar Control Messages and Trackbar Control Notifications
TCM and TCN Tab control Tab Control Messages and Tab Control Notifications
TDM and TDN Task dialog Task Dialog Messages and Task Dialog Notifications
TTM and TTN Tooltip control Tooltip Control Messages and Tooltip Control Notifications
TVM and TVN Tree-view control Tree View Messages and Tree View Notifications
UDM and UDN Up-down control

Up-Down Messages and Up-Down Notifications

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文档

posted on 2011-10-03 15:34  四-儿  阅读(652)  评论(0编辑  收藏  举报

导航