Arrow Tool  for drawing on canvas - more points available

Posted on 2002-03-13
Last Modified: 2010-04-05
I want to be able to create an arrow tool like the one in MS Word drawing toolbar - that is full head arrow (always filled) with either 1 or 2 heads. Possibility of dotted or dashed  shaft is an optional extra.

It also needs to be able to stretch like a line would on the canvas, determined by where the user places the mouse on the canvas.

any solutions greatly appreciated.

More points available for fullness of answer.


Question by:Vanessa
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 6863626
LVL 34

Expert Comment

ID: 6863633
I can not guess what you want? An Arrow Tool? Here is some code to make an arrow shape on a Canvas.

ArPoints: array[0..7] of TPoint;

Canvas.Brush.Color := clRed;
Canvas.Pen.Color := clRed;
{these points draw an Arrow with Polygon( )}
  ArPoints[0].x := 2;
  ArPoints[0].y := 8;
    ArPoints[1].x := 14;
    ArPoints[1].y := 2;
  ArPoints[2].x := 14;
  ArPoints[2].y := 6;
    ArPoints[3].x := 20;
    ArPoints[3].y := 6;
  ArPoints[4].x := 20;
  ArPoints[4].y := 10;
    ArPoints[5].x := 14;
    ArPoints[5].y := 10;
  ArPoints[6].x := 14;
  ArPoints[6].y := 14;
    ArPoints[7].x := 2;
    ArPoints[7].y := 8;

I do not understand "It also needs to be able to stretch like a line would on the canvas" Do lines streach? What is your goal? what are you trying to let the user do with your program, draw an arrow, make a line between 2 points that the user clicks?

Expert Comment

ID: 6863810
Sample "rubberband" arrow drawing program can be downloaded from
It can easily be modified to do what you want.
If you want me to modify it let me know.

There are other sample programs as well:
Most of these were developed in response to questions asked on EE.

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 6866196
Fuller explanation follows

The user has to click on a button then drag from pointA to pointB and on mouse up an arrow appears.  The head on the arrow is at pointB.  There can be multiple arrows on the canvas at anytime. Ideally they should be able to select whether they want a solid line arrow, a dotted or dashed arrow of varying thicknesses.  

I am using a paintbox for the canvas, which then coverts to a bitmap and the picture is saved in the database.

Your assistence slick182 and zebada have been astounding.  Thanks for the fullness of your explanations.

LVL 34

Expert Comment

ID: 6873472
Have you tried to combine the code I listed for
and the code in zebada's arrow zip? that should be enough to get you started. Do you have some code you tried for this?
LVL 34

Expert Comment

ID: 6878782
Had time this weekend, so I did an arrow draw, I did it with a 7 point array for Polygon, but you could use a 4 point array and just draw a wide line for the arrow tail

    { Private declarations }
    DoArrow, ReArrow, Start1: Boolean;
    StartArrow, ALPt: TPoint;
    ArrowPoints: Array[0..7] of TPoint;

procedure TForm1.Button_DrawArrowClick(Sender: TObject);
Start1 := True;

procedure TForm1.PaintBox5MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
if Start1 then
  DoArrow := True;
  Start1 := False;
  PaintBox5.Canvas.Brush.Style := bsClear;
  PaintBox5.Canvas.Pen.Color := clBlack;
  PaintBox5.Canvas.Pen.Style := psSolid;
  PaintBox5.Canvas.Pen.Mode := pmNot;
  PaintBox5.Canvas.Pen.Width := 1;

  StartArrow.x := X;
  StartArrow.y := Y;
  ALPt.x := X;
  ALPt.y := Y;

