PRB: Enabling Menu Mnemonics in an MFC ActiveX Control (199431)



The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), when used with:
    • Microsoft Visual C++, 32-bit Enterprise Edition 5.0
    • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
    • Microsoft Visual C++, 32-bit Professional Edition 5.0
    • Microsoft Visual C++, 32-bit Professional Edition 6.0
    • Microsoft Visual C++, 32-bit Learning Edition 6.0
    • Microsoft Visual C++ .NET (2002)

This article was previously published under Q199431

SYMPTOMS

MFC supports the creation of ActiveX controls that can merge a menu resource with its container application though the COleControl::OnGetInPlaceMenu() function, as illustrated by the MFC Button sample. However, you may find that menu items belonging to an MFC ActiveX control cannot be invoked through the keyboard.

CAUSE

The default implementation of an MFC ActiveX control does not give the container application a chance to process menu mnemonics.

RESOLUTION

To work around this problem, you need to give the container an opportunity to translate menu mnemonics by calling the OleTranslateAccelerator() from the control's PreTranslateMessage() function. You can use the following steps to do this.
  1. Use ClassWizard to override the PreTranslateMessage function for the control.
  2. Use the following code in the PreTranslateMessage function:
      if (pMsg->message >= WM_KEYFIRST && pMsg->message <= WM_KEYLAST)
         if (::OleTranslateAccelerator(m_pInPlaceFrame, &m_frameInfo, pMsg)==S_OK)
            return TRUE;
    
       return COleControl::PreTranslateMessage(pMsg);
    						
Adding the earlier function to the PreTranslateMessage function allows the container to process menu mnemonics and forward the resulting command messages to the control.

STATUS

The MFC implementation at this time does not forward keyboard requests on for menu mnemonics. This is a limitation in the MFC framework that can be worked around with the resolution earlier.

MORE INFORMATION

Steps to reproduce behavior

  1. Build and register the MFC Button sample.
  2. Run the ActiveX Test Container utility. You can find this from tool on the Tools menu inside of Microsoft Visual C++. Insert the Button control into the ActiveX Test Container. Note the container's menus have changed.
  3. Type ALT+H to drop down the Help menu.
  4. Type A to invoke the control's about box.
Note that nothing happens. However, when you select the menu item using the mouse, the control's about box is properly displayed.

REFERENCES

For additional information concerning handling accelerators and menu mnemonics for in-place active objects, such as ActiveX controls, please consult the following sources of information:

"Inside OLE" - second edition by Kraig Brockschmidt. Microsoft Press. ISBN 1-55615-843-2.

104460 PRB: Menu Mnemonics Not Working During In-Place Activation.


Modification Type:MajorLast Reviewed:3/27/2002
Keywords:kbAcceleratorKey kbContainer kbCtrlCreate kbDSupport kbKeyAccel kbprb KB199431