Mega Code Archive

 
Categories / Delphi / Forms
 

How to make move your forms like WinAMP

Title: How to make move your forms like WinAMP. Question: How to move a window like WinAMP does, with a "magnetic field" near the borders. Answer: First: you need to declare 3 variables in your form: FMoving : Boolean; FOldX : Integer; FOldY : Integer; Then, you can put this code in your form: Procedure TfrmSplash.frmSplashMouseDown( Sender : TObject; Button : TMouseButton; Shift : TShiftState; X, Y : Integer ); Begin FMoving := True; FOldX := X; FOldY := Y; End; Procedure TfrmSplash.frmSplashMouseUp( Sender : TObject; Button : TMouseButton; Shift : TShiftState; X, Y : Integer ); Begin FMoving := False; End; Procedure TfrmSplash.frmSplashMouseMove( Sender : TObject; Shift : TShiftState; X, Y : Integer ); Const BorderSize = 10; // Size of the "magnetic field". Var WorkArea : TRect; Begin If ( SystemParametersInfo( SPI_GETWORKAREA, 0, @WorkArea, 0 ) ) Then Begin // Soluzione con l'area di lavoro. If ( FMoving ) Then Begin If ( ( ( frmSplash.Left - ( FOldX - X ) ) ( WorkArea.Left + BorderSize ) ) And ( ( frmSplash.Left - ( FOldX - X ) + frmSplash.Width ) frmSplash.Left := frmSplash.Left - ( FOldX - X ) Else If ( ( frmSplash.Left - ( FOldX - X ) ) frmSplash.Left := WorkArea.Left Else frmSplash.Left := WorkArea.Right - frmSplash.Width; If ( ( ( frmSplash.Top - ( FOldY - Y ) ) ( WorkArea.Top + BorderSize ) ) And ( ( frmSplash.Top - ( FOldY - Y ) + frmSplash.Height ) frmSplash.Top := frmSplash.Top - ( FOldY - Y ) Else If ( ( frmSplash.Top - ( FOldY - Y ) ) frmSplash.Top := WorkArea.Top Else frmSplash.Top := WorkArea.Bottom - frmSplash.Height; End; End Else Begin // Soluzione con il solo screen. If ( FMoving ) Then Begin If ( ( ( frmSplash.Left - ( FOldX - X ) ) BorderSize ) And ( ( frmSplash.Left - ( FOldX - X ) + frmSplash.Width ) frmSplash.Left := frmSplash.Left - ( FOldX - X ) Else If ( ( frmSplash.Left - ( FOldX - X ) ) frmSplash.Left := 0 Else frmSplash.Left := Screen.Width - frmSplash.Width; If ( ( ( frmSplash.Top - ( FOldY - Y ) ) BorderSize ) And ( ( frmSplash.Top - ( FOldY - Y ) + frmSplash.Height ) frmSplash.Top := frmSplash.Top - ( FOldY - Y ) Else If ( ( frmSplash.Top - ( FOldY - Y ) ) frmSplash.Top := 0 Else frmSplash.Top := Screen.Height - frmSplash.Height; End; End; End; Run your program, hold down mouse on your splash form and move it around.