procedure TForm1.PaintBox5MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
Angle, Rad1: Extended;
HalfHWidth, HalfTWidth, AheadLength: Integer;
if DoArrow then
  if (abs(StartArrow.x - X) < 14) and
  (abs(StartArrow.y - Y) < 14) and
  not ReArrow then Exit;
  {since this arrow head is 17 pixels long then don't start drawing
  until the cursor moves 14 pixels}
  if ReArrow then
    {erase the arrow}
    PaintBox5.Canvas.LineTo(ALPt.x, ALPt.y);
  Angle := 180 * (1 + ArcTan2(Y-StartArrow.y, X-StartArrow.x) / Pi);
  {I use a trig function to get the angle from the rise and run (Y-StartArrow.y, X-StartArrow.x)
  ArcTan2( )  returns a Radian value from the rise (Cos-Y) and run (Sin-X) values
  I convert it to Angle mesurement, because it's easier than radians for me to use}
  AheadLength := 17;
  {set the length of the arrow head here by changing AheadLength}
  Rad1 := DegToRad(Angle+180);
  {reverse the angle and move 17 pixels back toward StartPoint for the length of the arrow head}
  ALPt.x := Round(X-Cos(Rad1)*AheadLength);
  ALPt.y := Round(Y-Sin(Rad1)*AheadLength);
  PaintBox5.Canvas.LineTo(ALPt.x, ALPt.y);
  ArrowPoints[0].x := X;
  ArrowPoints[0].y := Y;
  {build your point array, start with the current cursor Position}
  Rad1 := DegToRad(Angle+90);
  {now rotate your angle 90 degrees}
  HalfHWidth := 7;
  HalfTWidth := 2;
  {set your half Head and Tail Width}
  ArrowPoints[1].x := Round(ALPt.x-(Cos(Rad1)*HalfHWidth));
  {the Cos( ) gives you the X factor to mutiply your X mesurements
   and Sin( ) gives you the Y}
  ArrowPoints[1].y := Round(ALPt.y-(Sin(Rad1)*HalfHWidth));
  {you have rotated the angle 90 so move the point out 7
  pixels from ALPt point for arrow head width}
  ArrowPoints[2].x := Round(ALPt.x-(Cos(Rad1)*HalfTWidth));
  ArrowPoints[2].y := Round(ALPt.y-(Sin(Rad1)*HalfTWidth));
  {move the point out 2 pixels for the arrow tail width}
  ArrowPoints[3].x := Round(StartArrow.x-(Cos(Rad1)*HalfTWidth));
  ArrowPoints[3].y := Round(StartArrow.y-(Sin(Rad1)*HalfTWidth));
  {now do the start point}
  Rad1 := DegToRad(Angle-90);
  {rotate angle 90 other way}
  ArrowPoints[4].x := Round(StartArrow.x-(Cos(Rad1)*HalfTWidth));
  ArrowPoints[4].y := Round(StartArrow.y-(Sin(Rad1)*HalfTWidth));

  ArrowPoints[5].x := Round(ALPt.x-(Cos(Rad1)*HalfTWidth));
  ArrowPoints[5].y := Round(ALPt.y-(Sin(Rad1)*HalfTWidth));

  ArrowPoints[6].x := Round(ALPt.x-(Cos(Rad1)*HalfHWidth));
  ArrowPoints[6].y := Round(ALPt.y-(Sin(Rad1)*HalfHWidth));
  {because all the Radian values are Rounded, small HalfTWidth like
    2 pixels may not be consistant}

  ArrowPoints[7] := ArrowPoints[0];
  {close the polygon}

  ReArrow := True;

procedure TForm1.PaintBox5MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
if DoArrow and ReArrow then
  PaintBox5.Canvas.Brush.Style := bsSolid;
  PaintBox5.Canvas.Brush.Color := clAqua;
  PaintBox5.Canvas.Pen.Color := clBlue;
  PaintBox5.Canvas.Pen.Style := psSolid;
  PaintBox5.Canvas.Pen.Mode := pmCopy;

  {below are some alternate ways to draw the arrow tail}
  {PaintBox5.Canvas.Pen.Width := 3;
  PaintBox5.Canvas.Pen.Width := 1;
  PaintBox5.Canvas.Pen.Color := clWhite;
ReArrow := False;
DoArrow := False;

- - - - - - - - - - - - - - - -
if you're going to do a graphics program, you might practice using the drawng methods available for TCanvas
LVL 34

Accepted Solution

Slick812 earned 100 total points
ID: 6879070
you might look at this About web site for delphi graphics

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses
Course of the Month5 days, 12 hours left to enroll

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question