Setting the Line End Styles for Canvas.LineTo

2020-03-23 02:38发布

问题:

Is there any way to set the style for the lineends for the TCanvas.LineTo method? It seems to default to rounded ends, which looks very bad for several lines in a row of different colours when Pen.Width is set to a large value (e.g. 9).

It looks like this (rounded ends):

 ********........******
**********........******
**********........******
 ********........******

(where * is e.g. blue and . is yellow)

It is even worse if the two outer lines are drawn after the middle line:

 ********........******
**********......********
**********......********
 ********........******

I'd like it to look like this (streight ends):

 ********........******
 ********........******
 ********........******
 ********........******

Pen does not seem to offer any setting for this and neither does the LineTo method. Is there maybe a windows API function I could call?

回答1:

Maybe ExtCreatePen helps. Check the PS_ENDCAP_* and PS_JOIN_* flags.



回答2:

Sample code from DelphiPraxis forum (German language)

procedure TForm1.FormCreate(Sender: TObject);
   var LogBrush:TLOGBRUSH;
begin
   ZeroMemory(@LogBrush, SizeOf(LogBrush));
   LogBrush.lbColor:=ColorToRGB(Canvas.Pen.Color);
   LogBrush.lbHatch:=0;

   DeleteObject(Canvas.Pen.Handle);
   Canvas.Pen.Handle:=ExtCreatePen(PS_Geometric or PS_Solid or PS_EndCap_Square or PS_Join_Miter, 10, LogBrush, 0, nil);
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
   Canvas.MoveTo(0, 0);
   Canvas.LineTo(50, 50);
end;