MFC resource.h 命令/消息 ID

2022-01-12 00:00:00 command resources messages mfc

我正在开发一个 MFC 应用程序,该应用程序多年来和不同的开发团队都变得非常混乱.包含所有命令/消息映射的 resource.h 文件随着时间的推移变得相当大,并且有很多问题(例如重复的 ID).我不精通 MFC,所以这个问题听起来可能很愚蠢......

I'm working on an MFC application, that got pretty messy over years and over different teams of developers. The resource.h file, which contains all command/message mappings grew pretty big over time, and has lots of problems (like duplicate IDs). I am not proficient with MFC, so the question might sound pretty stupid...

MSDN 文档提到命令 ID 和消息 ID 不应相应小于 WM_USER 和 WM_APP.我看到 Visual Studio 生成的 resource.h 中的大多数命令 ID 开始于 100 左右.这不应该导致一些与应用程序定义的 ID 重叠的 MFC/Windows 命令和消息的干扰吗?例如,我有一个命令 ID:

MSDN docs mention that Command IDs and Message IDs should not be less than WM_USER and WM_APP correspondingly. I saw that most of the command IDs in resource.h generated by Visual Studio begin around 100. Shouldn't this cause some interfering with MFC/Windows commands and messages, that overlap with the application defined IDs? For example, I have a command ID :

#define ID_MY_ID 101

并且有一个具有相同 ID 的 windows 命令.当 MC 向 APP 发送此命令时,它就像应用程序定义的 ID_MY_ID 一样处理,并且应用程序正在执行不必要的操作.这是一种可能的情况吗?

and there is a windows command that has the same ID. When MC send this command to the APP, it's handled like an application defined ID_MY_ID, and the app is taking unnecessary actions. Is it a possible scenario?

另外,是否有一些第三方工具可以帮助分析项目资源?

Also, is there some third party tool that helps to profile the project resources?

更新 1:

出现了新问题:向应用程序类添加新的自定义命令的首选方式是什么?据我了解,之前它们是通过以下方式添加的:将命令ID添加到resouce.h,然后将消息映射处理程序添加到处理类.

New question showed up: What is the preferred way of adding new custom commands to the application classes? As I understood, before they were added in the following way: add a command ID to the resouce.h, and then add a message map handler to the handling class.

推荐答案

你混合了两件事:

  1. 消息 ID.这些必须大于 WM_USER.消息 ID 未在 resource.h 中定义.从您的描述看来,您没有使用应用程序私人消息.
  2. 命令 ID.您的应用程序本身不能有重复的命令 ID.命令 ID 值也不应干扰 afxres.h 中定义的标准 MFC ID.这些命令 ID 从 0xE100 开始,因此 resource.h 中的值不太可能.资源编译器会为您的 rc 文件中的重复 ID 生成错误

您可能不需要手动编辑 resource.h.

There is probably no need for you to edit resource.h manually.

我建议使用资源符号"工具(在资源视图中右键单击资源并从弹出菜单中选择,我假设您使用的是 VC++),从 resource.h 中删除所有未使用的 ID.

I would recommend to use the "Resource symbols" tool (right click on the resources in resource view and choose from the popup menu, I assume you are using VC++), to remove all the unused IDs from resource.h.

相关文章