Mega Code Archive

 
Categories / Delphi / Graphic
 

Dashed and Dotted Lines

Title: Dashed and Dotted Lines Question: How do you draw dashed and dotted lines with a pen width greater than 1 ? Answer: The Pen styles psDash and psDot don't work for a Pen width greater than 1. With the API function ExtCreatePen you can draw dotted and dashed lines of any width and style, but only for Win2K or NT. Specify a geometric pen with a certain userstyle, defined by an array. The values you put in this userstyle array correspond to the size of the dash, then the size of the first gap, then the next dash, and so on. See also How to draw custom dashed/dotted lines on a canvas. - by Borland Developer Support Staff http://community.borland.com/article/0,1410,26475,00.html The following examples demonstrates this. It draws a custom line of width w, which can for example be specified by a TrackBar. procedure TForm1.FormPaint(Sender: TObject); var LogBrush : tLogBrush; Userstyle : array[0..3] of integer; begin UserStyle[0]:=w*4; UserStyle[1]:=w*2; UserStyle[2]:=w*4; UserStyle[3]:=w*2; LogBrush.lbStyle := BS_Solid; LogBrush.lbColor := $ff; LogBrush.lbHatch := 0; Canvas.Pen.Handle:=ExtCreatePen(PS_GEOMETRIC or PS_USERSTYLE, w, LogBrush, 4, @Userstyle); canvas.MoveTo(20,20); canvas.LineTo(20+200,220); end; Use the following UserStyle Arrays for dotted, dashed and mixed lines: psDot UserStyle[0]:=w div 2; UserStyle[1]:=w*2; UserStyle[2]:=w div 2; UserStyle[3]:=w*2; psDash UserStyle[0]:=w*4; UserStyle[1]:=w*2; UserStyle[2]:=w*4; UserStyle[3]:=w*2; psDashDot UserStyle[0]:=w div 2; UserStyle[1]:=w*2; UserStyle[2]:=w*4; UserStyle[3]:=w*2;