Q181664: PRB: Rich Edit Control Message Handlers Are Not Called

Article: Q181664
Product(s): Microsoft C Compiler
Version(s): winnt:5.0,6.0
Operating System(s): 
Keyword(s): kbwizard kbCtrl kbVC500 kbVC600 kbprb
Last Modified: 17-JUL-2001

-------------------------------------------------------------------------------
The information in this article applies to:

- Microsoft Visual C++, 32-bit Enterprise Edition, versions 5.0, 6.0 
- Microsoft Visual C++, 32-bit Professional Edition, versions 5.0, 6.0 
- Microsoft Visual C++, 32-bit Learning Edition, version 6.0 
-------------------------------------------------------------------------------

SYMPTOMS
========

There is a problem with the message-map entries that ClassWizard creates to
handle the following Rich Edit control messages: EN_SETFOCUS, EN_KILLFOCUS. The
message-map entry created by ClassWizard does not call the handler for these
messages.

CAUSE
=====

The wrong message map entries are added.

RESOLUTION
==========

Replace the message-map entries generated for EN_SETFOCUS and EN_KILLFOCUS with
the following entries:

  ON_EN_SETFOCUS(IDC_RICHEDIT1, OnSetfocusRichedit1)

  ON_EN_KILLFOCUS(IDC_RICHEDIT1, OnKillfocusRichedit1)

Note: You need to alter IDC_RICHEDIT1 to match the ID of your control. You will
also need to change the prototypes for the message handlers generated by the
wizard to:

  afx_msg void OnSetfocusRichedit1();

  afx_msg void OnKillfocusRichedit1();

STATUS
======

Microsoft has confirmed this to be a bug in the Microsoft products listed at the
beginning of this article.

MORE INFORMATION
================

Steps to Reproduce Behavior
---------------------------

1. Use the MFC AppWizard to create a dialog-based application.

2. Drag a RichEdit control from the Controls palette onto the Dialog template
  using the Dialog Resource Editor.

3. Open ClassWizard, and choose IDC_RICHEDIT1 for the ObjectID. To add message
  handlers for the EN_SETFOCUS and the EN_KILLFOCUS messages, highlight the
  message and click Add Function.

4. Click OK, and look at the code. The following entry appears in the
  message-map if you selected EN_SETFOCUS:

  ON_NOTIFY(EN_SETFOCUS, IDC_RICHEDIT1, OnSetfocusRichedit1)

  However, this message-map entry should be:

  ON_EN_SETFOCUS(IDC_RICHEDIT1, OnSetfocusRichedit1)

  The following entry is also in the message-map if you selected EN_KILLFOCUS:

  ON_NOTIFY(EN_KILLFOCUS, IDC_RICHEDIT1, OnKillfocusRichedit1)

  However, this message-map entry should be:

  ON_EN_KILLFOCUS(IDC_RICHEDIT1, OnKillfocusRichedit1)

5. If you build and run this project to see the problematic behavior, remember
  to add the line AfxInitRichEdit(); in the InitInstance()

     function of the CWinApp derived class.

6. You can use the default handler created by ClassWizard and add a TRACE
  statement. Following is an example for the EN_KILLFOCUS message:

        void CMyDialog::OnSetfocusRichedit1(NMHDR* pNMHDR, LRESULT* pResult)
        {
           TRACE("This function will never be called\n");
           *pResult = 0;
        }

7. The TRACE statement does not execute when you run a Debug build.

REFERENCES
==========

For additional information, please see the following articles in the Microsoft
Knowledge Base:

  Q166132 PRB: Dialog With RichEdit May Fail During Creation

  Q165744 BUG: RichEditCtrl IDs Not Shown in Member Variables Tab

Additional query words: CRichEditCtrl EN_SETFOCUS EN_KILLFOCUS RichEdit Class Wizard

======================================================================
Keywords          : kbwizard kbCtrl kbVC500 kbVC600 kbprb 
Technology        : kbVCsearch kbAudDeveloper kbVC500 kbVC600 kbVC32bitSearch kbVC500Search
Version           : winnt:5.0,6.0
Issue type        : kbbug kbprb
Solution Type     : kbpending

=============================================================================