Microsoft KB Archive/199431

= PRB: Enabling Menu Mnemonics in an MFC ActiveX Control =

Article ID: 199431

Article Last Modified on 3/27/2002

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 5.0 Enterprise Edition

 Microsoft Visual C++ 6.0 Enterprise Edition

 Microsoft Visual C++ 5.0 Professional Edition

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2002 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q199431

<div class="symptoms_section">

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.

<div class="cause_section">

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

<div class="resolution_section">

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. <ol> Use ClassWizard to override the PreTranslateMessage function for the control.</li>  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); </li></ol>

Adding the earlier function to the PreTranslateMessage function allows the container to process menu mnemonics and forward the resulting command messages to the control.

<div class="status_section">

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.

<div class="moreinformation_section">

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.

<div class="references_section">