Delphi - 拖拽用 ListView 拖放

发布于 2024-10-26 04:39:35 字数 1079 浏览 3 评论 0原文

晚上好 :-)!

我有这段代码可以使用 Drag & 文件的Drop方法:

TForm1 = class(TForm)
...
public
    procedure DropFiles(var msg: TMessage ); message WM_DROPFILES;
end;

procedure TForm1.FormCreate(Sender: TObject)
begin
    DragAcceptFiles(ListView1.Handle, True);
end;

procedure TForm1.DropFiles(var msg: TMessage );
var
  i, count  : integer;
  dropFileName : array [0..511] of Char;
  MAXFILENAME: integer;
begin
  MAXFILENAME := 511;
  count := DragQueryFile(msg.WParam, $FFFFFFFF, dropFileName, MAXFILENAME);
  for i := 0 to count - 1 do
  begin
    DragQueryFile(msg.WParam, i, dropFileName, MAXFILENAME);
    Memo1.Lines.Add(dropFileName);
  end;
  DragFinish(msg.WParam);
end;

ListView区域是DragCursor,但在Memo1中不是任何记录。 当我使用例如 ListBox 和方法 DragAcceptFiles(ListBox1.Handle, True) 时,一切都很好。

ListView 属性DragMode 我设置为dmAutomatic

谢谢 :-)

Good evening :-)!

I have this code to use Drag & Drop method for files:

TForm1 = class(TForm)
...
public
    procedure DropFiles(var msg: TMessage ); message WM_DROPFILES;
end;

procedure TForm1.FormCreate(Sender: TObject)
begin
    DragAcceptFiles(ListView1.Handle, True);
end;

procedure TForm1.DropFiles(var msg: TMessage );
var
  i, count  : integer;
  dropFileName : array [0..511] of Char;
  MAXFILENAME: integer;
begin
  MAXFILENAME := 511;
  count := DragQueryFile(msg.WParam, $FFFFFFFF, dropFileName, MAXFILENAME);
  for i := 0 to count - 1 do
  begin
    DragQueryFile(msg.WParam, i, dropFileName, MAXFILENAME);
    Memo1.Lines.Add(dropFileName);
  end;
  DragFinish(msg.WParam);
end;

In area of ListView is DragCursor, but in Memo1 aren't any records.
When I use for example ListBox and method DragAcceptFiles(ListBox1.Handle, True) ever is fine.

ListView property DragMode I set to dmAutomatic.

Thanks :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫性小仙女 2024-11-02 04:39:35

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

  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;

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;
空宴 2024-11-02 04:39:35

您的问题是,您将列表视图窗口注册为放置目标,但在表单类中处理 WM_DROPFILES 消息。该消息被发送到列表视图控件,您应该在那里处理该消息。

Your problem is, you're registering the list view window as a drop target, but handling the WM_DROPFILES message in the form class. The message is sent to the list view control, you should handle the message there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文