The Rich Edit 2.0 Library


 

Introduction

Rich Edit 2.0 is a library created by Microsoft to enhance the capabilities of the RICHEDIT_CLASS class. The idea was to allow programmers to create applications that are closely compatible to Microsoft Word.

When using Borland C++ Builder and the Visual Component Library, the TRichEdit class ships with many variables and functions used to perform various formatting assignments on characters and paragraphs. Unfortunately, the TRichEdit class is limited to the Win32's RICHEDIT_CLASS class. One solution you can use is to call the variables and functions that are part of the Rich Edit 2.0 library.

 

Using the Rich Edit 2.0 Library

The Rich Edit 2.0 library comes through a file called riched20 .dll. This DLL can be found in the Windows\System32 folder. Because it is a dynamic DLL, in order to use it, the first thing you should do is to load it. This can be done in the OnCreate event of the form as follows:
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
    int RichText = (int)LoadLibrary("riched20.dll");

    if( !RichText ) // If you could not load riched20.dll
    {
        ShowMessage("Could not load the riched20.dll");
        Application->Terminate();
        return;
    }
}
//---------------------------------------------------------------------------

Text Formatting

Text formatting in the TRichEdit class is performed using the TFont class. For example, to change the text of a selection to Bold, you can use code as follows:

//---------------------------------------------------------------------------
void __fastcall TForm1::btnBoldClick(TObject *Sender)
{
    if( btnBold->Down )
        rchEditor->SelAttributes->Style = TFontStyles() << fsBold;
    else
        rchEditor->SelAttributes->Style = TFontStyles() >> fsBold;
}
//---------------------------------------------------------------------------

To format text using the Rich Edit 2.0 library, you use the CHARFORMAT2 structure (you can call it a class if you want) which is defined as follows:

typedef struct _charformat2 {
  UINT        cbSize;
  DWORD       dwMask;
  DWORD       dwEffects;
  LONG        yHeight;
  LONG        yOffset;
  COLORREF    crTextColor;
  BYTE        bCharSet;
  BYTE        bPitchAndFamily;
  TCHAR       szFaceName[LF_FACESIZE];
  WORD        wWeight;
  SHORT       sSpacing;
  COLORREF    crBackColor;
  LCID        lcid;
  DWORD       dwReserved;
  SHORT       sStyle;
  WORD        wKerning;
  BYTE        bUnderlineType;
  BYTE        bAnimation;
  BYTE        bRevAuthor;
  BYTE        bReserved1;
} CHARFORMAT2;

Selection Highlight

//---------------------------------------------------------------------------
void __fastcall TForm1::btnHighlightClick(TObject *Sender)
{
    Richedit::CHARFORMAT2 cfm;

    if( ColorDialog1->Execute() )
    {
        cfm.cbSize = sizeof(cfm);
        cfm.dwMask = CFM_PROTECTED|CFM_BACKCOLOR;
        cfm.dwEffects = CFE_PROTECTED;
        cfm.crBackColor = ColorDialog1->Color;

        SendMessage( RichEdit1->Handle, EM_SETCHARFORMAT,
                    (WPARAM)SCF_SELECTION, (LPARAM)&cfm);
    }
}
//---------------------------------------------------------------------------

Paragraph Formatting


    

    

    

UNDER CONSTRUCTION

//---------------------------------------------------------------------------

