三种自定义消息数值范围

在CSDN上看到一贴关于自定义消息的讨论:
  【http://topic.csdn.net/u/20080417/11/9e091cfe-3978-4b73-a1bd-ff5e7d508917.html】 

第一种楼主的方法是通过WM_USER自定义消息,这个方法,比较常用,也是容易知道的。楼主的5个步骤:
    1.#define wm_user_message wm_user+int (shift)语句后不能加";"号;
    2.上述shift 为1~1000以内的 整数值;
    3.该值的选择与系统消息,窗口消息,控件消息等无关;
    4.消息映射必须在BEGIN_MESSAGE_MAPS()和END_MESSAGE_MAPS()之内;
    5.消息处理的声明以及代码添加必须在指定的类中.
是正确的,不过这也说明楼主没有系统学过MFC,对MFC了解的不是很熟悉,如果在还没有熟悉MFC的情况下,就在MFC中定义自己的消息,不能称之为一个好办法。

第二种方法是WM_APP,这个消息我没有用过,查MSDN: 

WM_APP

The WM_APP constant is used by applications to help define private messages, usually of the form WM_APP+X, where X is an integer value.  (WM_APP常量在应用程序中帮助定义私有消息,常用WM_APP+X方式定义,其中X为整数值)
#define WM_APP       0x8000

在此文后面还有消息的范围,一并摘录,便于了解:
There are five ranges of message numbers: 五种消息范围数值

Range Meaning
0 through WM_USER – 1 Messages reserved for use by the system.系统保留消息
WM_USER through 0x7FFF Integer messages for use by private window classes.私有定义窗口类整数消息
WM_APP through 0xBFFF Messages available for use by applications.应用程序可获取消息
0xC000 through 0xFFFF String messages for use by applications.应用程序字符串消息
Greater than 0xFFFF Reserved by the system for future use.保留消息

从这儿可以看到,楼主的整数说明不是很确切了,而WM_USER与WM_APP的使用范围也是有差别的,其应用范围也是不一样的。WM_USER用在窗口消息中,而WM_APP用在程序消息中,当然由于是自己定义的消息,自己爱怎么用就怎么用,不过,如果想管理好自己的代码,建议按MSDN推荐方式做。

第三种方法:RegisterWindowMessage
RegisterWindowMessage

The RegisterWindowMessage function defines a new window message that is guaranteed to be unique throughout the system. The returned message value can be used when calling the SendMessage or PostMessage function.  (RegisterWindowMessage函数定义了新的在系统内唯一的窗口消息,这个消息可以在SendMessage和PostMessage函数中使用)

UINT RegisterWindowMessage(
  LPCTSTR lpString   // address of message string
);

Parameters
lpString

Pointer to a null-terminated string that specifies the message to be registered. (指向NULL结尾的指定消息注册的字符串)

Return Values

If the message is successfully registered, the return value is a message identifier in the range 0xC000 through 0xFFFF.(如果消息成功注册,返回值将在0xC000和0xFFFF之间。
If the function fails, the return value is zero. To get extended error information, call< id="alink_1" type="application/x-oleobject" classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11"> GetLastError.
如果函数失败,返回值为0,获取下扩展消息信息,调用GetLastError,

在上面的消息数值范围表格中我们可以知道,这个消息是处于string消息范围之中的。

综上所述,这三个自定义消息都是可以使用的,而且是三个不同数值范围要求的三个消息。

/*
*
* Copyright (c) 2011 Ubunoon.
* All rights reserved.
*
* email: netubu#gmail.com replace '#' to '@'
* http://www.cnblogs.com/ubunoon
* 欢迎来邮件定制各类验证码识别,条码识别,图像处理等软件
* 推荐不错的珍珠饰品,欢迎订购 * 宜臣珍珠(淡水好珍珠) */
原文地址:https://www.cnblogs.com/ubunoon/p/2058915.html