Solved

Canvas question

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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