procedure TForm1.Shape1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if GetKeyState(1)>=0 then exit;
(Sender as TShape).Left:=(Sender as TShape).Left+X-mousep.X;
(Sender as TShape).Top:=(Sender as TShape).Top+y-mousep.Y;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
bmp:Tbitmap;
R:TRect;
begin
bmp:=Tbitmap.Create;
bmp.PixelFormat:=image1.Picture.Bitmap.PixelFormat;
bmp.Width:=200;
bmp.Height:=300;
R:=Shape1.ClientRect;
OffsetRect(R,Shape1.Left,Shape1.Top);
bmp.Canvas.CopyRect(bmp.Canvas.ClipRect,image1.Picture.Bitmap.Canvas,R);
bmp.SaveToFile('D:\mwy.bmp');
bmp.Free;
end;
end.
窗体代码如下:
object Form1: TForm1
Left = 192
Top = 114
Width = 814
Height = 585
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object Shape1: TShape
Left = 536
Top = 144
Width = 65
Height = 65
OnMouseDown = Shape1MouseDown
OnMouseMove = Shape1MouseMove
end
object Image1: TImage
Left = 144
Top = 72
Width = 321
Height = 257
end
object Button1: TButton
Left = 192
Top = 456
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
end