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.

2 Responses to Using Richedit 4.1 with D2010

  1. iseek says:

    how to insert a table or an image into richedit?

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>