Author: Jonas Bilinkevicius
How to display text from a TRichEdit on the canvas of a TGraphicControl
Answer:
If you just want to view the text you can make a component derived from
TGraphicControl. The following code shows how to put text from a TRichEdit control
on the canvas of the TGraphicControl.
1
2 {rtf is a property of the TRICHLABEL of the type TRichEdit }
3
4 procedure TRICHLABEL.Paint;
5 var
6 myFormatRange: TFormatRange;
7 myCharRange: TCharRange;
8 myRect: TRect;
9 OldMap: Integer;
10 LastChar: Integer;
11 rc: TRect;
12 hDC, hDCTarget: THandle;
13 begin
14 if not Assigned(rtf) then
15 exit;
16 FillChar(myFormatRange, sizeof(TFormatRange), 0);
17 myCharRange.cpMin := 0;
18 myCharRange.cpMax := -1;
19 canvas.brush.color := BackColor;
20 if FTransparent then
21 Canvas.brush.style := bsClear
22 else
23 begin
24 Canvas.brush.style := bsSolid;
25 Canvas.FillRect(ClientRect);
26 end;
27 with myFormatRange do
28 begin
29 hDC := canvas.handle;
30 hDCTarget := canvas.handle;
31 rc := Rect(0, ptOrigin.y * 15, Width * 15, ptOrigin.y * 15 + Height * 15);
32 chrg.cpMin := 0;
33 chrg.cpMax := -1;
34 end;
35 LastChar := SendMessage(rtf.handle, EM_FORMATRANGE, 1, LPARAM(@myFormatRange));
36 myRect := Rect(0, 0, width * 15, height * 15);
37 SendMessage(rtf.handle, EM_DISPLAYBAND, 0, LPARAM(@myRect));
38 LastChar := SendMessage(rtf.handle, EM_FORMATRANGE, 0, LPARAM(nil));
39 end;
|