Tutorials to .com

Tutorials to .com » Software » Delphi » Window Paint program draw a simple curve to achieve

Window Paint program draw a simple curve to achieve

Print View , by: iSee ,Total views: 32 ,Word Count: 557 ,Date: Mon, 24 Aug 2009 Time: 10:08 PM

The following is the previous one into the program, imitating Windows Paint program in drawing curves. In fact, terribly simple in principle, that is, a PolyBezier function.
That may be interested in a package into a class, to facilitate future use.
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TStep = (sStartEnd, sCtrl1, sCtrl2);
TForm1 = class (TForm)
Button1: TButton;
procedure FormCreate (Sender: TObject);
procedure FormMouseDown (Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormMouseMove (Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure FormMouseUp (Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormPaint (Sender: TObject);
private
(Private declarations)
isDown: Boolean;
isOK: Boolean; / / identifies a Bezier line drawing is completed
Step: TStep; / / instruction to that stage of the
StartP: TPoint; / / The following is a four-point coordinates
EndP: TPoint;
CtrP1: TPoint;
CtrP2: TPoint;
public
(Public declarations)
end;

var
Form1: TForm1;

implementation

($ R *. dfm)

procedure TForm1.FormCreate (Sender: TObject);
begin
self.DoubleBuffered: = true;
isDown: = False;
Step: = sStartEnd;
isOK: = False;
end;

procedure TForm1.FormMouseDown (Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
isDown: = true;
if Button = mbLeft then
begin
if (Step = sStartEnd) then
begin
StartP.X: = X;
StartP.Y: = Y;
EndP: = Point (X, Y);
isOK: = False;
end
else if (Step = sCtrl1) then
begin
CtrP1: = Point (X, Y);
CtrP2: = Point (X, Y);
end
else if (Step = sCtrl2) then
begin
CtrP2: = Point (X, Y);
end;
end;
end;

procedure TForm1.FormMouseMove (Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if isDown and (Shift = [ssLeft]) then
begin
if (Step = sStartEnd) then
begin
EndP.X: = X;
EndP.Y: = Y;
end
else if Step = sCtrl1 then
begin
CtrP1: = Point (X, Y);
CtrP2: = Point (X, Y);
end
else if (Step = sCtrl2) then
begin
CtrP2: = Point (X, Y);
end;
self.Invalidate;
end;
end;

procedure TForm1.FormMouseUp (Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if isDown and (Button = mbLeft) then
begin
isDown: = False;
if Step = sStartEnd then Step: = sCtrl1
else if Step = sCtrl1 then Step: = sCtrl2
else if Step = sCtrl2 then begin Step: = sStartEnd; isOK: = True; end;
end;
end;

procedure TForm1.FormPaint (Sender: TObject);
begin
if not isOK then
begin
if Step = sStartEnd then
begin
self.Canvas.MoveTo (StartP.X, StartP.Y);
self.Canvas.LineTo (EndP.X, EndP.Y);
end
else begin
Self.Canvas.PolyBezier ([StartP, CtrP1, CtrP2, EndP]);
end;
end else begin
Self.Canvas.PolyBezier ([StartP, CtrP1, CtrP2, EndP]);
end;
end;

end.


Delphi Tutorial Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.