C++ Builder

La Isla Bonita



How to Insert an Image in a TRichEdit Control
How to Insert an Image in a TRichEdit Control


Description

This article shows how to insert a BMP image into a TRichEdit component whose text is static (never changing) The image remains in the same position relative to the rest of the text in the component.

In the Programmer's Design Journal I explain how to insert a large number of images in a TRichEdit control at runtime, how to resize this control at runtime (allowing to have either more of less images), and how to change all the images at runtime.

RichEditInsertImage.cpp

#include <vcl.h>
#pragma hdrstop

#include "RichEditInsertImage.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TRichEdit* RichEdit1;
TImage* p;


__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
    Color = (TColor)0x12dec;
    Width  = 800;
    Height = 600;
    Position = poScreenCenter;
     
     //Create the TRichEdit Component
    RichEdit1 = new TRichEdit(this);
    RichEdit1->Parent = this;
    RichEdit1->Top    = 50;
    RichEdit1->Left   = 50;
    RichEdit1->Width  = 300;
    RichEdit1->Height = 300;

     //Create the TImage Component
    p = new TImage( RichEdit1 );
    p->Parent = RichEdit1;
    p->Top = 100;
    p->Left = 10;
    p->Width = 30;
    p->Height = 30;
    p->Picture->LoadFromFile( String("C:\\Dir1\\Dir2\\Image1.bmp") );
}

RichEditInsertImage.h

#ifndef RichEditInsertImageH
#define RichEditInsertImageH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <ImgList.hpp>
#include <ExtCtrls.hpp>
#include <sGraphics.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        void __fastcall RichEdit1Enter(TObject *Sender);
private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Homepage

Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.