Delphi - 拖拽用 ListView 拖放
晚上好 :-)!
我有这段代码可以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已经为 ListView 调用了 DragAcceptFiles,因此 Windows 将 WM_DROPFILES 发送到您的 ListView 而不是您的窗体。您必须从 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.
您的问题是,您将列表视图窗口注册为放置目标,但在表单类中处理
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.