pedro.olimpio
pedro.olimpio

Reputation: 1498

Delphi - Drag and Drop + MouseDown + MouseUp

I'm developing a drag 'n drop application and i'm feeling troubled with the Default DragCursor when Drag and Dropping an item as the following list of the default DragCursors:

Default DragCursors

So i'm trying to develop a new way to the user see the Drag 'n Drop movement like GMAIL:

Gmail

My question is: Are there the possibility to use Drag 'n drop events together Mouse events in Delphi 7?

enter image description here enter image description here

If i put dmAutomatic in DragMode the MouseDown event does not work and if I put dmManual in DragMode the MouseDown works fine, but the DragDrop event does not work.

Here is my code below:

type
  TForm1 = class(TForm)
    pnlInformacaoDragDrop: TPanel;
    pnl1: TPanel;
    pnl2: TPanel;
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure pnl1DragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure pnl2DragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure pnl2DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure pnl1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure pnl1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure pnl1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  if Assigned(Self) then
  begin
    if pnlInformacaoDragDrop.Visible then
    begin
      pnlInformacaoDragDrop.Left :=X + 10;
      pnlInformacaoDragDrop.Top := Y + 10;
    end;
  end;
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Assigned(Self) then
  begin
    if not pnlInformacaoDragDrop.Visible then
      pnlInformacaoDragDrop.Visible := True;

//    img1.BeginDrag(True);
  end;
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Assigned(Self) then
  begin
    if pnlInformacaoDragDrop.Visible then
      pnlInformacaoDragDrop.Visible := False;
  end;
end;

procedure TForm1.pnl1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
  TPanel(Sender).Caption := TPanel(Sender).Caption + ' - ' + TPanel(Source).Caption;
end;

procedure TForm1.pnl2DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
  TPanel(Sender).Caption := TPanel(Sender).Caption + ' - ' + TPanel(Source).Caption;
end;

procedure TForm1.pnl2DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept := true;
end;

procedure TForm1.pnl1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept := true;
end;

Sorry for my simple question, but I don't know how can i do it...

Thanks a lot!

Upvotes: 1

Views: 2823

Answers (2)

Tom Brunberg
Tom Brunberg

Reputation: 21033

You can use dmAutomatic and write a handler for the OnStartDrag event instead of the mouse events you tried to use.

From D7 documentation:

Description

Use the OnStartDrag event handler to implement special processing when the user starts to drag the control or an object it contains. OnStartDrag only occurs if DragKind is dkDrag.

...

The OnStartDrag event handler can create a TDragControlObjectEx instance for the DragObject parameter to specify the drag cursor, or, optionally, a drag image list.

Upvotes: 2

Allen Bauer
Allen Bauer

Reputation: 16842

Drag-n-drop is a modal operation. It necessarily will abscond with the mouse events while the button is down in order to service the drag operation.

In cmAutomatic, you're telling the component to automatically initiate a drag-n-drop operation on left button down. In dmManual, you are responsible for initiating the drag operation by calling BeginDrag from within the MouseDown event.

IOW, without grabbing the actual Windows mouse events (WM_LBUTTONDOWN, WM_MOUSEMOVE, WM_LBUTTONUP, etc..), the VCL drag-n-drop mechanism will obscure the higher-level mouse events. However, should you decide to process those messages directly, you will also break the drag-n-drop mechanism. Without carefully managing the events and the drag-n-drop subsystem, you can easily make things behave very badly.

Upvotes: 1

Related Questions