再加100分 我现在实现了能拖放文件到listview,并且当拖放完成时,能获取当前鼠标下的item,可问题是当鼠标拖拽着文件浮动于listview上时,我希望

  • 主题发起人 主题发起人 291118
  • 开始时间 开始时间
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.
 
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.
 
ListView1.DropTarget不就是你鼠标当前放置的位置了嘛
 
我跟踪了,当拖文件在程序窗口移动时,系统不会向窗口发送任何消息,当然也不会触发任何事件了,只有放开鼠标时才会发送消息,触发事件,但不知道WINDOWS的资源管理器怎么做的,它就能够在拖文件到某个节点附近时该节点自动会被选中,希望大家共同探讨!
 
那你就直接在onMouseMove事件里面写
 
老大,您还没明白,没有消息发生,就意味着不可能捕捉到事件啊!
 
Delphi一样也会自动选中啊
 
我不知道你是怎么实现拖拉文件的,但是我这样做的话从ListView1拖到ListView2是会自己选中的.
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ExtCtrls;

type
TForm1 = class(TForm)
ListView1: TListView;
ListView2: TListView;
procedure ListView2DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure ListView2DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
i : integer;

implementation

{$R *.dfm}

procedure TForm1.ListView2DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
ListView2.Items.Insert(ListView2.DropTarget.Index).Caption :=
(Source as TListView).Selected.Caption;
end;

procedure TForm1.ListView2DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
if Source = ListView1 then
Accept := true;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
i : integer;
begin
for i :=0 to 9 do
begin
ListView1.Items.Add.Caption := 'A'+IntToStr(i);
ListView2.Items.Add.Caption := 'B'+IntToStr(i);
end;
ListView1.DragMode := dmAutomatic;
end;

end.
 
我是从资源管理器中选一个文件往LISTVIEW中拖,你那样的是肯定没问题的,因为都是在同一程序中,并且不是真正的拖文件而是ITEM的拖拽。
 
后退
顶部