Tester sous Delphi7
Voici ce morceau de code qui permet dessiner le texte d'un richedit sur une image (.bmp)le code n'est pas très élégant mais reste utilisable
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
Uses richedit;
procedure PrintRichedit(ARichEdit:TRichEdit;ACanvas:TCanvas;SLine:Integer=0;ELine:Integer=-1);
var
  CharRange: TFormatRange;
  sBitmap:TBitmap;
  SPos,EPos:Integer;
begin
    sBitmap:=TBitmap.Create;
    FillChar(CharRange, SizeOf(TFormatRange), 0);
 try
  with ARichEdit do
    begin
      sBitmap.Height:=ClientHeight;
      sBitmap.Width:=ClientWidth;
      sBitmap.Canvas.Brush.Color:=Color;
      sBitmap.Canvas.FillRect(ClientRect);
      if(ELine<>-1) then inc(ELine);
      SPos:=Perform(EM_LINEINDEX,SLine,0);
      EPos:=Perform(EM_LINEINDEX,ELine,0);
  with sBitmap.Canvas, CharRange do
    begin
      chrg.cpMin:=0;
      chrg.cpMax := -1;
      if(((SPos<>-1)and (EPos<>-1))and (SPos <= EPos)) then
      begin
        chrg.cpMin:=SPos;
        chrg.cpMax:=EPos;
      end;
      hdc := Handle;
      rc:=ClientRect;
      hdcTarget := Handle;
      rc.left:=0;
      rc.top :=0;
      rc.right :=MulDiv(rc.right,1440,GetDeviceCaps(Handle, LOGPIXELSX));
      rc.bottom :=MulDiv(rc.bottom,1440,GetDeviceCaps(Handle, LOGPIXELSY));
      rcPage := rc;
    end;
      Perform(EM_FORMATRANGE, 0, 0);
      Perform(EM_FORMATRANGE, 1, Longint(@CharRange));
      Perform(EM_FORMATRANGE, 0, 0);
      TransparentBlt(ACanvas.Handle,2,2,ClientWidth,ClientHeight,
      sBitmap.Canvas.Handle,0,0,ClientWidth,ClientHeight,ColorToRgb(Color));
  end;
 finally
    sBitmap.Free;
 end;
end;
Utilisation
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
procedure TForm1.Button1Click(Sender: TObject);
begin
  PrintRichedit(RichEdit1,Image1.Canvas,0,10);
  Image1.Invalidate;
end;
le toisième et quatrième paramètre sont les début et fin des lignes sélectionnées mais attention à la property WordWrap .