unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
procedure Label1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Label1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Label1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
downX,downY,downLeft,downTop: Integer; //鼠标按下的位置以及鼠标按下时Label的位置
dragging: Boolean; //表示现在能不能移动Label
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Label1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
downX:=X;
downY:=Y;
downLeft:=Label1.Left;
downTop:=Label1.Top;
dragging := TRue; // //MouseDown的时候,允许移动
end;
procedure TForm1.Label1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if dragging then
begin
Label1.Left:=X-downX+Label1.Left;
Label1.Top:=Y-downY+Label1.Top;
end;
end;
procedure TForm1.Label1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if dragging then //MouseUp的时候,不允许移动
dragging := False;
end;
end.