Draw a line on canvas with custom style (delphi)

I need to draw some lines on the canvas, however I need to use my own style, custom style as solid, dot, ...

For example, I need to draw a string as "__. __. _" Or ". _. _. _. _". My whole line is a combination of dashes and dots, and I need to set also the length and dash of the stroke, the width of the point.

I do not want to use GDI + or another external library ...

Is there an easy way to do this?

+6
source share
2 answers

You can do this with a simple 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; 
+8
source

You can also use the LineDDA API . Of course, the pen style (as advised by Andreas Reggrand) is much faster, but LineDDA allows you to draw parts of the line in different colors.

 var DottedLineDrawCounter: Integer; procedure DDAProc(AX, AY: Integer; ACanvas: TCanvas); stdcall; begin if DottedLineDrawCounter mod 4 = 0 then ACanvas.Pixels[AX, AY] := clRed else ACanvas.Pixels[AX, AY] := clBlack; Inc(DottedLineDrawCounter); end; ... begin // Usage DottedLineDrawCounter := 0; LineDDA(X1, Y1, X2, Y2, @DDAProc, LPARAM(Canvas)); end; 
+2
source

All Articles