以我理解的意思写段代码给你:
var
BeginPoint, EndPoint: TPoint; //用来保存画图时起始点坐标和终止的坐标
myImage : TImage;//临时创建用来加载小图片的控键
procedure TForm1.PaintBoxMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
BeginPoint := Point(X, Y);
EndPoint := Point(X, Y);
end;
procedure TForm1..PaintBoxMouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
begin
EndPoint := Point(X, Y);
Self.Canvas.Pen.Style := psDot;
Self.Canvas.Brush.Style := bsClear;
Self.Canvas.Rectangle(BeginPoint.X, BeginPoint.Y, EndPoint.X + 1, EndPoint.Y + 1);//画一个虚线框,来标注新的图形的位置
end;
procedure TForm..PaintBoxMouseUp(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
myImage := TImage.Create(Self);
myImage.Parent := Self;
myImage.Top := BeginPoint.X;
myImage.Left := BeginPoint.Y;
myImage.Width := abs(EndPoint.X - BeginPoint.X);
myImage.Height := abs(EndPoint.Y - BeginPoint.Y);
myImage.Visible := True;
if OpenPictureDialog1.Execute then //加载图片
myImage.LoadFromFile(OpenPictureDialog1.FileName);
end;
这是比较简单的办法,应该可以实现你说的功能。如果需要创建多个这样的图形对象,你可以另外再建一个结构:
var GraphicsList : TList;
在MourseUp中加入:GraphicsList.add(myImage);
如果需要对新增加的图形拖放,建议你用派生类,创建新的图形对象。