我的毕业设计和这个问题有相同之处
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.