2010. június 30., szerda

How to draw a line in a RichEdit (just like in the Deplhi Editor)


Problem/Question/Abstract:

How to draw a line in a RichEdit (just like in the Delphi editor)

Answer:

procedure DrawLine;

var
  aCanvas: Tcanvas;
  X1, X2, Y1: Integer;
  byLineLength: BYTE;

begin
  byLineLength := 80; // Draw the line after 80 chars on the RichEdit
  aCanvas := TCanvas.Create;
  Y1 := RichEdit1.Height;
  try
    aCanvas.Handle := GetDC(RichEdit1.Handle);
    aCanvas.Font := RichEdit1.Font;
    X1 := aCanvas.TextWidth('W');
    X2 := aCanvas.TextWidth('i');
    aCanvas.Pen.color := clSilver; // Color of line
    if X1 = X2 then // Check for fixed or variable font
    begin
      aCanvas.MoveTo(byLineLength * X1, 0);
      aCanvas.LineTo(byLineLength * X1, Y1);
    end;
  finally
    ReleaseDC(RichEdit1.Handle, aCanvas.Handle);
    aCanvas.Free;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése