Title: Moving a Form/Component with mouse
Question: How to move a form or component with the mouse?
Answer:
This is for moving forms that has Borderstyle set to none or if you want to move components on the form.
First we need to create the following Global Variables
bMove : Boolean
iX, iY : Integer
Next, we must determine when the mouse goes down on the component we want to move and we need to set the mouse's origin point(x, y)
Go to the above mentioned components OnMouseDown event.
code :
bMove := True ;
iX := x ;
iY := y ;
The x and y variables are pre-created variables for the x and y points on the component witch was just clicked.
Next is the moving part, now depending on what you want to move, you will need to make some adjustments here.
Go the the selected component's OnMouseMove event.
code :
if bMove = true then
Begin
form1.left := form1.left + x - iX ;
form1.top := form1.top + y - iY ;
end
The above is used if you want to move the form. If you want to move a component then just replace the 'form1' with your component.
Finally we want the moving to stop when the mouse click is released.
Go to the selected components OnMouseUp event.
code :
bMove := false ;
It is as easy as that. Here is the code for the above mentioned.
//----------------
procedure TfrmMain.imgBorderMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
bMove := true ;
iX := x ;
iY := y ;
end;
procedure TfrmMain.imgBorderMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
begin
if bMove = true then
Begin
frmMain.Top := frmMain.Top + y - iY ;
frmMain.Left := frmMain.Left + x - iX ;
end;
end;
procedure TfrmMain.imgBorderMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
bMove := false ;
end;
Zander de Vos