1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
procedure RTFToRichEdit(const aRtf: String; pRichEdit: TRichEdit);
var
aStream2 : TStream;
begin
aStream2 := TStringStream.Create(aRtf);
aStream2.Position := 0;
pRichEdit.Lines.LoadFromStream(aStream2);
aStream2.Free;
end;
function EditStreamReader(dwCookie: DWORD; pBuff: Pointer; cb: Longint; pcb: PLongInt): DWORD; stdcall;
begin
Result := $0000; // assume no error
try
pcb^ := TStream(dwCookie).Read(pBuff^, cb); // read data from stream
except
Result := $FFFF; // indicates error to calling routine
end;
end;
procedure RTFInsertStream(const RE: TRichEdit; const Stream: TStream);
var
EditStream: TEditStream; // callback used to read inserted RTF
begin
RE.Lines.BeginUpdate;
try
// Make sure rich edit is large enough to take inserted code
RE.MaxLength := RE.MaxLength + Stream.Size;
// Stream in the RTF via EM_STREAMIN message
EditStream.dwCookie := DWORD(Stream);
EditStream.dwError := $0000;
EditStream.pfnCallback := @EditStreamReader;
RE.Perform(EM_STREAMIN, SFF_SELECTION or SF_RTF or SFF_PLAINRTF, LPARAM(@EditStream));
// Report any errors as a bug
if EditStream.dwError <> $0000 then
raise Exception.Create('RTFInsertStream: Error inserting stream');
finally
RE.Lines.EndUpdate;
end;
end;
function TableauRTF(const Nbcol, Nbligne, Largeur: Integer): string;
var
i, j: Integer;
begin
Result := '{\rtf1\trowd';
for i := 1 to Nbcol do
Result := Result + format('\cellx%d', [i * (2000 + Largeur)]);
for j := 1 to Nbligne do
begin
Result := Result + '\intbl';
for i := 1 to Nbcol do
Result := Result + '\cell';
Result := Result + '\row';
end;
Result := Result + '}';
end;
procedure TForm1.FormCreate(Sender: TObject);
var
textRTF: String;
begin
textRTF :=
'{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Calibri;}}{\colortbl ;\red0\green77\blue187;}{\*\generator Riched20 10.0.18362}\viewkind4\uc1\pard\sa200\sl276\slmult1\cf1\b\f0\fs22\lang12 coucou\cf0\b0\par}';
RTFToRichEdit(textRTF, RichEdit1);end;
procedure TForm1.Btn_InsereTableauClick(Sender: TObject);
var
sTableau: String;
aStream: TStream;
begin
sTableau := TableauRTF(3, 7, 150);
aStream := TStringstream.Create(sTableau);
try
aStream.Position := 0;
RTFInsertStream(RichEdit1, aStream);
finally
aStream.Free;
end;
end; |
Partager