如何在基于MFC的环境中使用自定义消息函数?

来源:互联网 发布:windows字体放在哪里 编辑:程序博客网 时间:2024/06/09 18:51

祝大家平安夜快乐!Merry Christmas!

 

  引入正题,前几天解决了一个关于自定义消息的问题。对于Windows的自定义消息,我最喜欢其中的两种:

  一种是存在于窗口类内部的自定义消息,它的作用范围仅是定义它的窗口类的内部,也就是说不同的窗口类即使定义了相同的内部消息,这些消息之间也不会产生什么关联。(比如一个文本框,它的内部就存在着许多已经定义好的内部消息,这些消息帮助它实现了自身的种种功能,然而即便另外一个文本框也存在着相同的内部消息,但它们彼此是不会互相影响的),有时候我们也需要为自己的类定义这样的内部消息。通常来说,自定义一个内部消息的语句是"#define WM_MYMESSAGE WM_USER+1",这种消息的ID数值范围在0x0400~0x7FFF之间。

  另一种是某个应用程序向系统注册的自定义消息,与刚才那种内部消息不同的是当有另一个应用程序向系统注册了相同的自定义消息时,这两个应用程序就可以互相发送这样的自定义消息了。这就好像你在用SendMessage向另一个程序发送WM_KEYDOWN消息一样,只不过这个按键消息是系统为你定义好的。不难想象,这是一种进程间通信的重要手段,如果你觉得它很有用,那么就来看看我们如何使用它吧!通常来说,如果想定义一个应用程序间通信的消息,你需要用到下面这条语句
"UINT WM_MYSTRING = RegisterWindowMessage("MyString");",这种消息的ID数值范围在0xC000~0xFFFF之间。

 

Remarks

There are five ranges of message numbers:

RangeMeaning0 through WM_USER – 1Messages reserved for use by the system.WM_USER through 0x7FFFInteger messages for use by private window classes.WM_APP through 0xBFFFMessages available for use by applications.0xC000 through 0xFFFFString messages for use by applications.Greater than 0xFFFFReserved by the system for future use.

Message numbers in the first range (0 through WM_USER – 1) are defined by the system. Values in this range that are not explicitly defined are reserved for future use by the system.

 

Message numbers in the second range (WM_USER through 0x7FFF) can be defined and used by an application to send messages within a private window class. These values cannot be used to define messages that are meaningful throughout an application, because some predefined window classes already define values in this range. For example, predefined control classes such as BUTTON, EDIT, LISTBOX, and COMBOBOX may use these values. Messages in this range should not be sent to other applications unless the applications have been designed to exchange messages and to attach the same meaning to the message numbers.

 

Message numbers in the third range (0x8000 through 0xBFFF) are available for application to use as private messages. Message in this range do not conflict with system messages.

 

Message numbers in the fourth range (0xC000 through 0xFFFF) are defined at run time when an application calls the RegisterWindowMessage function to retrieve a message number for a string. All applications that register the same string can use the associated message number for exchanging messages. The actual message number, however, is not a constant and cannot be assumed to be the same between different sessions.

 

Message numbers in the fifth range (greater than 0xFFFF) are reserved for future use by the system.

 

 

综上所述:

第一类消息(消息ID从0到WM_USER–1)是系统消息;

第二类消息(消息ID从WM_USER到0x7FFF)是供窗口类内部使用的自定义消息;

第三类消息(消息ID从0x8000到0xBFFF)是供应用程序内部使用的自定义消息;

第四类消息(消息ID从0xC000到0xFFFF)是供应用程序之间使用的自定义消息;

第五类消息(消息ID大于0xFFFF)是目前保留起来的供将来使用的消息,无定义;

 

 

【未完待续】

原创粉丝点击