85 lines
1.9 KiB
ObjectPascal
85 lines
1.9 KiB
ObjectPascal
unit MovePanel;
|
||
|
||
interface
|
||
uses
|
||
Windows, Classes, Controls,ExtCtrls;
|
||
type
|
||
TMovePanel = class(TPanel) //这个控件是继承Tpanel类的
|
||
private
|
||
PrePoint:TPoint;
|
||
Down:Boolean;
|
||
{ Private declarations }
|
||
protected
|
||
{ Protected declarations }
|
||
public
|
||
constructor Create(AOwner:TComponent);
|
||
override;
|
||
//重载鼠标事件,抢先处理消息
|
||
procedure MouseDown(Button: TMouseButton;
|
||
Shift: TShiftState; X, Y: Integer);override;
|
||
procedure MouseUp(Button: TMouseButton;
|
||
Shift: TShiftState; X, Y: Integer);override;
|
||
procedure MouseMove(Shift: TShiftState;
|
||
X, Y: Integer);override;
|
||
{ Public declarations }
|
||
published
|
||
{ Published declarations }
|
||
end;
|
||
|
||
procedure Register;
|
||
|
||
implementation
|
||
|
||
constructor TMovePanel.Create(AOwner:TComponent);
|
||
begin
|
||
inherited Create(AOwner); //继承父类的Create方法
|
||
end;
|
||
|
||
procedure TMovePanel.MouseDown(Button:
|
||
TMouseButton; Shift: TShiftState; X, Y: Integer);
|
||
begin
|
||
if (Button=MBLeft) then
|
||
begin
|
||
Down:=true;
|
||
GetCursorPos(PrePoint);
|
||
end;
|
||
//如果方法已存在,就触发相应事件去调用它,若不加此语句会造成访存异常
|
||
if assigned(OnMouseDown) then
|
||
OnMouseDown(self,Button,shift,x,y);
|
||
end;
|
||
|
||
procedure TMovePanel.MouseUp(Button:
|
||
TMouseButton; Shift: TShiftState; X, Y: Integer);
|
||
begin
|
||
if (Button=MBLeft) and Down then
|
||
Down:=False;
|
||
if assigned(OnMouseUp) then
|
||
OnMouseUp(Self,Button,shift,X,y);
|
||
end;
|
||
|
||
procedure TMovePanel.MouseMove(Shift:
|
||
TShiftState; X, Y: Integer);
|
||
Var
|
||
NowPoint:TPoint;
|
||
begin
|
||
if down then
|
||
begin
|
||
GetCursorPos(nowPoint);
|
||
//self.Parent在Form中就是MovePanel所在的窗体,或是MovePanel所在的容器像Panel
|
||
self.Parent.Left:=self.Parent.left
|
||
+NowPoint.x-PrePoint.x;
|
||
self.parent.Top:=self.Parent.Top
|
||
+NowPoint.y-PrePoint.y;
|
||
PrePoint:=NowPoint;
|
||
end;
|
||
if Assigned(OnMouseMove) then
|
||
OnMouseMove(self,Shift,X,y);
|
||
end;
|
||
|
||
procedure Register;
|
||
begin
|
||
RegisterComponents('Data Controls', [TMovePanel]);
|
||
end;
|
||
|
||
end.
|