Click here to Skip to main content
13,150,178 members (60,108 online)
Click here to Skip to main content
Add your own
alternative version

Stats

40.6K views
18 bookmarked
Posted 8 Feb 2010

A Very Simple Way to Use Richedit 5.0 in VC6 and other VS versions

, 24 Feb 2010
Rate this:
Please Sign up or sign in to vote.
A good tip for using RichEdit 5.0.

Introduction

Microsoft VC++ 6.0 can only build a RichEdit 1.0 app with MFC 4.2. My friends always ask me how to use RichEdit 2.0 or another version of the RichEdit control in a VC6 project. So, this tiny article is just a little attempt of mine to share the info with all of you, and it may be helpful. :)

This article will show you how to use RichEdit v4.1 (RICHEDIT50W in msftedit.dll) directly.

An additional advantage of the method which is shown in the article is that we can get a fully functional CRichEditView based on the RichEdit v4.1 control, instead of just creating a RichEdit v4.1 control with only text functions. Because we still use MFC, we just only replace the base RichEdit control from 1.0 to 4.1. So, we can still Ctrl+V a bitmap from the clipboard to our CRichEditView, can still insert an object and add some hyperlink text with the in-place MFC member functions.

Background

Since Windows(R) has upgraded to Version 6.1, why should we only use RichEdit 1.0 or 2.0? Well... here is something that could let you use RichEdit 4.1 (msftedit.dll) directly.

Principle

Because our CMyAppView is derived from CRichEditView, we can see the statements below in VIEWRICH.CPP:

CRichEditView::CRichEditView():
  CCtrlView(_T("RICHEDIT"),      //MFC hard-coded the ClassName ....
                               //here is the Key.
                               //Our goal is to REPLACE "RICHEDIT"
                               //to "RichEdit50W". And we can do this in our
                               //View Class' Constructor. You can get those
                               //codes under this segment :)
    AFX_WS_DEFAULT_VIEW |
    WS_HSCROLL | WS_VSCROLL | ES_AUTOHSCROLL | ES_AUTOVSCROLL |
    ES_MULTILINE | ES_NOHIDESEL | ES_SAVESEL | ES_SELECTIONBAR)
    {
     m_bSyncCharFormat = m_bSyncParaFormat = TRUE;
     m_lpRichEditOle = NULL;
     ...
    }

Let's go deeper. The MFC source shows us that CRichEditView is derived from CCtrlView, and in VIEWCORE.CPP, in the constructor of CCtrlView, we can see CCtrlView save the class name "RICHEDIT" which gets from CRichEditView into m_strClass, then use this member var in the PreCreateWindow() and Create() functions. The code is shown below:

CCtrlView::CCtrlView(LPCTSTR lpszClass, DWORD dwStyle)
{
 m_strClass = lpszClass;
 //here, MFC save the ClassName given by
 //CRichEditView's Constructor

 m_dwDefaultStyle = dwStyle;
}
BOOL CCtrlView::PreCreateWindow(CREATESTRUCT& cs)
{
 ASSERT(cs.lpszClass == NULL);
 cs.lpszClass = m_strClass;//CCtrlView apply the ClassName to a
CREATESTRUCT here
 ...
 return CView::PreCreateWindow(cs);
}

As we know, every window is created by the CreateWindowEx() API eventually. 'CREATESTRUCT& cs' is just brought from CWnd::CreateEx(), which is used to tell the system how to create a window.

BOOL CWnd::CreateEx(DWORD dwExStyle, LPCTSTR lpszClassName,
       LPCTSTR lpszWindowName, DWORD dwStyle,
       int x, int y, int nWidth, int nHeight,
       HWND hWndParent, HMENU nIDorHMenu, LPVOID lpParam)
{
 // allow modification of several common create parameters
 CREATESTRUCT cs;
 cs.dwExStyle = dwExStyle;
 cs.lpszClass = lpszClassName;
 cs.lpszName = lpszWindowName;
 cs.style = dwStyle;
 cs.x = x;
 cs.y = y;
 cs.cx = nWidth;
 cs.cy = nHeight;
 cs.hwndParent = hWndParent;
 cs.hMenu = nIDorHMenu;
 cs.hInstance = AfxGetInstanceHandle();
 cs.lpCreateParams = lpParam;

 if (!PreCreateWindow(cs))//CCtrlView::PreCreateWindow() will be called here,
       //and our New ClassName "RichEdit50W" has been already in cs
 {
   PostNcDestroy();
   return FALSE;
 }

 AfxHookWindowCreate(this);
 HWND hWnd = ::CreateWindowEx(cs.dwExStyle, cs.lpszClass,
   cs.lpszName, cs.style, cs.x, cs.y, cs.cx, cs.cy,
   cs.hwndParent, cs.hMenu, cs.hInstance, cs.lpCreateParams);
   // Our RichEdit v4.1(RICHEDIT50W) Ctrl
   // has been created successfully!
 ...
}

