Delphi - 拖放 + MouseDown + MouseUp

Delphi - Drag and Drop + MouseDown + MouseUp

我正在开发一个拖放应用程序,当拖放项目时默认 DragCursor 感到困扰,如下面的默认 DragCursors 列表:

所以我正在尝试开发一种新的方式让用户看到像 GMAIL 一样的拖放移动:

我的问题是: Delphi 7 中是否可以同时使用拖放事件和鼠标事件?

如果我将 dmAutomatic 放入 DragModeMouseDown 事件将不起作用,如果我将 dmManual 放入 DragMode,则 MouseDown工作正常,但 DragDrop 事件不起作用。

下面是我的代码:

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;

对不起,我的问题很简单,但我不知道该怎么做...

非常感谢!

拖放是一种模态操作。当按钮按下时,它必然会随着鼠标事件潜逃,以便为拖动操作提供服务。

在 cmAutomatic 中,您告诉组件在按下左键时自动启动拖放操作。在 dmManual 中,您负责通过从 MouseDown 事件中调用 BeginDrag 来启动拖动操作。

IOW,没有抓取实际的 Windows 鼠标事件(WM_LBUTTONDOWN、WM_MOUSEMOVE、WM_LBUTTONUP 等),VCL 拖放机制将掩盖更高级别的鼠标事件。但是,如果您决定直接处理这些消息,您也会破坏拖放机制。如果不仔细管理事件和拖放子系统,您很容易使事情表现得很糟糕。

您可以使用 dmAutomatic 并为 OnStartDrag 事件编写处理程序,而不是您尝试使用的鼠标事件。

来自 D7 文档:

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.