Solved

Canvas question

Posted on 2001-08-09
4
168 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:-_-
[X]
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
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
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…

734 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