简体   繁体   English

Delphi - 使用ListView拖放

[英]Delphi - Drag & Drop with ListView

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. ListView区域是DragCursor ,但在Memo1 中没有任何记录。 When I use for example ListBox and method DragAcceptFiles(ListBox1.Handle, True) ever is fine. 当我使用例如ListBox和方法DragAcceptFiles(ListBox1.Handle,True)时一切都很好。

ListView property DragMode I set to dmAutomatic . ListView属性DragMode我设置为dmAutomatic

Thanks :-) 谢谢 :-)

You've called DragAcceptFiles for the ListView, so Windows sends the WM_DROPFILES to your ListView and not to your Form. 您已为ListView调用DragAcceptFiles,因此Windows将WM_DROPFILES发送到ListView而不是表单。 You have to catch the WM_DROPFILES message from the 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;

Your problem is, you're registering the list view window as a drop target, but handling the WM_DROPFILES message in the form class. 您的问题是,您正在将列表视图窗口注册为放置目标,但是在表单类中处理WM_DROPFILES消息。 The message is sent to the list view control, you should handle the message there. 消息被发送到列表视图控件,您应该在那里处理消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM