2
291118
Unregistered / Unconfirmed
GUEST, unregistred user!
再加100分 我现在实现了能拖放文件到listview,并且当拖放完成时,能获取当前鼠标下的item,可问题是当鼠标拖拽着文件浮动于listview上时,我希望它能热修追踪!! ( 积分: 100 )<br />unit Unit1;
interface
uses
Windows, Messages,shellapi, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
ListView1: TListView;
procedure FormCreate(Sender: TObject);
private
procedure ApponMessage(VAR Msg:TMsg;VAR Handled:Boolean);
// procedure WMDropFiles(VAR Msg: TWMDropFiles);message WM_DROPFILES;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ApponMessage(var Msg: TMsg; var Handled: Boolean);
var
nFiles, I: Integer;
Filename: string;
ListItem: TListItem;
begin
//
// 注意!所有消息都将通过这里!
// 不要在此过程中编写过多的或者需要长时间操作的代码,否则将影响程序的性能
//
// 判断是否是发送到ListView1的WM_DROPFILES消息
if (Msg.message = WM_DROPFILES) and (msg.hwnd = ListView1.Handle) then
begin
// 取dropped files的数量
nFiles := DragQueryFile (Msg.wParam, $FFFFFFFF, nil, 0);
// 循环取每个拖下文件的全文件名
try
for I := 0 to nFiles - 1 do
begin
// 为文件名分配缓冲 allocate memory
SetLength (Filename, 80);
// 取文件名 read the file name
DragQueryFile (Msg.wParam, I, PChar (Filename), 80);
Filename := PChar (Filename);
//file://将全文件名分解程文件名和路径
ListItem := ListView1.Items.Add;
ListItem.Caption := ExtractFileName(FileName);
ListItem.SubItems.Add(ExtractFilePath(FileName));
end;
finally
//file://结束这次拖放操作
DragFinish (Msg.wParam);
end;
//file://标识已处理了这条消息
Handled := True;
end;
//if (msg.message =wm_minimize) then showmessage('min');;
end;
{
procedure TForm1.WMDropFiles(VAR Msg: TWMDropFiles);
VAR
N : Word;
buffer : ARRAY[0..180] OF Char;
BEGIN
WITH Msg DO
BEGIN
FOR N := 0 TO DragQueryFile(Drop, $FFFFFFFF, buffer,1)-1 DO
BEGIN
DragQueryFile(Drop, N, Buffer, 80);
ListBox1.Items.Add(StrPas(Buffer));
END;
DragFinish(Drop);
END;
end;
}
procedure TForm1.FormCreate(Sender: TObject);
begin
DragAcceptFiles(Handle, True);
DragAcceptFiles(ListView1.Handle, True);
Application.OnMessage := AppOnMessage;
end;
end.
interface
uses
Windows, Messages,shellapi, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
ListView1: TListView;
procedure FormCreate(Sender: TObject);
private
procedure ApponMessage(VAR Msg:TMsg;VAR Handled:Boolean);
// procedure WMDropFiles(VAR Msg: TWMDropFiles);message WM_DROPFILES;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ApponMessage(var Msg: TMsg; var Handled: Boolean);
var
nFiles, I: Integer;
Filename: string;
ListItem: TListItem;
begin
//
// 注意!所有消息都将通过这里!
// 不要在此过程中编写过多的或者需要长时间操作的代码,否则将影响程序的性能
//
// 判断是否是发送到ListView1的WM_DROPFILES消息
if (Msg.message = WM_DROPFILES) and (msg.hwnd = ListView1.Handle) then
begin
// 取dropped files的数量
nFiles := DragQueryFile (Msg.wParam, $FFFFFFFF, nil, 0);
// 循环取每个拖下文件的全文件名
try
for I := 0 to nFiles - 1 do
begin
// 为文件名分配缓冲 allocate memory
SetLength (Filename, 80);
// 取文件名 read the file name
DragQueryFile (Msg.wParam, I, PChar (Filename), 80);
Filename := PChar (Filename);
//file://将全文件名分解程文件名和路径
ListItem := ListView1.Items.Add;
ListItem.Caption := ExtractFileName(FileName);
ListItem.SubItems.Add(ExtractFilePath(FileName));
end;
finally
//file://结束这次拖放操作
DragFinish (Msg.wParam);
end;
//file://标识已处理了这条消息
Handled := True;
end;
//if (msg.message =wm_minimize) then showmessage('min');;
end;
{
procedure TForm1.WMDropFiles(VAR Msg: TWMDropFiles);
VAR
N : Word;
buffer : ARRAY[0..180] OF Char;
BEGIN
WITH Msg DO
BEGIN
FOR N := 0 TO DragQueryFile(Drop, $FFFFFFFF, buffer,1)-1 DO
BEGIN
DragQueryFile(Drop, N, Buffer, 80);
ListBox1.Items.Add(StrPas(Buffer));
END;
DragFinish(Drop);
END;
end;
}
procedure TForm1.FormCreate(Sender: TObject);
begin
DragAcceptFiles(Handle, True);
DragAcceptFiles(ListView1.Handle, True);
Application.OnMessage := AppOnMessage;
end;
end.