晚上好:-)!
我有以下代码可以对文件使用拖放方法:
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/