Delphi - 使用 ListView 拖放

标签 delphi listview file drag-and-drop handle

晚上好:-)!

我有以下代码可以对文件使用拖放方法:

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

谢谢:-)

最佳答案

您已为 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;

关于Delphi - 使用 ListView 拖放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5397158/

相关文章:

delphi - TToolbar 与 TForm.DoubleBuffered 不兼容?

c# - 如何判断是否连接到互联网

sql - 如何从存储过程返回所有值?

C# listview - 嵌入控件

java - 将 TextView 更改为自定义 ListView 中的可点击单词

php - 在 PHP 中高效的平面文件搜索

delphi - 对“foo”的歧义重载调用。 D6有问题吗?错误范围?

Android listView适配器错误

file - 在 gnuplot 中读取外部数据文件

c - off_t without -D_FILE_OFFSET_BITS=64 on a file > 2GB