В стандартном RichEdit нельзя, для RichEdit с картинками используйте RichEdit из RxLib или JVCL.

Ниже представлен пример, который можно применить к RxRichEdit, RichEditEx, RichEdit98, и Microsoft RichTextBox (поставляемый с VB5+) не прибегая к использованию буфера обмена или OLE:

 

Вот так можно вставить картинку в формате Bitmap в позицию курсора в TRichEdit:

Так как вопрос давольно часто поднимается в форумах, то хотелось бы привести ответ на него. Итак, как же получить текущие координаты курсора (Row и Col) в TRichEdit ?

 Вот пример решения данной проблемы:

Code:

var

pt: TPoint;

begin

with richedit1 do begin

   Perform( messages.EM_POSFROMCHAR, WPARAM(@pt), selstart );

   label1.caption := Format('(%d,%d)', [pt.x, pt.y]);

end;

end;

var

r: LongInt;

begin

with memo1 do begin

   r := Perform( messages.EM_POSFROMCHAR, selstart, 0);

   if r >= 0 then begin

     label1.caption := IntToStr(HiWord(r));

     label2.caption := IntToStr(LoWord(r));

   end;

end;

end;

 

 

Code:

{

You have to dig into the Rich Text Format if you want to copy text to the

clipboard that has format information embedded. The application you paste

this text into has to understand RTF, or the formatting will not show up.

 

OK, the first step is to register a clipboard format for RTF, since this is

not a predefined format:

}

 

Var

CF_RTF : Word;

 

CF_RTF := RegisterClipboardFormat('Rich Text Format');

 

{

The format name has to appear as typed above, this is the name used by MS

Word for Windows and similar MS products.

 

NOTE: The Richedit Unit declares a constant CF_RTF, which is NOT the

clipboard format handle but the string you need to pass to RegisterClipboard

format! So you can place Richedit into your uses clause and change the line

above to

}

 

CF_RTF := RegisterClipboardFormat(Richedit.CF_RTF);

 

{

The next step is to build a RTF string with the embedded format information.

You will get a shock if you inspect the mess of RTF stuff W4W will put into

the clipboard if you copy just a few characters (the app below allows you to

inspect the clipboard), but you can get away with a lot less. The bare

minimum would be something like this (inserts an underlined 44444):

}

 

const

testtext: PChar = '{\rtf1\ansi\pard\plain 12{\ul 44444}}';

 

 

{

The correct balance of opening and closing braces is extremely important, one

mismatch and the target app will not be able to interpret the text

correctly. If you want to control the font used for the pasted text you need

to add a fonttable (the default font is Tms Rmn, not the active font in the

target app!). See example app below, testtext2. If you want more info, the

full RTF specs can be found on www.microsoft.com, a subset is also described

in the Windows help compiler docs (hcw.hlp, comes with Delphi).

}

 

 

unit Clipfmt1;

 

interface

 

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls;

 

type

TForm1 = class(TForm)

   MemFormats: TMemo;

   label1: TLabel;

   BtnShowFormats: TButton;

   BtnGetRTF: TButton;

   BtnSetRTF: TButton;

   MemExample: TMemo;

   procedure FormCreate(Sender: TObject);

   procedure BtnShowFormatsClick(Sender: TObject);

   procedure BtnGetRTFClick(Sender: TObject);

   procedure BtnSetRTFClick(Sender: TObject);

private

public

   CF_RTF: Word;

end;

 

var

Form1: TForm1;

 

implementation

 

uses Clipbrd;

 

{$R *.DFM}

 

procedure TForm1.FormCreate(Sender: TObject);

begin

// register clipboard format rtf

CF_RTF := RegisterClipboardFormat('Rich Text Format');

if CF_RTF = 0 then

begin

   ShowMessage('Unable to register the Rich Text clipboard format!');

   Application.Terminate;

end;

BtnShowFormats.Click;

end;

 

// show clipboard formats

procedure TForm1.BtnShowFormatsClick(Sender: TObject);

var

buf: array [0..60] of Char;

n: Integer;

fmt: Word;

Name: string[30];

begin

MemFormats.Clear;

for n := 0 to Clipboard.FormatCount - 1 do

begin

   fmt := Clipboard.Formats[n];

   if GetClipboardFormatName(fmt, buf, Pred(SizeOf(buf))) <> 0 then

     MemFormats.Lines.Add(StrPas(buf))

   else

   begin

     case fmt of

       1: Name := 'CF_TEXT';

       2: Name := 'CF_BITMAP';

       3: Name := 'CF_METAFILEPICT';

       4: Name := 'CF_SYLK';

       5: Name := 'CF_DIF';

       6: Name := 'CF_TIFF';

       7: Name := 'CF_OEMTEXT';

       8: Name := 'CF_DIB';

       9: Name := 'CF_PALETTE';

       10: Name := 'CF_PENDATA';

       11: Name := 'CF_RIFF';

       12: Name := 'CF_WAVE';

       13: Name := 'CF_UNICODETEXT';

       14: Name := 'CF_ENHMETAFILE';

       15: Name := 'CF_HDROP (Win 95)';

       16: Name := 'CF_LOCALE (Win 95)';

       17: Name := 'CF_MAX (Win 95)';

       $0080: Name := 'CF_OWNERDISPLAY';

       $0081: Name := 'CF_DSPTEXT';

       $0082: Name := 'CF_DSPBITMAP';

       $0083: Name := 'CF_DSPMETAFILEPICT';

       $008E: Name := 'CF_DSPENHMETAFILE';

       $0200..$02FF: Name := 'private format';

       $0300..$03FF: Name := 'GDI object';

       else

         Name := 'unknown format';

     end;

     MemFormats.Lines.Add(Name);

   end;

