locked
How to initialize the object of class CCmdUI? RRS feed

  • Question

  • I have been browsing about this and found this link.

    But I couldn't find anywhere about how to initialize the objects of class CCmdUI. Can someone help me out with this?

    If I get to know how to initialize this then I can call the function

    CMyWinApp::OnUpdateEditClearAll(CCmdUI *pCmdUI) in which I can enable

    the respective object


    • Edited by Abejoe Monday, January 11, 2016 9:16 AM
    Monday, January 11, 2016 9:07 AM

Answers

  • You should follow the MFC framework process, the framework passes the handler a pointer to a CCmdUI object (or to an object of a CCmdUI-derived class.
    If you enable/disable/set text menu item, you should add and handle ON_UPDATE_COMMAND_UI()

    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    Monday, January 11, 2016 10:45 AM
  • @

    Avatar of VisualEleven

    VisualEleven

    Well actually i have a start and stop button. On clicking the start button, a set of operations take place. I need the stop button to be highlighted when the start button is pressed but its not happening in my case. I have used

    ON_UPDATE_COMMAND_UI(ID OF THE MENU ITEM, FUNCTION WHERE THE STOP BUTTON GETS ENABLED).

    But its not working

    can you tell me the sequence in which i need to relate the handlers and classes?


    You are not understanding how the UPDATE_UI mechanism works. The COMMAND_UI handlers are called automatically by the framework; you do not call them in your code. They get called when you launch a menu, and also in the background (during idle time) in order to update toolbar buttons. In these update handlers you should merely set the state of the corresponding menu item or toolbar button, using the CCmdUI::Enable() method.

    David Wilkinson | Visual C++ MVP

    Tuesday, January 12, 2016 10:44 AM

All replies

  • You should follow the MFC framework process, the framework passes the handler a pointer to a CCmdUI object (or to an object of a CCmdUI-derived class.
    If you enable/disable/set text menu item, you should add and handle ON_UPDATE_COMMAND_UI()

    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    Monday, January 11, 2016 10:45 AM
  • // In CFrameWnd::OnInitMenuPopup() function, you can see it.
    Route:
    OnInitMenuPopup()->CCmdUI::DoUpdate()->OnCmdMsg()-> ... ->message handler function

    void CFrameWnd::OnInitMenuPopup(CMenu* pMenu, UINT nIndex, BOOL bSysMenu) { AfxCancelModes(m_hWnd); if (bSysMenu) return; // don't support system menu #ifndef _WIN32_WCE // OLE #ifndef _AFX_NO_OLE_SUPPORT // allow hook to consume message if (m_pNotifyHook != NULL && m_pNotifyHook->OnInitMenuPopup(pMenu, nIndex, bSysMenu)) { return; } #endif #else (nIndex); #endif // !_WIN32_WCE ENSURE_VALID(pMenu); // check the enabled state of various menu items CCmdUI state; state.m_pMenu = pMenu; ASSERT(state.m_pOther == NULL); ASSERT(state.m_pParentMenu == NULL); // determine if menu is popup in top-level menu and set m_pOther to // it if so (m_pParentMenu == NULL indicates that it is secondary popup) HMENU hParentMenu; if (AfxGetThreadState()->m_hTrackingMenu == pMenu->m_hMenu) state.m_pParentMenu = pMenu; // parent == child for tracking popup #ifndef _WIN32_WCE else if ((hParentMenu = (m_dwMenuBarState == AFX_MBS_VISIBLE) ? ::GetMenu(m_hWnd) : m_hMenu) != NULL) #else else if ((hParentMenu = ::GetMenu(m_hWnd)) != NULL) #endif //!_WIN32_WCE { CWnd* pParent = GetTopLevelParent(); // child windows don't have menus -- need to go to the top! if (pParent != NULL && #ifndef _WIN32_WCE (hParentMenu = pParent->GetMenu()->GetSafeHmenu()) != NULL) #else (hParentMenu = ::GetMenu(pParent->m_hWnd)) != NULL) #endif //!_WIN32_WCE { int nIndexMax = ::GetMenuItemCount(hParentMenu); for (int nItemIndex = 0; nItemIndex < nIndexMax; nItemIndex++) { if (::GetSubMenu(hParentMenu, nItemIndex) == pMenu->m_hMenu) { // when popup is found, m_pParentMenu is containing menu state.m_pParentMenu = CMenu::FromHandle(hParentMenu); break; } } } } state.m_nIndexMax = pMenu->GetMenuItemCount(); for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax; state.m_nIndex++) { state.m_nID = pMenu->GetMenuItemID(state.m_nIndex); if (state.m_nID == 0) continue; // menu separator or invalid cmd - ignore it ASSERT(state.m_pOther == NULL); ASSERT(state.m_pMenu != NULL); if (state.m_nID == (UINT)-1) { // possibly a popup menu, route to first item of that popup state.m_pSubMenu = pMenu->GetSubMenu(state.m_nIndex); if (state.m_pSubMenu == NULL || (state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 || state.m_nID == (UINT)-1) { continue; // first item of popup can't be routed to } state.DoUpdate(this, FALSE); // popups are never auto disabled } else { // normal menu item // Auto enable/disable if frame window has 'm_bAutoMenuEnable' // set and command is _not_ a system command. state.m_pSubMenu = NULL; state.DoUpdate(this, m_bAutoMenuEnable && state.m_nID < 0xF000); } // adjust for menu deletions and additions UINT nCount = pMenu->GetMenuItemCount(); if (nCount < state.m_nIndexMax) { state.m_nIndex -= (state.m_nIndexMax - nCount); while (state.m_nIndex < nCount && pMenu->GetMenuItemID(state.m_nIndex) == state.m_nID) { state.m_nIndex++; } } state.m_nIndexMax = nCount; } }



    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.



    Monday, January 11, 2016 10:59 AM
  • @

    Avatar of VisualEleven

    VisualEleven

    Well actually i have a start and stop button. On clicking the start button, a set of operations take place. I need the stop button to be highlighted when the start button is pressed but its not happening in my case. I have used

    ON_UPDATE_COMMAND_UI(ID OF THE MENU ITEM, FUNCTION WHERE THE STOP BUTTON GETS ENABLED).

    But its not working

    can you tell me the sequence in which i need to relate the handlers and classes?


    • Edited by Abejoe Monday, January 11, 2016 11:16 AM
    Monday, January 11, 2016 11:08 AM
  •  I need the stop button to be highlighted when the start button is pressed...
    -----------------------------------------------------------------------------------------
    You should owner draw button(BS_OWNERDRAW style), derived new class from CButton class, add and handle DrawItem() virtual function in new class.

    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    Tuesday, January 12, 2016 2:29 AM
  • @

    Avatar of VisualEleven

    VisualEleven

    Well actually i have a start and stop button. On clicking the start button, a set of operations take place. I need the stop button to be highlighted when the start button is pressed but its not happening in my case. I have used

    ON_UPDATE_COMMAND_UI(ID OF THE MENU ITEM, FUNCTION WHERE THE STOP BUTTON GETS ENABLED).

    But its not working

    can you tell me the sequence in which i need to relate the handlers and classes?


    You are not understanding how the UPDATE_UI mechanism works. The COMMAND_UI handlers are called automatically by the framework; you do not call them in your code. They get called when you launch a menu, and also in the background (during idle time) in order to update toolbar buttons. In these update handlers you should merely set the state of the corresponding menu item or toolbar button, using the CCmdUI::Enable() method.

    David Wilkinson | Visual C++ MVP

    Tuesday, January 12, 2016 10:44 AM