Microsoft KB Archive/810302

= The text is missing when an ActiveX control is based on Rich Edit 2.0 =

Article ID: 810302

Article Last Modified on 11/13/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-



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 &quot;1&quot;, 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.



Steps to Reproduce the Behavior
 In Microsoft Visual Studio .NET, create a new Visual C++ MFC ActiveX Control project. Name the project MyTestControl, and then click OK. To use all default settings, click Finish.  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.  For the MyTestControl class, override the PreCreateWindow function.</li> To override the PreCreateWindow function, follow these steps: <ol style="list-style-type: lower-alpha;"> In Class View, right-click CMyTestControlCtrl.</li> Click to select Add function.</li> In the Add Member Function Wizard, set the parameters as follows: <ul> Return Type: BOOL</li> Function name: PreCreateWindow</li> Parameter type: CREATESTRUCT&</li> Parameter name: cs</li></ul> </li> Click Add to add the following parameter to the parameter list: Access: public</li> Click to select virtual as the access modifier, and then click Finish.</li></ol> </li>  Add the following code in the newly created PreCreateWindow function of CMyTestControlCtrl class: BOOL CMyTestControlCtrl::PreCreateWindow(CREATESTRUCT& cs) {   cs.lpszClass = _T(&quot;RichEdit20W&quot;); return COleControl::PreCreateWindow(cs); } Note Set the cs.lpszClass to RichEdit20A for ANSI and RichEdit20W for UNICODE build. </li> Add the IsSubclassedControl function for the CMyTestControlCtrl class. Perform step 6 using the following parameters: <ul> Return Type: BOOL</li> Function Name: IsSubclassedControl</li> Access: public</li></ul>

Click to select virtual as access modifier.</li>  Add the following code to the newly created IsSubclassedControl function of the CMyTestControlCtrl class: BOOL CMyTestControlCtrl::IsSubclassedControl(void) {   return TRUE; } </li> <li> 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); } </li> <li>Build the project, and then run application.</li> <li>From the list, click to select ActiveX Control Test Container, and then click OK.</li> <li>On the Edit menu of the ActiveX Control Test Container application, click Insert new control.</li> <li>From the list, click to select MyTestControl, and then click OK.</li> <li>Try to write text in control, and notice that text is missing from the control.</li></ol>

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.

<div class="references_section">