end;

end;

 

// get rtf code from clipboard

procedure TForm1.BtnGetRTFClick(Sender: TObject);

var

MemHandle: THandle;

begin

with Clipboard do

begin

   Open;

   try

     if HasFormat(CF_RTF) then

     begin

       MemHandle := GetAsHandle(CF_RTF);

       MemExample.SetTextBuf(GlobalLock(MemHandle));

       GlobalUnlock(MemHandle);

     end

     else

       MessageDlg('The clipboard contains no RTF text!',

         mtError, [mbOK], 0);

   finally

     Close;

   end;

end;

end;

 

// set rtf code to the clipboard

procedure TForm1.BtnSetRTFClick(Sender: TObject);

const

testtext: PChar = '{\rtf1\ansi\pard\plain 12{\ul 44444}}';

testtext2: PChar = '{\rtf1\ansi' +

   '\deff4\deflang1033{\fonttbl{\f4\froman\fcharset0\fprq2 Times New Roman;}}' +

   '\pard\plain 12{\ul 44444}}';

flap: Boolean = False;

var

MemHandle: THandle;

rtfstring: PChar;

begin

with Clipboard do

begin

   if flap then

     rtfstring := testtext2

   else

     rtfstring := testtext;

   flap := not flap;

   MemHandle := GlobalAlloc(GHND or GMEM_SHARE, StrLen(rtfstring) + 1);

   if MemHandle <> 0 then

   begin

     StrCopy(GlobalLock(MemHandle), rtfstring);

     GlobalUnlock(MemHandle);

     Open;

     try

       AsText := '1244444';

       SetAsHandle(CF_RTF, MemHandle);

     finally

       Close;

     end;

   end

   else

     MessageDlg('Global Alloc failed!',

       mtError, [mbOK], 0);

end;

end;

 

end.

 

 

Code:

with Richedit1 do

begin

    selstart := perform( EM_LINEINDEX, linenumber, 0 );

    perform( EM_SCROLLCARET, 0, 0 );

end;

 

{

The EM_LINEINDEX message returns the character index of the first character

on a given line, assigning that to selstart moves the caret to that position.

The control will only automatically scroll the caret into view if it has

the focus, thus the EM_SCROLLCARET.

}

 

 

 

Code:

function GetRTFText(ARichEdit: TRichedit): string;

var

  ss: TStringStream;

  emptystr: string;

begin

  emptystr := '';

  ss := TStringStream.Create(emptystr);

  try

    ARichEdit.PlainText := False;

    ARichEdit.Lines.SaveToStream(ss);

    Result := ss.DataString;

  finally

    ss.Free

  end;

end;

 

 

procedure TForm1.Button1Click(Sender: TObject);

begin

  Memo1.Text := GetRTFText(RichEdit1);

end;

 

Code:

unit Unit1;

 

interface

 

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls,

Forms, Dialogs, StdCtrls, ComCtrls;

 

type

TForm1 = class(TForm)

   Button1: TButton;

   Button2: TButton;

   RichEdit1: TRichEdit;

   procedure RichEdit1KeyPress(Sender: TObject; var Key: Char);

private

   { Private declarations }

public

   { Public declarations }

end;

 

var

Form1: TForm1;

 

implementation

{$R *.DFM}

 

uses

richedit;

 

procedure TForm1.RichEdit1KeyPress(Sender: TObject; var Key: Char);

var

line, col, indent: integer;

S: string;

begin

if key = #13 then

begin

   key := #0;

   with sender as TRichEdit do

   begin

     {figure out line and column position of caret}

     line := PerForm( EM_EXLINEFROMCHAR, 0, SelStart );

     Col := SelStart - Perform( EM_LINEINDEX, line, 0 );

     {get part of current line in front of caret}

     S:= Copy( lines[ line ], 1, col );

     {count blanks and tabs in this string}

     indent := 0;

     while (indent < length( S )) and (S[indent + 1] in [' ', #9]) do

       Inc( indent );

     {insert a linebreak followed by the substring of blanks and tabs}

     SelText := #13#10 + Copy(S, 1, indent);

   end;

end;

end;

 

end.

 

 

Узнать положение курсора в RichEdit не составляет труда (richedit.getcaret). А вот как установить каретку в нужное место?

 

Code:

// This example doesn't use TReplaceDialog

// Ohne Benutzung von TReplaceDialog

 

function Search_And_Replace(RichEdit: TRichEdit;

SearchText, ReplaceText: string): Boolean;

var

startpos, Position, endpos: integer;

begin

startpos := 0;

with RichEdit do

begin

   endpos := Length(RichEdit.Text);

   Lines.BeginUpdate;

   while FindText(SearchText, startpos, endpos, [stMatchCase])<>-1 do

   begin

     endpos   := Length(RichEdit.Text) - startpos;

     Position := FindText(SearchText, startpos, endpos, [stMatchCase]);

     Inc(startpos, Length(SearchText));

     SetFocus;

     SelStart  := Position;

     SelLength := Length(SearchText);

     richedit.clearselection;

     SelText := ReplaceText;

   end;

   Lines.EndUpdate;

end;

end;

 

procedure TForm1.Button1Click(Sender: TObject);

begin

Search_And_Replace(Richedit1, 'OldText', 'NewText');

end;