2007. február 16., péntek

How to display text from a TRichEdit on the canvas of a TGraphicControl


Problem/Question/Abstract:

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.

{rtf is a property of the TRICHLABEL of the type TRichEdit }

procedure TRICHLABEL.Paint;
var
  myFormatRange: TFormatRange;
  myCharRange: TCharRange;
  myRect: TRect;
  OldMap: Integer;
  LastChar: Integer;
  rc: TRect;
  hDC, hDCTarget: THandle;
begin
  if not Assigned(rtf) then
    exit;
  FillChar(myFormatRange, sizeof(TFormatRange), 0);
  myCharRange.cpMin := 0;
  myCharRange.cpMax := -1;
  canvas.brush.color := BackColor;
  if FTransparent then
    Canvas.brush.style := bsClear
  else
  begin
    Canvas.brush.style := bsSolid;
    Canvas.FillRect(ClientRect);
  end;
  with myFormatRange do
  begin
    hDC := canvas.handle;
    hDCTarget := canvas.handle;
    rc := Rect(0, ptOrigin.y * 15, Width * 15, ptOrigin.y * 15 + Height * 15);
    chrg.cpMin := 0;
    chrg.cpMax := -1;
  end;
  LastChar := SendMessage(rtf.handle, EM_FORMATRANGE, 1, LPARAM(@myFormatRange));
  myRect := Rect(0, 0, width * 15, height * 15);
  SendMessage(rtf.handle, EM_DISPLAYBAND, 0, LPARAM(@myRect));
  LastChar := SendMessage(rtf.handle, EM_FORMATRANGE, 0, LPARAM(nil));
end;

Nincsenek megjegyzések:

Megjegyzés küldése