请教:如何实现象资源管理器一样的Drag & Drop?(200分)

  • 主题发起人 主题发起人 Micro Whaight
  • 开始时间 开始时间
M

Micro Whaight

Unregistered / Unconfirmed
GUEST, unregistred user!
在两个资源管理器之间进行拖动时,接收拖入的资源管理器<br>会随着鼠标的移动而加亮显示对应的TreeNode,这样当Drop时,<br>会把拖放的文件Copy/Move到对应的目录中。该如何实现这种效<br>果?<br><br>Form的DragOver事件好像只能响应自己内部的DragOver动作,<br>如果是从资源管理器中拖入的文件,Delphi不理我?我只知道<br>响应WM_DropFiles消息和设置DragAcceptFiles()。Win的帮<br>助中找不到一个WM_Drag消息。
 
&nbsp;private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; procedure wmdropfiles(var message:tmessage);message wm_dropfiles;<br><br>procedure TFrmMain.wmdropfiles(var message:tmessage);<br>var<br>&nbsp; p:array[0..254] of char;<br>&nbsp; i:word;<br>&nbsp; Num:Word;<br>begin<br>&nbsp; inherited;<br>&nbsp; Num:=dragqueryfile(message.wparam,$ffffffff,nil,0);<br>&nbsp; &nbsp; &nbsp;for i:=0 to Num-1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dragqueryfile(message.wparam,i,p,255);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//p中存储了拖放过来的文件名<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp;end;<br>end;<br>
 
到我的主页:http://www.wapsec.com.cn/delphi/中找有关drop的控件.
 
不好意思,我可能没讲清楚。<br><br>我是想在鼠标还没放开的时候,窗口就可以收到消息并<br>对它进行处理。WM_DropFiles消息好象只能在放开鼠标<br>的时候,才能接收到?<br><br>www: 那个控件我也下过了,不过它好象没有我要的功能?<br>(我没有仔细看,只运行了它的for D3 的Demo),也只<br>能是在鼠标放开时才行?另外,这个控件用到了接口?我<br>对此已通六窍。 &nbsp;:(
 
唉,好吧, 结束。
 
好象无法实现,我也曾想做过,但没成功。
 
应该独立来考虑,判断拖入本程序中的object的属性、来源等,再决定是否应该有相应动作。
 
unit FileDropPan;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; ExtCtrls;<br><br>type<br>&nbsp; &nbsp; TDragFileEvent = procedure (Sender: TComponent; FileList: TStrings) of object;<br><br>&nbsp; TAIFileDropPan = class(TPanel)<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; FDragFileEvent: TDragFileEvent;<br>&nbsp; &nbsp; FAcceptFile: boolean;<br>&nbsp; &nbsp; procedure SetAcceptFile(const Value: boolean);<br>&nbsp; protected<br>&nbsp; &nbsp; { Protected declarations }<br>&nbsp; &nbsp; FileList: TStringList;<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; &nbsp; procedure WithDropFIles(var Msg: TMessage); message WM_DROPFILES;<br>&nbsp; &nbsp; constructor Create(AOwner: TComponent); override;<br>&nbsp; &nbsp; destructor Destroy; override;<br>&nbsp; published<br>&nbsp; &nbsp; { Published declarations }<br>&nbsp; &nbsp; property OnDragFile :TDragFileEvent read FDragFileEvent write FDragFileEvent;<br>&nbsp; &nbsp; property AcceptFile: boolean read FAcceptFile write SetAcceptFile;<br>&nbsp; end;<br><br>procedure Register;<br><br>implementation<br>Uses ShellAPI;<br><br>procedure Register;<br>begin<br>&nbsp; RegisterComponents('Voice', [TAIFileDropPan]);<br>end;<br><br>constructor TAIFileDropPan.Create(AOwner: TComponent);<br>begin<br>&nbsp; &nbsp; &nbsp;Inherited Create(AOwner);<br>&nbsp; &nbsp; &nbsp;parent := AOwner As TWinControl;<br>&nbsp; &nbsp; &nbsp;AcceptFile := true;<br>&nbsp; &nbsp; &nbsp;FileList := TStringList.Create;<br>end;<br><br>destructor TAIFileDropPan.Destroy;<br>begin<br>&nbsp; &nbsp; &nbsp;if parent &lt;&gt; nil then<br>&nbsp; &nbsp; &nbsp; &nbsp; if AcceptFile then AcceptFile := false;<br><br>&nbsp; &nbsp; &nbsp;FileList.Free;<br>&nbsp; &nbsp; &nbsp;inherited;<br>end;<br><br>procedure TAIFileDropPan.SetAcceptFile(const Value: boolean);<br>begin<br>&nbsp; &nbsp; &nbsp;FAcceptFile := Value;<br>&nbsp; &nbsp; &nbsp;DragAcceptFiles(Handle, FAcceptFile);<br>end;<br><br>procedure TAIFileDropPan.WithDropFIles(var Msg: TMessage);<br>Var<br>&nbsp; &nbsp;Buffer: Array[0..255] of Char;<br>&nbsp; &nbsp;count: Integer;<br>&nbsp; &nbsp;intX: Integer;<br>begin<br>&nbsp; &nbsp; &nbsp;count := DragQueryFile( Msg.WParam, $FFFFFFFF, Buffer, 255);<br>&nbsp; &nbsp; &nbsp;FileList.Clear;<br>&nbsp; &nbsp; &nbsp;for intX := 0 to count-1 do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DragQueryFile( Msg.WParam, intX, Buffer, 255);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileList.Add( Buffer);<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;DragFinish( Msg.WParam);<br>&nbsp; &nbsp; &nbsp;if Assigned(FDragFileEvent) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FDragFileEvent(Self, FileList);<br>end;<br><br>end.<br>我写的控件,从Panel继承过来的。能够接收拖放的文件,上面放置的其他控件也能接收文件。
 
IDropTarget中就有你关心的方法:<br>IDropTarget::DragEnter<br>IDropTarget::DragOver<br>IDropTarget::DragLeave<br>IDropTarget::Drop
 
最不想发生的事情终于发生了,还是要用到COM接口…… &nbsp;:)<br><br><br>谢谢几位的帮助。要对www说声对不起,上次你要我下的控件,第一次<br>没仔细用,后来看了几位的说明,再仔细看,可以达到我的要求。不过,<br>看了半天,决定还是用这个控件,不自己写了。写不出那个水平<br>:( &nbsp; &nbsp; 谢谢devuser提供的代码。这几天放假,我好好学习一下。<br><br>
 
后退
顶部