ON_UPDATE_COMMAND_UI When Update Handlers Are Called

转载自:https://docs.microsoft.com/zh-cn/cpp/mfc/user-interface-objects-and-command-ids?view=vs-2017

Suppose the user clicks the mouse in the File menu, which generates a WM_INITMENUPOPUP message. The framework's update mechanism collectively updates all items on the File menu before the menu drops down so the user can see it.

To do this, the framework routes update commands for all menu items in the pop-up menu along the standard command routing. Command targets on the routing have an opportunity to update any menu items by matching the update command with an appropriate message-map entry (of the form ON_UPDATE_COMMAND_UI) and calling an "update handler" function. Thus, for a menu with six menu items, six update commands are sent out. If an update handler exists for the command ID of the menu item, it is called to do the updating. If not, the framework checks for the existence of a handler for that command ID and enables or disables the menu item as appropriate.

If the framework does not find an ON_UPDATE_COMMAND_UI entry during command routing, it automatically enables the user-interface object if there is an ON_COMMAND entry somewhere with the same command ID. Otherwise, it disables the user-interface object. Therefore, to ensure that a user-interface object is enabled, supply a handler for the command the object generates or supply an update handler for it. See the figure in the topic User-Interface Objects and Command IDs.

It is possible to disable the default disabling of user-interface objects. For more information, see the m_bAutoMenuEnablemember of class CFrameWnd in the MFC Reference.

Menu initialization is automatic in the framework, occurring when the application receives a WM_INITMENUPOPUP message.During the idle loop, the framework searches the command routing for button update handlers in much the same way as it does for menus.

假设用户使用鼠标在文件菜单中,将生成 WM_INITMENUPOPUP 消息。 在菜单下拉以便用户可看到它之前,框架的更新机制将统一更新“文件”菜单中的所有项。

为此,在标准命令路由过程中,框架路由将更新弹出菜单中所有菜单项的命令。 路由上的命令目标有机会更新任何菜单项,方式为将更新命令与适当的消息映射项(属于 ON_UPDATE_COMMAND_UI 窗体)匹配并调用“更新处理程序”函数。 因此,对于具有 6 个菜单项的菜单,将发出 6 条更新命令。如果菜单项的命令 ID 存在更新处理程序,则将调用其来更新。 如果不存在,则框架将检查是否存在此命令 ID 的处理程序并启用或禁用菜单项(根据需要)。

如果框架在命令路由期间未找到 ON_UPDATE_COMMAND_UI 项,则在某个位置存在具有相同命令 ID 的 ON_COMMAND 项时,框架将自动启用用户界面对象。 否则,它将禁用用户界面对象。 因此,若要确保启用用户界面对象,请为对象生成的命令提供一个处理程序或为之提供一个更新处理程序。 请参阅本主题中的图形用户界面对象和命令 Id

可禁用用户界面对象的默认禁用。 有关详细信息,请参阅m_bAutoMenuEnable类的成员CFrameWndMFC 参考

菜单初始化是自动在 framework 中,应用程序收到 WM_INITMENUPOPUP 消息时所发生的。 在空闲循环期间,框架将通过其为菜单执行的同一方式为按钮更新处理程序搜索命令路由。

猜你喜欢

转载自blog.csdn.net/daizhiyan1/article/details/85114639