Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw a line on canvas with custom style (delphi)

I need draw some lines on a canvas, however I need use a custom style, no standart style as solid, dot, ...

For example I need draw a line as "__ . __ . _" or " . _ . _ . _ . _". All my line are a mix of dash and dot and I need set also dash lenght and dash, dot width.

I don't want use GDI+ or other external library...

Is there a simple way to do it?

like image 523
Martin Avatar asked Dec 09 '12 16:12

Martin


1 Answers

You can do this with plain GDI:

procedure TForm1.FormPaint(Sender: TObject);
const
  pattern: array[0..3] of cardinal = (10, 1, 1, 1);
var
  lb: TLogBrush;
  pen, oldpen: HPEN;
begin
  lb.lbStyle := BS_SOLID;
  lb.lbColor := RGB(255, 0, 0);
  pen := ExtCreatePen(PS_COSMETIC or PS_USERSTYLE, 1, lb, length(pattern), @pattern);
  if pen <> 0 then
    try
      oldpen := SelectObject(Canvas.Handle, pen);
      Canvas.MoveTo(0, 0);
      Canvas.LineTo(ClientWidth, ClientHeight);
      SelectObject(Canvas.Handle, oldpen);
    finally
      DeleteObject(pen);
    end;
end;
like image 68
Andreas Rejbrand Avatar answered Oct 04 '22 00:10

Andreas Rejbrand