#ifndef MainH
#define MainH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <ImgList.hpp>
#include <Menus.hpp>
#include <ToolWin.hpp>
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
#include <Dialogs.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
    TMainMenu *MainMenu1;
    TRichEdit *RichEdit1;
    TStatusBar *StatusBar1;
    TToolBar *ToolBar1;
    TMenuItem *FileMenu;
    TMenuItem *FileExit;
    TMenuItem *N1;
    TMenuItem *FilePrintSetup;
    TMenuItem *FilePrint;
    TMenuItem *N2;
    TMenuItem *FileSaveAs;
    TMenuItem *FileSave;
    TMenuItem *FileOpen;
    TMenuItem *FileNew;
    TMenuItem *EditMenu;
    TMenuItem *EditReplace;
    TMenuItem *EditFind;
    TMenuItem *N4;
    TMenuItem *EditPaste;
    TMenuItem *EditCopy;
    TMenuItem *EditCut;
    TMenuItem *N5;
    TMenuItem *EditRedo;
    TMenuItem *EditUndo;
    TMenuItem *ViewMenu;
    TMenuItem *ToolbarsMenu;
    TMenuItem *ViewStatusBar;
    TMenuItem *ViewStandard;
    TMenuItem *ViewFormatting;
    TMenuItem *FormatMenu;
    TMenuItem *FormatFont;
    TMenuItem *FormatParagraph;
    TMenuItem *FormatBulletsAndNumbering;
    TToolBar *ToolBar2;
    TImageList *ImageList1;
    TToolButton *btnNew;
    TToolButton *btnOpen;
    TToolButton *btnSave;
    TToolButton *tbrSeparator1;
    TToolButton *btnPrint;
    TToolButton *tbrSeparator2;
    TToolButton *btnUndo;
    TToolButton *btnRedo;
    TToolButton *btnCut;
    TToolButton *btnCopy;
    TToolButton *btnPaste;
    TToolButton *tbrSeparator3;
    TToolButton *btnFind;
    TComboBox *cboFontNames;
    TComboBox *btnFontSizes;
    TToolButton *tbrSeparator4;
    TToolButton *btnBold;
    TToolButton *btnItalic;
    TToolButton *btnUnderline;
    TToolButton *btnStrikeout;
    TToolButton *tbrSeparator5;
    TToolButton *btnAlignLeft;
    TToolButton *btnCenter;
    TToolButton *btnAlignRight;
    TToolButton *tbrSeparator6;
    TToolButton *btnBulletNumbers;
    TToolButton *btnBullets;
    TToolButton *tbrSeparator7;
    TToolButton *btnIndentLeft;
    TToolButton *btnIndentRight;
    TToolButton *tbrSeparator8;
    TToolButton *btnHighlight;
    TColorDialog *ColorDialog1;
    void __fastcall FormCreate(TObject *Sender);
    void __fastcall FileExitClick(TObject *Sender);
    void __fastcall btnHighlightClick(TObject *Sender);
    void __fastcall btnBulletsClick(TObject *Sender);
    void __fastcall btnBoldClick(TObject *Sender);
    void __fastcall btnItalicClick(TObject *Sender);
    void __fastcall btnUnderlineClick(TObject *Sender);
private:	// User declarations
public:		// User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
#include <vcl.h>
#include <Richedit.h>
#pragma hdrstop

#include "Main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
    int RichText = (int)LoadLibrary("riched20.dll");

    if( !RichText ) // If you could not load riched20.dll
    {
        ShowMessage("Could not load the riched20.dll");
        Application->Terminate();
        return;
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FileExitClick(TObject *Sender)
{
    Close();    
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnHighlightClick(TObject *Sender)
{
    Richedit::CHARFORMAT2 cfm;

    if( ColorDialog1->Execute() )
    {
        cfm.cbSize = sizeof(cfm);
        cfm.dwMask = CFM_PROTECTED|CFM_BACKCOLOR;
        cfm.dwEffects = CFE_PROTECTED;
        cfm.crBackColor = ColorDialog1->Color;

        SendMessage( RichEdit1->Handle, EM_SETCHARFORMAT,
                    (WPARAM)SCF_SELECTION, (LPARAM)&cfm);
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnBulletsClick(TObject *Sender)
{
    PARAFORMAT2 pfm;

    pfm.cbSize = sizeof(PARAFORMAT2);
    pfm.dwMask = PFM_NUMBERING | PFM_OFFSET;
    pfm.wNumbering = PFN_BULLET;
    pfm.dxOffset = 100;

    SendMessage(RichEdit1->Handle, EM_SETPARAFORMAT, 0, (LPARAM)&pfm);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnBoldClick(TObject *Sender)
{
    Richedit::CHARFORMAT2 cfm;

    SendMessage( RichEdit1->Handle, EM_GETCHARFORMAT,
               ( WPARAM)SCF_SELECTION, (LPARAM)&cfm );

    cfm.cbSize = sizeof(cfm);
    cfm.dwMask = CFM_PROTECTED | CFM_BOLD;
    cfm.dwEffects = CFE_BOLD;

    SendMessage( RichEdit1->Handle, EM_SETCHARFORMAT,
               ( WPARAM)SCF_SELECTION, (LPARAM)&cfm );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnItalicClick(TObject *Sender)
{
    Richedit::CHARFORMAT2 cfm;

    cfm.cbSize = sizeof(cfm);
    cfm.dwMask = CFM_PROTECTED | CFM_ITALIC;
    cfm.dwEffects = CFE_ITALIC;

    SendMessage( RichEdit1->Handle, EM_SETCHARFORMAT,
               ( WPARAM)SCF_SELECTION, (LPARAM)&cfm );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnUnderlineClick(TObject *Sender)
{
    Richedit::CHARFORMAT2 cfm;

    cfm.cbSize = sizeof(cfm);
    cfm.dwMask = CFM_UNDERLINE;
    cfm.dwEffects = CFE_UNDERLINE;

    SendMessage( RichEdit1->Handle, EM_SETCHARFORMAT,
                (WPARAM)SCF_SELECTION, (LPARAM)&cfm );
}
//---------------------------------------------------------------------------

 


Copyright 2003 FunctionX, Inc.