Microsoft KB Archive/183167

= PRB: TranslateAccelerator Not Called for ActiveX Controls =

Article ID: 183167

Article Last Modified on 9/22/2003

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 4.0 Standard Edition

 Microsoft Visual C++ 4.1 Subscription

 Microsoft Visual C++ 4.2 Enterprise Edition

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

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

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

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

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

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

 Microsoft Visual C++ .NET 2003 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 Q183167

<div class="symptoms_section">

SYMPTOMS
An ActiveX control can process keyboard messages in the IOleInPlaceActiveObject::TranslateAccelerator function. If the ActiveX control is created dynamically in a CView class or a CPropertyPage class, TranslateAccelerator will not be called.

<div class="cause_section">

CAUSE
The CView and CPropertyPage classes are not designed to host ActiveX controls, therefore they do not call the TranslateAccelerator function.

<div class="resolution_section">

RESOLUTION
Override the PreTranslateMessage function and call the PreTranslateInput function. This results in a call to the COccManager::IsDialogMessage function, which calls the control's IOleInPlaceActiveObject::TranslateAccelerator function.

The following sample code shows how to override PreTranslateMessage in a CView-derived class called CTestView:

Sample Code
BOOL CTestView::PreTranslateMessage(MSG* pMsg) {     CView::PreTranslateMessage(pMsg); return PreTranslateInput (pMsg); }

<div class="status_section">

STATUS
This behavior is by design.

<div class="moreinformation_section">

Steps to Reproduce Behavior
<ol> Create an ActiveX control project with the MFC ActiveX ControlWizard.</li>  In the WM_CREATE message handler, create two edit controls as child windows of the ActiveX control, as in the following example: // CEditOCXCtrl is a COleControl-derived class. int CEditOCXCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) {        if (COleControl::OnCreate(lpCreateStruct) == -1) return -1;

// m_Edit1 and m_Edit2 are of type CEdit. VERIFY(m_Edit1.Create(WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, CRect(10, 10, 100, 40), this, 100)); VERIFY(m_Edit2.Create(WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, CRect(10, 50, 100, 80), this, 101)); return 0; }                       </li> <li> Override PreTranslateMessage in the COleControl-derived class as follows: BOOL CEditOCXCtrl::PreTranslateMessage(MSG* pMsg) {        if (pMsg->message >= WM_KEYFIRST && pMsg->message <= WM_KEYLAST) {           // Allow tabbing between child controls. if (VK_TAB == pMsg->wParam) {              if (IsDialogMessage(pMsg)) return TRUE; }        }

return COleControl::PreTranslateMessage(pMsg); }                       </li> <li>Create an SDI application with CView as its view class in AppWizard.</li> <li>Insert the ActiveX control's CWnd-wrapper class to the SDI project in Component Gallery.

NOTE: To insert the ActiveX control's wrapper class using the .NET IDE, right-click on the project and select Add | Add Class from the Context menu. In the Add Class dialog box, select the MFC Class from ActiveX Control template, and then select Open. In the Add Class from ActiveX control Wizard select your control from the drop down list, add the interfaces and select Finish.</li> <li> Create the ActiveX control dynamically in the OnInitialUpdate function of the CView-derived class: // CSdiView is a CView-derived class. void CSdiView::OnInitialUpdate {        CView::OnInitialUpdate;

m_pSelection = NULL;   // Initialize selection.

// m_EditOCX is the CWnd-wrapper class of the ActiveX control. VERIFY(m_EditOCX.Create("", WS_CHILD | WS_VISIBLE | WS_TABSTOP, CRect(10, 10, 200, 200), this, 555)); }                       </li> <li>Build the SDI project and run the SDI application. Type something in the edit control of the ActiveX control. Note that the PreTranslateMessage of the ActiveX control is not called.</li></ol>

Additional query words: ocx

Keywords: kbprb KB183167

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.