manianis manianis - 1 month ago 17
C++ Question

How to make MFC Ribbon Statusbar act like the regular CStatusBar

The first image shows what I want to do. The second one shows what I get.

I want the status bar to show the state of the special keys CAPS LOCK, NUM LOCK and SCROLL LOCK. This image is from an old project using the CStatusBar.
This snapshot is from CMainFrame class.



This image shows how I am doing it in Visual C++ 2015 MFC and the results I get. This snapshot is from CMainFrame class, too.



My question is : Can anyone tell me how my application can intercept the change from toggle buttons CAPS LOCK and NUM LOCK then update the user interface.

I tried to use this code but it doesn't work :

ON_UPDATE_COMMAND_UI(ID_INDICATOR_STYLE, &CMainFrame::OnUpdateStyle)
ON_UPDATE_COMMAND_UI(ID_INDICATOR_FOLD, &CMainFrame::OnUpdateFold)
ON_UPDATE_COMMAND_UI(ID_INDICATOR_OVR, &CMainFrame::OnUpdateInsert)


Thanks in advance.

Answer

First of all the standard behavior of CStatusBar is not supported by CMFCRibbonStatusBar.

The good news is that it is very easy to implement the same behavior. Here is what you have to do in order to implement it in your application:

Add this to message map of your main frame class:

ON_UPDATE_COMMAND_UI(ID_INDICATOR_CAPS, &CMainFrame::OnUpdateButtonsState)
ON_UPDATE_COMMAND_UI(ID_INDICATOR_NUM, &CMainFrame::OnUpdateButtonsState)
ON_UPDATE_COMMAND_UI(ID_INDICATOR_SCRL, &CMainFrame::OnUpdateButtonsState)

The actual update handler should look like this:

void CMainFrame::OnUpdateButtonsState(CCmdUI* pCmdUI)
{
    UINT nVK;
    UINT flag = 0x0001;

    switch (pCmdUI->m_nID)
    {
    case ID_INDICATOR_CAPS:
        nVK = VK_CAPITAL;
        break;

    case ID_INDICATOR_NUM:
        nVK = VK_NUMLOCK;
        break;

    case ID_INDICATOR_SCRL:
        nVK = VK_SCROLL;
        break;

    case ID_INDICATOR_KANA:
        nVK = VK_KANA;
        break;

    default:
        TRACE(traceAppMsg, 0, "Warning: OnUpdateKeyIndicator - unknown indicator 0x%04X.\n",
            pCmdUI->m_nID);
        pCmdUI->ContinueRouting();
        return; // not for us
    }

    pCmdUI->SetCheck(::GetKeyState(nVK) & flag);        

}