Using Richedit 4.1 with D2010

Why

Starting with Windows Vista, the OS offers a lot of features and improvements to the embedded Speech capabilities that are directly available for free to the Delphi developer, as I have shown in my CodeRage and DelphiLive sessions. But to take full advantage of the Text Services Framework implementation of many Speech Recognition functionalities, it is necessary to use a more advanced version of the RichEdit control than the version 2.0 shipped with Delphi (2010 and even XE).

Another reason for using RichEdit 4.1 is the proper handling of fancy Table layouts from the recent Word versions like:


As copied in a TRichEdit, standard VCL version (2.0):

As copied in a TRichEdit 4.1:

Disclaimer

This code is proposed “as is”, exert your better judgment and use at your own risk…

Note1: Richedit 4.1 is only available in Win XP and above.

Note2: The code modifications are only somewhat tested for Win32. (not tested for CLR portions, only there just in case)…

Usage

Either replace ComCtrls.pas in RAD Studio folder (not recommended), place the patched version in your usual folder holding the patched Delphi units (be sure to have it found before the regular one), or place the modified version in your project folder and reference it at the beginning of the dpr.

program PTestRichEdit41;
uses
  ComCtrls in 'ComCtrls.pas',
  Forms,
  UTestRichEdit41 in 'UTestRichEdit41.pas' {Form9};

Your new version will be used by the application.

Disable the conditional directive {$define richedit41} if you want the old RichEdit : {.$define richedit41}.

The code will try to load the 4.1 version of the DLL, and will fall back automatically to the old VCL version if it cannot (for instance on Win2000).

Code

  • Change the DLL name from RICHED20.DLL to MSFTEDIT.DLL.
  RichEditModuleName = 'MSFTEDIT.DLL';
  OldRichEditModuleName = 'RICHED20.DLL';
  • Define a global variable to know if you are using the newer version.
{$ifdef richedit41}
  IsRichEdit41: Boolean;
{$endif}
  • Try to load the new version and if it fails, load the old one.
    FRichEditModule := LoadLibrary(RichEditModuleName);
{$ifdef richedit41}
    if FRichEditModule <= HINSTANCE_ERROR then
    begin
      IsRichEdit41 := False;
      FRichEditModule := LoadLibrary(OldRichEditModuleName);
    end
    else
      IsRichEdit41 := True;
{$endif}
    if FRichEditModule <= HINSTANCE_ERROR then FRichEditModule := 0;
  • Change the ClassName used to instantiate a TRichEdit (with suffix A instead of W for non Unicode version).
{$ifdef richedit41}
  NewRichEditClassName = 'RICHEDIT50W';
{$endif}
  RichEditClassName = 'RICHEDIT20W';
  • Use the appropriate ClassName.
{$ifdef richedit41}
  if IsRichEdit41 then
    CreateSubClass(Params, NewRichEditClassName)
  else
{$endif}
  CreateSubClass(Params, RichEditClassName);

Code Diff.


This entry was posted in Delphi, VCL and tagged , , , , . Bookmark the permalink.

5 Responses to Using Richedit 4.1 with D2010

  1. iseek says:

    how to insert a table or an image into richedit?

  2. J.-G. Yu says:

    It seems to me that this modification is not needed. For example, one can create a new VCL project, put a TRichEdit on the form and compile without any modification in your post. Then, one just have to copy the dll files (RICHED20.DLL and MSPTLS.DLL) from Office 2010/2013/2016 to the exe folder, and the exe will load the new version automatically (tested by table copy paste example in your post). Could you help to comment ?

  3. J.-G. Yu says:

    (The above comments are tested in WinXP SP2 and Win7 SP1, where no Delphi or Office is installed.).

Leave a Reply to J.-G. Yu Cancel reply

Your email address will not be published. Required fields are marked *