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

来源:互联网 发布:ping域名找不到主机 编辑:程序博客网 时间:2024/04/30 11:55

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

   否,还有

PrefixMessage categoryDocumentationABM and ABNApplication desktop toolbarShell Messages and NotificationsACM and ACNAnimation controlAnimation Control Messages and Animation Control NotificationsBCM, BCN, BM, and BNButton controlButton Control Messages and Button Control NotificationsCB and CBNComboBox controlComboBox Control Messages and ComboBox Control NotificationsCBEM and CBENComboBoxEx controlComboBoxEx Messages and ComboBoxEx NotificationsCCMGeneral controlControl MessagesCDMCommon dialog boxCommon Dialog Box MessagesDFMDefault context menuShell Messages and NotificationsDLDrag list boxDrag List Box NotificationsDMDefault push button controlDialog Box MessagesDTM and DTNDate and time picker controlDate and Time Picker Messages and Date and Time Picker NotificationsEM and ENEdit controlEdit Control Messages, Edit Control Notifications, Rich Edit Messages, and Rich Edit NotificationsHDM and HDNHeader controlHeader Control Messages and Header Control NotificationsHKMHot key controlHot Key Control MessagesIPM and IPNIP address controlIP Address Messages and IP Address NotificationsLB and LBNList box controlList Box Messages and List Box NotificationsLMSysLink controlSysLink Control MessagesLVM and LVNList view controlList View Messages and List View NotificationsMCM and MCNMonth calendar controlMonth Calendar Messages and Month Calendar NotificationsPBMProgress barProgress Bar MessagesPGM and PGNPager controlPager Control Messages and Pager Control NotificationsPSM and PSNProperty sheetProperty Sheet Messages and Property Sheet NotificationsRB and RBNRebar controlRebar Control Messages and Rebar Control NotificationsSB and SBNStatus bar windowStatus Bar Messages and Status Bar NotificationsSBMScroll bar controlScroll Bar MessagesSMCShell menuShell Messages and NotificationsSTM and STNStatic controlStatic Control Messages and Static Control NotificationsTB and TBNToolbarToolbar Control Messages and Toolbar Control NotificationsTBM and TRBNTrackbar controlTrackbar Control Messages and Trackbar Control NotificationsTCM and TCNTab controlTab Control Messages and Tab Control NotificationsTDM and TDNTask dialogTask Dialog Messages and Task Dialog NotificationsTTM and TTNTooltip controlTooltip Control Messages and Tooltip Control NotificationsTVM and TVNTree-view controlTree View Messages and Tree View NotificationsUDM and UDNUp-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文档

原创粉丝点击