How to make a TPanel's appearance smoother when it is being dragged?

147 Views Asked by At

I want to make a TPanel draggable. I have done so, but the panel looks jerky when it is being dragged. How can I make it smoother?

Perhaps you don't have to look at my code to answer that, but here it is, just in case:

unit fMainForm;
interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls;

type
TtheMainForm = class(TForm)
  procedure FormCreate(Sender: TObject);
private
  FDragging: Boolean;
  FDragStartPos: TPoint;
  FDragPanel: TPanel; // the panel being dragged

  procedure AddLocation(const name : String);
  procedure PanelMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  procedure PanelMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  procedure PanelMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
public
  { Public declarations }
end;

var theMainForm: TtheMainForm;

implementation

{$R *.dfm}

procedure TtheMainForm.FormCreate(Sender: TObject);
begin
  AddLocation('New');
end;

procedure TtheMainForm.AddLocation(const name: String);
var newPanel : TPanel;
begin
  newPanel := TPanel.Create(theMainForm);
  newPanel.Parent := theMainForm;
  newPanel.Name := name;
  newPanel.Caption := name;
  newPanel.Color := clBtnFace; // Set to a specific color to cause opacity when fdragging   to look smoother
  newPanel.ParentBackground := False;  // helps to reduce flicker

  newPanel.OnMouseDown := PanelMouseDown;
  newPanel.OnMouseMove := PanelMouseMove;
  newPanel.OnMouseUp := PanelMouseUp;
end;   // AddLocation()

procedure TtheMainForm.PanelMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Sender is TPanel then
  begin
    FDragPanel := TPanel(Sender);
    FDragging := True;
    FDragStartPos := Point(X, Y);
  end;
end;

procedure TtheMainForm.PanelMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var DeltaX, DeltaY: Integer;
begin
  if FDragging and (FDragPanel <> nil) then
  begin
    DeltaX := X - FDragStartPos.X;
    DeltaY := Y - FDragStartPos.Y;

    FDragPanel.Left := FDragPanel.Left + DeltaX;
    FDragPanel.Top := FDragPanel.Top + DeltaY;

    FDragStartPos := Point(X, Y);
  end;
end;

procedure TtheMainForm.PanelMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FDragging := False;
  FDragPanel := nil; // Reset the dragged panel
end;

end.

Please note that I must create my panels dynamically at run-time. This code creates a single panel. Ctrl-click it to grab it, move the mouse/trackpad to drag the panel, and release to end the drag.

The appearance when dragging doesn't really seem acceptable to me. How can I make it smoother?

0

There are 0 best solutions below