The text is missing when an ActiveX control is based on Rich Edit 2.0 (810302)



The information in this article applies to:

  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)

Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code. Microsoft Visual C++ 2005 supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model.

SYMPTOMS

When you create an ActiveX control in Visual C++ .NET using Rich Edit 2.0, the text does not appear.

CAUSE

The Rich Edit 2.0 window procedure does not do anything in WM_ERASEBKGND except return the value "1", which indicates that the Rich Edit 2.0 window procedure has erased the background. The WM_PAINT message handler is supposed to erase the background. However, the WM_PAINT message handler does not do anything because the update rect is (0,0,0,0).

RESOLUTION

To resolve this problem, you must invalidate the control window of Rich Edit 2.0, and then pass the WM_PAINT message to the windows procedure of the Rich Edit 2.0 control. To do this, add the WM_PAINT event handler. You must append the following code (to the existing code) for the WM_PAINT event handler:
// MyTestControlCtrl.h

class CMyTestControlCtrl : public COleControl
{
	DECLARE_DYNCREATE(CMyTestControlCtrl)
protected:
	afx_msg void OnPaint();
...
}



// MyTestControlCtrl.cpp


BEGIN_MESSAGE_MAP(CMyTestControlCtrl, COleControl)
	ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
 	ON_WM_PAINT()
END_MESSAGE_MAP()

void CMyTestControlCtrl::OnPaint()
{
    Invalidate(FALSE);
    ::CallWindowProc(*m_pfnSuper, m_hWnd, WM_PAINT, (WPARAM)0, (LPARAM)0);
    // Do not call COleControl::OnPaint() for painting messages
}
Compile the code and run application. Custom control displays text in ActiveX Control Test Container.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. In Microsoft Visual Studio .NET, create a new Visual C++ MFC ActiveX Control project.
  2. Name the project MyTestControl, and then click OK.
  3. To use all default settings, click Finish.
  4. Modify the InitInstance function of CMyTestControlApp class as follows:
    BOOL CMyTestControlApp::InitInstance()
    {
        BOOL bInit = COleControlModule::InitInstance();
    
        // Call this function to initialize the rich edit control (version 2.0 and later) for the application.
        AfxInitRichEdit2();
    
        if (bInit)
        {
            // TODO: Add your own module initialization code here.
        }
    
        return bInit;
    }
    
    
    Note Call to function AfxInitRichEdit2() loads RichEdit20.dll.
  5. For the MyTestControl class, override the PreCreateWindow function.
  6. To override the PreCreateWindow function, follow these steps:
    1. In Class View, right-click CMyTestControlCtrl.
    2. Click to select Add function.
    3. In the Add Member Function Wizard, set the parameters as follows:
      • Return Type: BOOL
      • Function name: PreCreateWindow
      • Parameter type: CREATESTRUCT&
      • Parameter name: cs
    4. Click Add to add the following parameter to the parameter list: Access: public
    5. Click to select virtual as the access modifier, and then click Finish.
  7. Add the following code in the newly created PreCreateWindow function of CMyTestControlCtrl class:
    BOOL CMyTestControlCtrl::PreCreateWindow(CREATESTRUCT& cs)
    {
        cs.lpszClass = _T("RichEdit20W");
        return COleControl::PreCreateWindow(cs);
    }
    Note Set the cs.lpszClass to RichEdit20A for ANSI and RichEdit20W for UNICODE build.
  8. Add the IsSubclassedControl function for the CMyTestControlCtrl class. Perform step 6 using the following parameters:
    • Return Type: BOOL
    • Function Name: IsSubclassedControl
    • Access: public
    Click to select virtual as access modifier.
  9. Add the following code to the newly created IsSubclassedControl function of the CMyTestControlCtrl class:
    BOOL CMyTestControlCtrl::IsSubclassedControl(void)
    {
        return TRUE;
    }
    
  10. Add the following code to the OnDraw function of the CMyTestControlCtrl class.
    void CMyTestControlCtrl::OnDraw(
    			CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
    {
    	// Redraws an OLE control that has been subclassed from a Windows control.
    	DoSuperclassPaint(pdc, rcBounds);
    }
    
  11. Build the project, and then run application.
  12. From the list, click to select ActiveX Control Test Container, and then click OK.
  13. On the Edit menu of the ActiveX Control Test Container application, click Insert new control.
  14. From the list, click to select MyTestControl, and then click OK.
  15. Try to write text in control, and notice that text is missing from the control.

Note The MFC CRichEditCtrl class was designed to support the Rich Edit 1.0 control, and does not support the Rich Edit 2.0 control.
To use Rich Edit 2.0 in an MFC application, call LoadLibrary to load the Riched20.dll, and access its functionality through the Win32 API.

REFERENCES

For additional information, see the following topic in the Visual C++ Programmer's Guide:

Modification Type:MajorLast Reviewed:1/5/2006
Keywords:kbRichEdit kbCtrlCreate kbprb KB810302 kbAudDeveloper kbAudITPRO