Solved

Canvas question

Posted on 2001-08-09
4
164 Views
Last Modified: 2010-04-06
Hi!
Does somebody know how to draw an arrow?
The procedure should look something like this
Arrow(200,210,300,340);
The arrow should start at 200,210 and point to 300,340!
The tip of the arrow can look e.x like this:  ->
THX for help
0
Comment
Question by:-_-
4 Comments
 
LVL 3

Expert Comment

by:andysalih
ID: 6370188
dont you need to tell it that you want to draw on the canvas,

this will allow you to draw on the canvas,

function windowsDesktopCanvas: TCanvas;
    var
    DC: HDc;
    begin
    DC := getWindowDC ( GetDeskTopWindow );
    result := TCanvas.Create;
    result.Handle := DC;
    end;


try and excute your command now.

cheers
andy
0
 
LVL 6

Accepted Solution

by:
zebada earned 50 total points
ID: 6370884
Try this code:

unit Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Math;

type
  TForm1 = class(TForm)
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormPaint(Sender: TObject);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
  private
    { Private declarations }
    x1,x2,y1,y2: integer;

    procedure arrow(x1,y1,x2,y2: integer; sharp,size: integer);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  x1 := X;
  y1 := Y;
  x2 := X;
  y2 := Y;
end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if ( ssLeft in shift ) then
  begin
    Canvas.Pen.Color := clWhite;
    Canvas.Pen.Style := psSolid;
    Canvas.Pen.Mode := pmXor;
    Canvas.Pen.width := 1;
    Canvas.MoveTo(x1,y1);
    Canvas.LineTo(x2,y2);
    x2 := X;
    y2 := Y;
    Canvas.MoveTo(x1,y1);
    Canvas.LineTo(x2,y2);
  end;
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  x2 := X;
  y2 := Y;
  Invalidate;
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  inherited;
  if ( (x1=x2) and (y1=y2) ) then
    exit;
  arrow(x1,y1,x2,y2,30,40);
end;

procedure TForm1.arrow(x1,y1,x2,y2: integer; sharp,size: integer);
var
  sharprad: single;
  angle,len,rise,run: single;
  hx,hy: single;
begin
  Canvas.Pen.Color := clBlue;
  Canvas.Pen.Style := psSolid;
  Canvas.Pen.Mode := pmCopy;
  Canvas.Pen.width := 1;
  Canvas.MoveTo(x1,y1);
  Canvas.LineTo(x2,y2);

  // convert arrow sharpness to radians.
  sharprad := (sharp/360)*(2*pi);

  // Determine the "rise" and "run" of the line
  rise := -(y2-y1); // use -ve because real world counts from bottom up,
                    // delphi counts from top down.
  run := (x2-x1);

  // Determine the length of the line
  len := sqrt(rise*rise+run*run);

  // Determine the angle (in radians) of the line from horizontal.
  angle := arcsin(abs(rise)/len);
  if ( run<0 ) then
    angle := pi-angle;
  if ( rise<0 ) then
    angle := 2*pi-angle;

  // Determine x,y of arrow's head part 1
  hx := -cos(angle+sharprad);
  hy := sin(angle+sharprad);
  x1 := Round(x2+hx*size);
  y1 := Round(y2+hy*size);

  // Center point is the end point of the line. (x2,y2)
  Canvas.MoveTo(x2,y2);
  Canvas.LineTo(x1,y1);

  // Determine x,y of arrow's head part 2
  hx := -cos(angle-sharprad);
  hy := sin(angle-sharprad);
  x1 := Round(x2+hx*size);
  y1 := Round(y2+hy*size);

  // Center point is the end point of the line. (x2,y2)
  Canvas.MoveTo(x2,y2);
  Canvas.LineTo(x1,y1);
end;

end.
0
 

Author Comment

by:-_-
ID: 6373270
Thanks! By the way, why do you need "inherited;" command in TForm1.FormPaint??? It works also without!
0
 

Expert Comment

by:bebakhshid
ID: 8871154
what should i do with (i/o errors32,...)except{$i-} while working with files in win xp
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now