紧急求救!!!!! (怎样才能用直线把两个Image相连?)(100分)

  • 主题发起人 主题发起人 yoyorr
  • 开始时间 开始时间
Y

yoyorr

Unregistered / Unconfirmed
GUEST, unregistred user!
我是一个Delphi的初学者,因学习需要,设计一个软件,其中要实现以下功能,
希望大侠们可以指教一下:

要求:
在一个paintbox上有两个image,现在需要把两个image用直线连起来(线的两头分别要在
两个image上)。
开始连线时点击一个image(作起点)后移动鼠标可拉出一条直线,直线顺着鼠标移动的方
向伸展,直到鼠标点击了另外一个image上的某点(作终点)。
画好直线后,如果移动其中一个image,直线也能跟着伸长或缩短, 而不能与两个image
断开。

 
用moveto(x,y),lineto(x,y)函数:
1.存储该直线的起始坐标point1(相对于image1).
2.存储该直线的终点坐标point2(相对于image2).
3.存储image1的起始坐标point3(相对于Form1)与宽度W3及高度H3.
4.存储image2的起始坐标point4(相对于Form1)与宽度W4及高度H4.
5.如移动image1,在image1的onMouseUp中,存储image1的当前起始坐标point5(相对于Form1),
该直线的当前起始坐标即为(point5.x+point1.x,point5.y+point1.y)
6.如移动image2,在image2的onMouseUp中,存储image2的当前起始坐标point6(相对于Form1),
该直线的终点起始坐标即为(point6.x+point1.x,point6.y+point1.y)
image的移动您自己做吧.

 
想作关联?象SQL Server中的?:)
 
///////////////////////////////////////////////
mouse left down:
bDragging:= true;
startpoint = {current position}
///////////////////////////////////////////////
mouse move:
if (bDragging = true)
begin
canvas......(画笔的模式是 xor)
line(startpoint, endpoint);
endpoint = {current position}
end;
/////////////////////////////////////////////////
mouse left down:
line(startpoint, endpoint);
////////////////////////////////////////////////
图像移动的处理与上面的方法大同小异。
 
直线能不能用Image来实现呢,效果和作用又怎样呢?
 
我的毕业设计和这个问题有相同之处

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Panel1: TPanel;
Image1: TImage;
Shape1: TShape;
Shape2: TShape;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
SpeedButton3: TSpeedButton;
procedure ShapeMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure ShapeMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure ShapeMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Shape1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure Shape1DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure Shape2DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure SpeedButton2Click(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
MouseDownSpot:Tpoint;
Capturing:bool;
Drawing:Boolean;
Origin,MovePt:TPoint;
Linked:bool;
end;

var
Form1: TForm1;

implementation

{$R *.DFM}
{具体编程时还要考虑到pmNotXor,以及互相连通的关系}
procedure TForm1.ShapeMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if SpeedButton1.Down then
begin
Capturing:=true;
MouseDownSpot.X:=x;
MouseDownSpot.Y:=Y;
if Linked then
Drawing:=True;
begin
if Sender = Shape1 then
begin

Origin:=Point(Shape2.Left+13,Shape2.Top+13);
MovePt:=Point(Shape1.Left+13,Shape1.Top+13);

end
else
begin

Origin:=Point(Shape1.Left+13,Shape1.Top+13);
MovePt:=Point(Shape2.Left+13,Shape2.Top+13);

end;
end;
end;
end;
procedure TForm1.ShapeMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if SpeedButton1.Down then
begin
if Capturing then
begin
(Sender as TShape).Left:=(Sender as TShape).Left-(MouseDownSpot.x-x);
(Sender as TShape).Top:=(Sender as TShape).Top-(MouseDownSpot.y-y);
end;
if Linked then
begin
if Drawing then
begin
with Image1 do
begin
if Sender = Shape1 then
begin
Canvas.Pen.Mode:=pmNotXor;
Canvas.MoveTo(MovePt.x,MovePt.y);
Canvas.LineTo(Origin.X,Origin.Y);
MovePt:=Point(Shape1.Left+13,Shape1.Top+13);
Canvas.MoveTo(MovePt.x,MovePt.y);
Canvas.LineTo(Origin.X,Origin.Y);
end
else
begin
Canvas.Pen.Mode:=pmNotXor;
Canvas.MoveTo(Origin.X,Origin.Y);
Canvas.LineTo(MovePt.x,MovePt.y);
MovePt:=Point(Shape2.Left+13,Shape2.Top+13);
Canvas.MoveTo(Origin.X,Origin.Y);
Canvas.LineTo(MovePt.x,MovePt.y);
end;
end;
end;
end;
end;
end;

procedure TForm1.ShapeMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if SpeedButton1.Down then
begin
if Capturing then
begin
Capturing:=false;
(Sender as TShape).Left:=(Sender as TShape).Left-(MouseDownSpot.x-x);
(Sender as TShape).Top:=(Sender as TShape).Top-(MouseDownSpot.y-y);
end;

Drawing:=False;

end;
end;

procedure TForm1.Shape1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept:=(Source = Shape2);
end;

procedure TForm1.Shape1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
Image1.Canvas.MoveTo(Shape1.Left+13,Shape1.Top+13);
Image1.Canvas.LineTo(Shape2.Left+13,Shape2.Top+13);
if SpeedButton2.Down then Linked:=False;
if SpeedButton3.Down then Linked:=True;
end;

procedure TForm1.Shape2DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept:=(Source = Shape1);
end;

procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
Shape1.DragMode:=dmAutomatic;
Shape2.DragMode:=dmAutomatic;
Image1.Canvas.Pen.Mode:=pmNotCopy;
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
Shape1.DragMode:=dmManual;
Shape2.DragMode:=dmManual;
end;

procedure TForm1.SpeedButton3Click(Sender: TObject);
begin
Shape1.DragMode:=dmAutomatic;
Shape2.DragMode:=dmAutomatic;
Image1.Canvas.Pen.Mode:=pmCopy;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Image1.Canvas.Ellipse(-1,-1,-1,-1);
end;

end.
 
到delphi深度历险(好象是http://vcl.vclxx.com/,不行就用新浪搜一下)站点去下载一个带
源码的TLine构件,看看源码你就都会了。
 
虽然不难,但确实要细心,要肯懂脑筋,要不还真做不出来。
 
多人接受答案了。
 

Similar threads

D
回复
0
查看
909
DelphiTeacher的专栏
D
D
回复
0
查看
704
DelphiTeacher的专栏
D
D
回复
0
查看
753
DelphiTeacher的专栏
D
D
回复
0
查看
659
DelphiTeacher的专栏
D
D
回复
0
查看
666
DelphiTeacher的专栏
D
后退
顶部