用winamp自制支持文件拖放的VCL组件
作者:佚名 出处:网络 更新时间:2007-05-24 责任编辑:
 
 

组件的代码如下:


  { TDropFileListBox V1.00 Component }
  { Copyright (c) 2000.5 by Shen Min, Sunisoft }
  { Email: sunisoft@21cn.com }
  { Web: http://www.sunistudio.com }
  unit DropFileListBox;
  interface
  uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ShellApi; //增加ShellApi单元,因为所使用的三个API函数声明于此单元文件中
  type
  TMyNotifyEvent = procedure (Sender: TObject;FileNames:TStringList) of object; //自定义事件类型。
  TDropFileListBox = class(TListBox) //新的类从TListBox继承
  private
  { Private declarations }
  FEnabled:Boolean; //属性DropEnabled的内部变量
  protected
  FDropFile:TMyNotifyEvent; //事件指针
  procedure DropFiles(var Mes:TMessage);message WM_DROPFILES;
  procedure FDropEnabled(Enabled:Boolean); //设置DropEnabled属性的过程
  { Protected declarations }
  public
  constructor Create(AOwner: TComponent);override;
  destructor Destroy;override;
  { Public declarations }
  published
  property OnDropFiles:TMyNotifyEvent read FDropFile write FDropFile;
  property DropEnabled:Boolean read FEnabled write FDropEnabled;
  { Published declarations }
  end;
  procedure Register; 
  implementation 
  procedure Register;
  begin
  RegisterComponents('Sunisoft', [TDropFileListBox]); //注册组件到组件板上
  end; 
  constructor TDropFileListBox.Create(AOwner: TComponent);
  begin
  inherited Create(AOwner);
  FEnabled:=true; //类被构造时,使DropEnabeld的默认值为True
  end; 
  destructor TDropFileListBox.Destroy;
  begin
  inherited Destroy;
  end; 
  //改变属性DropEnabled的调用过程
  procedure TDropFileListBox.FDropEnabled(Enabled:Boolean);
  begin
  FEnabled:=Enabled;
  DragAcceptFiles(Self.Handle,Enabled);//设置组件窗口是否接受文件拖放
  end; 
  //接受WM_DropFiles消息的过程
  procedure TDropFileListBox.DropFiles(var Mes:TMessage);
  var FN:TStringList;
  FileName:array [1..256] of char;
  sFN:String;
  i,Count,p:integer;
  begin
  FN:=TStringList.Create;
  Count:=DragQueryFile(Mes.WParam,$FFFFFFFF,@FileName,256);//得到拖放文件的个数
  For i:=0 to Count-1 do
  begin
  DragQueryFile(mes.WParam,i,@FileName,256);//查询文件名称
  sFN:=FileName;
  p:=pos(chr(0),sFN);//去掉文件名末尾的ASCII码为0的字符
  sFN:=copy(sFN,1,p-1);
  FN.Add(sFN);
  end;
  DragFinish(mes.WParam); //释放所使用的资源
  if Assigned(FDropFile) then
  FDropFile(self, FN); //调用事件,并返回文件名列表参数
  FN.Free;
  end; 
  end.

该组件安装后即可使用,我们可以新建一个工程,将该组件放置在Form上,然后在  TDropFileListBox组件的OnDropFiles事件中写下具体处理拖放操作的代码。

例如将所有拖放的文件名加入该列表中,添加代码如下:


  procedure TForm1.DropFileListBox1DropFiles(Sender: TObject;FileNames: TStringList);
  begin DropFileListBox1.Items.AddStrings(FileNames);
  end;

运行一下看看,是不是很有趣?本文仅仅对于ListBox组件做了扩展,你也可以对其它组件做类似的扩展,实现支持文件的拖放。

 
     
 
 
后退<<上一页 继续>>Winamp
[上一页][第1页][第2页][第3页][下一页]
 
内容导航  
第1页要用到的API函数 第2页组件的代码
第3页Winamp
上一篇 : FTP也疯狂 让迅雷资源探.. 下一篇制作迅雷版万能Windows映..
 
  您要为所发表的言论的后果负责,请各位遵纪守法并注意文明用语
标题:  

[ 关于本站 ] [ 广告服务 ] [ 商务合作 ] [ 联系我们 ] [ 合作伙伴 ] [ 法律顾问 ] [ 网站地图 ]
千橡公司 京ICP证041489号 Copyright (c) 1997-2007 All Right Reserved.