PRB: KeyPress Problem When Using MFC Control on MDI Child Form
ID: Q197504
|
The information in this article applies to:
-
Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, versions 5.0, 6.0
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.
MORE INFORMATIONSteps to Reproduce Behavior
Steps to Create an MFC Control
- From the File menu, choose "New..." and then click the Projects tab.
- 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.
- 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.
- 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
- Press F5 to run the application then follow these steps:
- Click the MFC control on the child form.
- Press a non-tab key and you see the message box "Key Pressed".
- Dismiss the message box then press a non-tab key again.
RESULTS: No message box displays at this time. This is incorrect.
- Close the project.
Steps to Correct the Problem
- Open the MyMfcCtrl project.
- 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)
- 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();
}
- 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);
}
- Build the project again.
- Test the Visual Basic project again and it works correctly.
REFERENCES
For other problems related to using MFC controls in MDI forms, please see
the following articles in the Microsoft Knowledge Base:
Q192347
PRB: Focus/Activation Problems with MFC Control on VB Forms
Q197503
PRB: Focus Problem When MFC Control is Used on MDI Child
Form
Additional query words:
Keywords : kbCtrlCreate kbMFC kbVBp kbVBp500 kbVBp600 kbGrpVB
Version : WINDOWS:5.0,6.0
Platform : WINDOWS
Issue type : kbprb
|