Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A few moments ago I had a similar problem - a standard MFC menu bar containing at least one submenu, and the need to be able to add a check mark to the <em>submenu</em> parent item, when any of the submenu <em>child</em> items were checked.</p> <p>The easiest solution (for me) turned out to be as simple as performing the update in the standard OnUpdateMenuItem(CCmdUI* pCmdUI) call. In my case I used ON_UPDATE_COMMAND_UI_RANGE() to feed a bunch of menu IDs into the same update call, but the principal is the same for a single ON_UPDATE_COMMAND_UI() map.</p> <p>The code I used (edited to be more easily inserted into other people's work) is:</p> <pre><code>void CMyApp::OnUpdateMenu(CCmdUI* pCmdUI) { // Note, a submenu parent (which has no editable ID in the resource editor) has the SAME ID as the first child item if (pCmdUI-&gt;m_nID == ID_FIRST_CHILD_MENU &amp;&amp; pCmdUI-&gt;m_pSubMenu != NULL) { // Get the child menu so we can see if any child items are checked CMenu* pSubMenu = pCmdUI-&gt;m_pSubMenu; BOOL fChildChecked = FALSE; for (UINT i = 0; !fChildChecked &amp;&amp; i &lt; pSubMenu-&gt;GetMenuItemCount(); ++i) { // Do something to decide if this child item should be checked... UINT nChildID = pSubMenu-&gt;GetMenuItemID(i); fChildChecked = IsThisChildChecked(nChildID); } // The POSITION of the current menu item is stored in pCmdUI-&gt;m_nIndex CMenu* pMenu = pCmdUI-&gt;m_pMenu; UINT flags = MF_BYPOSITION; if (fActiveChild) flags |= MF_CHECKED; pMenu-&gt;CheckMenuItem(pCmdUI-&gt;m_nIndex, flags); } // Set the enabled state of the menu item as you see fit... pCmdUI-&gt;Enable(TRUE); } </code></pre> <p><em>Et voilà</em> the submenu item automagically gains a check mark when any of its child menu items has a check mark.</p> <p>Hope this helps others looking for similar solutions!</p> <p>John</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload