CRichEditCtrlEx : Replacing "RICHEDIT" control with "RichEdit20A"

While writing a dev notes Visual Studio plug in, I wanted the rich edit control in a dialog to understand URL's so developers could add links to sites and email addresses. After talking to "Long John", he led me to the easy way of doing it, and now I am posting the work for others. Basically this requires CRichEditCtrlEx to use the new rich edit DLL RICHED20.DLL. The first thing to do it change the window class name of the rich edit controls from "RICHEDIT" to "RichEdit20A" in the *.rc file. Next, we create a class derived from CRichEditCtrl called CRichEditCtrlEx and override the Create() method:
class CRichEditCtrlEx : public CRichEditCtrl
{
public:
CRichEditCtrlEx();
virtual ~CRichEditCtrlEx();
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL( CRichEditCtrlEx )
public:
virtual BOOL Create( DWORD in_dwStyle, const RECT& in_rcRect,
CWnd* in_pParentWnd, UINT in_nID );
//}}AFX_VIRTUAL
// Generated message map functions
protected:
//{{AFX_MSG( CRichEditCtrlEx )
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
Now implement the Create() method like this:
BOOL CRichEditCtrlEx::Create(DWORD in_dwStyle, const RECT& in_rcRect,
CWnd* in_pParentWnd, UINT in_nID)
{
if( ! ::AfxInitRichEditEx() )
{
return FALSE ;
}
CWnd* l_pWnd = this ;
return l_pWnd->Create( _T( "RichEdit20A" ), NULL, in_dwStyle,
in_rcRect, in_pParentWnd, in_nID );
}
This is almost exactly what is in CRichEditCtrl::Create(), except the different window class name, and the call to AfxInitRichEdit() has been changed to AfxInitRichEditEx() which first calls AfxInitRichEdit (we do not want to break the normal CRichEditCtrl). In the RichEditCtrlEx.h file, you need to add the DLL holder class and function prototype:
class _AFX_RICHEDITEX_STATE
{
public:
_AFX_RICHEDITEX_STATE();
virtual ~_AFX_RICHEDITEX_STATE();
HINSTANCE m_hInstRichEdit20 ;
};
BOOL PASCAL AfxInitRichEditEx();
Now, in the source code, add the implementation:
_AFX_RICHEDITEX_STATE::_AFX_RICHEDITEX_STATE()
{
m_hInstRichEdit20 = NULL ;
}
_AFX_RICHEDITEX_STATE::~_AFX_RICHEDITEX_STATE()
{
if( m_hInstRichEdit20 != NULL )
{
::FreeLibrary( m_hInstRichEdit20 ) ;
}
}
_AFX_RICHEDITEX_STATE _afxRichEditStateEx ;
BOOL PASCAL AfxInitRichEditEx()
{
if( ! ::AfxInitRichEdit() )
{
return FALSE ;
}
_AFX_RICHEDITEX_STATE* l_pState = &_afxRichEditStateEx ;
if( l_pState->m_hInstRichEdit20 == NULL )
{
l_pState->m_hInstRichEdit20 = LoadLibraryA("RICHED20.DLL") ;
}
return l_pState->m_hInstRichEdit20 != NULL ;
}
Last thing to help out it we add a method to set the auto URL detection in the Rich Edit to on/off:
inline BOOL CRichEditCtrlEx::AutoURLDetect( BOOL in_fEnable )
{
ASSERT(::IsWindow( m_hWnd ) );
return ( BOOL )::SendMessage( m_hWnd, EM_AUTOURLDETECT, in_fEnable, 0 );
}
See the attached sample for the full source code.
Now to use this in your code, you need to use CRichEditCtrlEx for the rich edits you want to include URL detection, and you must call AfxInitRichEditEx() in your CMyApp::InitInstance() if you have dialogs using the rich edit controls.
To then use the URL, you must set the event mask on the rich edit control to include ENM_LINK and then the control will send EN_LINK notifications. The EN_LINK notification uses the ENLINK structure which includes a message ID. This message will be:
- WM_LBUTTONDBLCLK
- WM_LBUTTONDOWN
- WM_LBUTTONUP
- WM_MOUSEMOVE
- WM_RBUTTONDBLCLK
- WM_RBUTTONDOWN
- WM_RBUTTONUP
- WM_SETCURSOR
If you want to override the default action, set the pResult to 1, otherwise set it to zero. See the example code to see how to handle this notification.
Download demo project - 16KB (this sample was compiled and tested using VC 6.0 sp1 on NT 4.0 sp4)

Comments
RichEdit and XP themed style
Posted by Legacy on 11/25/2003 12:00amOriginally posted by: Peter Sander
I was hoping that the newer RichEdit version support a RichEdit control being drawn in WindowsXP style, e.g. like an edit control. However, they always end up in the old sunken style.
ReplyI also figured out, that the Themes do not natively support a control name RICHEDIT.
Has anybody figured out this?
Find and Replace Problem in RichEdit20W
Posted by Legacy on 07/14/2003 12:00amOriginally posted by: Kathy
I develop an MDI application using RichEdit20W DLL to support Unicode RichEdit View. I found that the Find and Replace functions doesn't work.
I use the default menu items (ID_EDIT_FIND and ID_EDIT_REPLACE) to provide these 2 functions. I just use the system default function handlers to find and replace the text in RichEdit view.
But, when I use the older DLL (the default one), both functions works correctly.
I develop my application using VC++ 6.0 with SP5 under Windows 2000.
Please help me to solve this problem!
Thanks
Kathy
ReplyRich Edit View2.0
Posted by Legacy on 08/16/2002 12:00amOriginally posted by: Leon Forsen
ReplyKeep Linkfont when changing RichEditCtrlFont, possible??
Posted by Legacy on 06/20/2002 12:00amOriginally posted by: christos
This is great article which spared me a lot of time. Thanks!
But there is one question left:
How to keep the LinkFont when changing the Font of the Control?
Thanks for your help,
Christos
ReplySome Fonts Not Displaying with RichEdit20.dll
Posted by Legacy on 06/06/2002 12:00amOriginally posted by: Ranjini
Some fonts (BookShelf,Webdings) is not taking into effect with RichEdit20.I could modify the font with RichEdit10.dll.
ReplyIt would be of great help if this problem could be resolved
geting character under mouse cursor in a window
Posted by Legacy on 05/26/2002 12:00amOriginally posted by: habib
Dear All,
I am trying to write a screan reader program, and I should read each character or world under mouse cursor. for example when mouse go on a text in a text box (or even a button or ..) I read just character under the mouse pointer.
pls note that I should read character by character note whole text of window.
I really appriciate if some one help me.
Thanks,
ReplyHabib Rostami
Upgrade to Riched20.dll problem.
Posted by Legacy on 04/15/2002 12:00amOriginally posted by: Zarin Thomas
In my application, I use the Dundas library which helps me to use the VB Script editor.
This editor provided by Dundas inturn uses the Riched32.dll
Now when upgrading to the Riched20.dll, there are some problems observed. Since Dundas no longer provides any support we need to tackle the problem ourselves.
The VB script editor looks likes this
'(Declarations)
Dim StDoc
Set Stdoc = Appobject.Opendocument ("Prog1.ST")
'End of (Declarations)
The '(Declarations)" and "'End of (Declarations)" lines in the script editor file are protected.
When I press enter after "'(Declarations)" and try to edit the next line I am not being permitted to do so.
Also another problem observed is that "'End of (Declarations)" line which is protected is also editable.
The same application works perfectly well with the Richedt32.dll.
The changes that I did to try and make it work for Riched20.dll are as follows
1. In the InitInstance of my main application I do a - LoadLibrary("RICHED20.DLL"); now instead of the earlier LoadLibrary("RICHED32.DLL");
2. In the the constructor of the CRichEditCtrl {Part of the Dundas library} I again do a - LoadLibrary("RICHED20.DLL"); just to be on the safe side.
3. In the ViewClass {Part of the Dundas library} where the rich edit control is created I have the following statement
m_pWndEditCtrl->CWnd::CreateEx(WS_EX_STATICEDGE,
_T("RichEdit20A"), NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL |ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_WANTRETURN, CRect(11, 11 + (m_bShowCombos?28:0),247,55), this, IDC_SED_TEXT)
Earlier instead of _T("RichEdit20A") it was _T("RICHEDIT"). Note: This change in the class name has been made in the ViewClass.cpp file and not in the resource file as this control has been created dynamically and not via the resource editor.
Does anybody have any idea why it happens like this. The library getting loaded is Riched20.dll itself, checked thru "Process Explorer" tool and not Riched32.dll.
Pls. help
Thanks in advance
Zarin.
ReplyHow can I set the color of the url text?
Posted by Legacy on 04/08/2002 12:00amOriginally posted by: Silviu Marele
Ok, it's very nice that my RichEdit control finds the url's . But when finding one, it sets for it a specific color and I don't know where this color is taken from. How can I set the color I want for the url's?
Thank you.
ReplyHow to create hyperlinks like MSWord Table of contents ??
Posted by Legacy on 03/12/2002 12:00amOriginally posted by: venky
This is an excellent article. Thanks.
BTW, do you have any idea if we can create our own custom tags that the RichEdit can interpret. Say I want to make a hyper link on some text. On clicking that hyper link I want to do some other action (Not opening a browser, because that link is not for any web page). ie. Somewhat similar to what MS Word has for its Table Of Contents. How do I do that
Please let me know
Venky
ReplyURL detection in CRichEditView derived class
Posted by Legacy on 03/03/2002 12:00amOriginally posted by: Suraj Gogoi
ReplyLoading, Please Wait ...