首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

怎么先画一条直线后然后继续从该直线的起点再画一条形成两直线构成一个夹角

2012-03-11 
如何先画一条直线后然后继续从该直线的起点再画一条形成两直线构成一个夹角我老是控制不好!谁有更好的办法

如何先画一条直线后然后继续从该直线的起点再画一条形成两直线构成一个夹角
我老是控制不好!谁有更好的办法!(前面那根线一定要保留!以便计算夹角)!谢谢!

[解决办法]
以下代码供参考:

unit Unit1;

interface

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

type
TForm1 = class(TForm)
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure FormPaint(Sender: TObject);
private
{ Private declarations }
points: array[0..2] of TPoint;
pointCount: Integer;
Drawing: Boolean;
MouseOrg, NextPoint: TPoint;
public
{ Public declarations }
procedure DrawLine(mode: TPenMode);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.DrawLine(mode: TPenMode);
begin
if pointCount > 0 then
begin
Canvas.Pen.Mode := mode;
Canvas.MoveTo(MouseOrg.X, MouseOrg.Y);
Canvas.LineTo(NextPoint.X, NextPoint.Y);
end;
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button <> mbLeft then Exit;
if not Drawing then
begin
if pointCount > 0 then
begin
pointCount := 0;
Invalidate;
end;
MouseOrg := Point(X, Y);
points[pointCount] := MouseOrg;
NextPoint := MouseOrg;
Drawing := True;
Inc(pointCount);
end else
begin
DrawLine(pmCopy);
MouseOrg := NextPoint;
points[pointCount] := NextPoint;
Inc(pointCount);
if pointCount = 3 then
begin
NextPoint := points[0];
Drawing := False;
end;
end;
end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if Drawing then
begin
DrawLine(pmNotXor);
NextPoint := Point(X, Y);
DrawLine(pmNotXor);
end;
end;

procedure TForm1.FormPaint(Sender: TObject);
var
i: Integer;
begin
if pointCount = 0 then Exit;
Canvas.Pen.Mode := pmCopy;
for i := 0 to pointCount - 2 do
begin
Canvas.MoveTo(Points[i].X, Points[i].Y);
Canvas.LineTo(Points[i + 1].X, Points[i + 1].Y);
end;
end;

end.

热点排行