且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

德尔福 - 拖动 &使用 ListView 拖放

更新时间:2023-11-13 11:42:52

您已经为 ListView 调用了 DragAcceptFiles,因此 Windows 将 WM_DROPFILES 发送到您的 ListView 而不是您的 Form.您必须从 ListView 中捕获 WM_DROPFILES 消息.

You've called DragAcceptFiles for the ListView, so Windows sends the WM_DROPFILES to your ListView and not to your Form. You have to catch the WM_DROPFILES message from the ListView.

  private
    FOrgListViewWndProc: TWndMethod;
    procedure ListViewWndProc(var Msg: TMessage);
  // ...
  end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Redirect the ListView's WindowProc to ListViewWndProc
  FOrgListViewWndProc := ListView1.WindowProc;
  ListView1.WindowProc := ListViewWndProc;

  DragAcceptFiles(ListView1.Handle, True);
end;

procedure TForm1.ListViewWndProc(var Msg: TMessage);
begin
  // Catch the WM_DROPFILES message, and call the original ListView WindowProc 
  // for all other messages.
  case Msg.Msg of
    WM_DROPFILES:
      DropFiles(Msg);
  else
    if Assigned(FOrgListViewWndProc) then
      FOrgListViewWndProc(Msg);
  end;
end;