How to get notification for keydown for CMFCRibbon

2019-08-06 02:47发布

问题:

I have CMFCRibbonComboBox on ribonbar and I want when that user press on a key open droplist and select Item acurding to chars that press by user.

For this purpose I want to get notification for keydown.

How can I to do it? Thanks

回答1:

I asked a very similar question on MSDN here and eventually solved it myself with the following hack;

Save a local copy of C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\atlmfc\src\mfc\afxribbonedit.cpp to your project

In BOOL CMFCRibbonRichEditCtrl::PreTranslateMessage(MSG* pMsg) replace this

    case VK_DOWN:
        if (m_edit.m_bHasDropDownList && !m_edit.IsDroppedDown())
        {
            m_edit.DropDownList();
            return TRUE;
        }

with this

case VK_DOWN:
        if (m_edit.m_bHasDropDownList && !m_edit.IsDroppedDown())
        {
            m_edit.DropDownList();
            CMFCRibbonBaseElement* pRibbonBaseElement = m_edit.GetDroppedDown();
            if (pRibbonBaseElement && (pRibbonBaseElement->IsKindOf(RUNTIME_CLASS(CMFCRibbonComboBox))))
            {
                CString str;
                GetWindowText(str);
                CMFCRibbonComboBox *pCombo = (CMFCRibbonComboBox*)pRibbonBaseElement;
                int ItemNo = -1;
                for (int i = 0; i < pCombo->GetCount(); i++)
                {
                    CString ItemText = pCombo->GetItem(i);
                    if (ItemText.Left(str.GetLength()).CompareNoCase(str) == 0)
                    {
                        ItemNo = i;
                        break;
                    }
                }
                if (ItemNo != -1)
                {
                    pCombo->OnSelectItem(ItemNo);
                    // Draw and redraw dropdown for selection to show
                    m_edit.DropDownList();
                    m_edit.DropDownList();
                }
            }
            return TRUE;
        }

For drop lists (as opposed to drop downs) you can similarly hand WM_CHAR to do a first letter search based on the next item after the current position. Note that the above hack would need to be checked against any future updates to the ribbon library and should be dumped once it has been properly implemented in the library.