Microsoft KB Archive/197504

= PRB: KeyPress Problem When Using MFC Control on MDI Child Form =

Article ID: 197504

Article Last Modified on 5/13/2003

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q197504



SYMPTOMS
You insert controls created in Visual C++ using the Microsoft Foundation Classes (MFC) on a multiple-document interface (MDI) child form. Your MFC control includes the stock event KeyPress and you display a message box in the KeyPress event handler. After you click this control, on a MDI child form and press a key, the event fires. However, if you press a key again, the event does not fire.



CAUSE
When the message box displays and is then dismissed, the MFC control loses the input focus. Consequently, the next key press message is not sent to the control.



RESOLUTION
To resolve the problem, you can set the focus back to that control after the KeyPress event fires.



STATUS
This behavior is by design.



Steps to Create an MFC Control

 * 1) From the File menu, choose "New..." and then click the Projects tab.
 * 2) Next, select the "MFC ActiveX Control Wizard". In the Project Name text box, type MyMfcCtrl, and then click OK. Select all the default settings as you step through the wizard.
 * 3) From the View menu, choose Class Wizard. Click the "ActiveX Events" tab in the MFC ClassWizard dialog box. Click the "Add Event..." button and the Add Event dialog box displays. In the External Name drop-down combo box, select KeyPress. Select the Stock option box located in the Implementation group. Click OK then click OK again. This adds the KeyPress event to your control.
 * 4) Build the project and the control registers automatically.

Steps to Create a Visual Basic Sample
 From the File menu, choose New Project and select the "VB Application Wizard". Click OK and select all the default settings as you go step through the wizard. From the Project menu, choose Components. In the Components dialog box, click the Controls tab. Select the "MyMfcCtrl ActiveX control module" and then click OK. Remove the RichTextBox from the frmDocument and add a MyMfcCtrl to frmDocument keeping the default names. Remove all of the code in the Code window of frmDocument.  Add the following code to the Code window of frmDocument: Private Sub MyMfcCtrl1_KeyPress(KeyAscii As Integer) MsgBox "key Pressed" End Sub </li> Press F5 to run the application then follow these steps:

<ol style="list-style-type: lower-alpha;"> Click the MFC control on the child form.</li> Press a non-tab key and you see the message box "Key Pressed".</li> Dismiss the message box then press a non-tab key again.</li></ol>

RESULTS: No message box displays at this time. This is incorrect.

</li> Close the project.</li></ol>

Steps to Correct the Problem
<ol> Open the MyMfcCtrl project.</li>  From the View menu, choose the Class Wizard, and then click the "Message Maps" tab. In the Class name drop-down list box, select CmyMfcCtrlCtrl. Double-click the WM_CHAR item in the Messages list box then click OK. The following message handler is added by the wizard: void CMyMfcCtrlCtrl::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) </li>  Change the OnChar message handler in the MyMfcCtrlCtl.cpp file to be as follows: void CMyMfcCtrlCtrl::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) {        COleControl::OnChar(nChar, nRepCnt, nFlags); SetFocus; }                       </li>  Add the following code to the MyMfcCtrlCtl.h file as the last member function: BOOL PreTranslateMessage(MSG* pMsg) {        if(pMsg->message == WM_CHAR) {           SendMessage(pMsg->message, pMsg->wParam, pMsg->lParam); return TRUE; }

return COleControl::PreTranslateMessage(pMsg); }                       </li> Build the project again.</li> Test the Visual Basic project again and it works correctly.</li></ol>

<div class="references_section">