Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Canvas question

Posted on 2001-08-09
4
Medium Priority
?
180 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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

916 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