Solved

Canvas question

Posted on 2001-08-09
4
165 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
code issue 8 157
Show Listview image from database (String field) 5 138
Controlled Assessment GCSE - desperate help needed 4 103
Delphi and Access based Enumeration 9 72
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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