How Do We Use the Code?

This is a very typical project produced by the MFC App Wiz. CMyAppView is the View Class derived from CRichEditView.

class CMyAppView : public CRichEditView
{
...
}

There are two steps. First, put 'm_strClass=TEXT("RichEdit50W");' into the the constructor of CMyAppView. It should look like this:

CMyAppView::CMyAppView()
{
  m_strClass=TEXT("RichEdit50W");
  //Must be m_strClass, it is a member of CCtrlView
  ...
}

Then , put 'LoadLibrary(TEXT("msftedit.dll"));' into the constructor of the CMyApp class. And, OLE must be initialized too. When it is done, it should look like this:

CMyApp::CMyApp()
{
 CoInitialize(NULL); //this must be called FIRST!

 //m_hinstRE41 is a HINSTANCE type member var of CMyApp
 m_hinstRE41=LoadLibrary(TEXT("msftedit.dll"));
 //this DLL must be loaded.
 ...
}

CMyApp::~CMyApp()
{
 if(m_hinstRE41)
 FreeLibrary(m_hinstRE41);
}
CMyApp::InitInstance()
{
 AfxOleInit();                     //you don't want to miss this ....
 AfxEnableControlContainer();      //and this ....
 ...
}

Another Important Tip

In CMyAppView::OnDestroy, you should see:

void CGreatTracerView::OnDestroy()
{
  //CRichEditView::OnDestroy(); <------the MFC App Wiz put the codes here,
  // but it shouldn't.
  // Deactivate the item on destruction; this is important
  // when a splitter view is being used.
  COleClientItem* pActiveItem = GetDocument()->
               GetInPlaceActiveItem(this); //If OnDestroy() were still up there,
                                       //the app would CRASH here!
  if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
  {
     pActiveItem->Deactivate();
     ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
  }
  CRichEditView::OnDestroy();
  // this codes should be here, so, everything is fine!
}

By the way, I really recommend building the project in Unicode. :)

History

  • 2010.2.6: First version - 1.0.
  • 2010.2.7: Version 1.1 - Added more intro and the principle part.
  • 2010.2.23: Version 1.2 - Corrected the version error; msftedit.dll contains RichEdit v4.1 (the class name is RICHEDIT50W), not v5.0.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

GhostEx
China China
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 5 Pin
cccfff77714-Jul-10 23:11
membercccfff77714-Jul-10 23:11 
General.NET Richedit Control Pin
Dmex8713-Mar-10 6:50
memberDmex8713-Mar-10 6:50 
QuestionRichEdit 7.0 Pin
Carc2-Mar-10 7:49
memberCarc2-Mar-10 7:49 
AnswerRe: RichEdit 7.0 Pin
GhostEx2-Mar-10 23:30
memberGhostEx2-Mar-10 23:30 
GeneralRe: RichEdit 7.0 Pin
Carc3-Mar-10 0:53
memberCarc3-Mar-10 0:53 
GeneralRe: RichEdit 7.0 Pin
Warrick Procter5-Dec-10 21:09
memberWarrick Procter5-Dec-10 21:09 
GeneralRich Edit v4.1 Pin
S.H.Bouwhuis17-Feb-10 2:56
memberS.H.Bouwhuis17-Feb-10 2:56 
GeneralRe: Rich Edit v4.1 Pin
Axel Rietschin20-Feb-10 16:51
memberAxel Rietschin20-Feb-10 16:51 
GeneralRe: Rich Edit v4.1 Pin
S.H.Bouwhuis21-Feb-10 2:10
memberS.H.Bouwhuis21-Feb-10 2:10 
AnswerRe: Rich Edit v4.1 Pin
GhostEx23-Feb-10 1:22
memberGhostEx23-Feb-10 1:22 
GeneralHi Pin
Aric Green15-Feb-10 19:42
memberAric Green15-Feb-10 19:42 
GeneralRe: Hi Pin
GhostEx23-Feb-10 1:29
memberGhostEx23-Feb-10 1:29 
GeneralRe: Hi Pin
Aric Green23-Feb-10 4:35
memberAric Green23-Feb-10 4:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170924.2 | Last Updated 24 Feb 2010
Article Copyright 2010 by GhostEx
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid