自制支持文件拖放的VCL组件[ 作者: 不详 添加时间: 2001-7-13 11:46:46 ]
用过Winamp的朋友都知道,Winamp的界面能支持文件拖放,当你想欣赏某MP3文件时,只需要
将文件拖到Winamp的窗口上,然后放开鼠标就行了。那我们如何让自己的程序也实现这样的功能
呢?我们可以通过改进开发工具提供的标准组件来实现。下面以Delphi环境中的ListBox组件为
例,让ListBox支持文件拖放。
首先介绍一下要用到的API函数:
DragAcceptFiles() 初始化某窗口使其允许/禁止接受文件拖放
DragQueryFile() 查询拖放的文件名
DragFinish() 释放拖放文件时使用的资源
实现的基本原理如下:首先调用DragAcceptFiles()函数初始化组件窗口,使其允许接受文件
拖放,然后等待WM_DropFiles消息(一旦用户进行了拖放文件操作,组件窗口即可获得此消息),
获得消息后即可使用DragQueryFile()函数查询被拖放的文件名,最后调用DragFinish()释放资
源。 因为在VCL类库中,ListBox组件,所属类名为:TListBox,所以我们可以从TListBox继承建立
自己的组件。新组件名为:TDropFileListBox,它比标准TListBox增加了一个OnDropFiles事件和
一个DropEnabled属性。当DropEnabled为True时即可接受文件拖放,文件拖放完成后激发
OnDropFiles事件,该事件提供一个FileNames参数让用户获得文件名。 组件的代码如下: { TDropFileListBox V1.00 Component }
{ Copyright (c) 2000.5 by Shen Min, Sunisoft }
{ Email: [email protected] }
{ 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.
用过Winamp的朋友都知道,Winamp的界面能支持文件拖放,当你想欣赏某MP3文件时,只需要
将文件拖到Winamp的窗口上,然后放开鼠标就行了。那我们如何让自己的程序也实现这样的功能
呢?我们可以通过改进开发工具提供的标准组件来实现。下面以Delphi环境中的ListBox组件为
例,让ListBox支持文件拖放。
首先介绍一下要用到的API函数:
DragAcceptFiles() 初始化某窗口使其允许/禁止接受文件拖放
DragQueryFile() 查询拖放的文件名
DragFinish() 释放拖放文件时使用的资源
实现的基本原理如下:首先调用DragAcceptFiles()函数初始化组件窗口,使其允许接受文件
拖放,然后等待WM_DropFiles消息(一旦用户进行了拖放文件操作,组件窗口即可获得此消息),
获得消息后即可使用DragQueryFile()函数查询被拖放的文件名,最后调用DragFinish()释放资
源。 因为在VCL类库中,ListBox组件,所属类名为:TListBox,所以我们可以从TListBox继承建立
自己的组件。新组件名为:TDropFileListBox,它比标准TListBox增加了一个OnDropFiles事件和
一个DropEnabled属性。当DropEnabled为True时即可接受文件拖放,文件拖放完成后激发
OnDropFiles事件,该事件提供一个FileNames参数让用户获得文件名。 组件的代码如下: { TDropFileListBox V1.00 Component }
{ Copyright (c) 2000.5 by Shen Min, Sunisoft }
{ Email: [email protected] }
{ 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.
解决方案 »
- 在Delphi7中用什么组件做报表?怎么介绍Delphi5的书上介绍的TQuickRep找不到了?
- 这么简单的SQL语句没人会么??????????
- 为什么priter打印图形到tif文件计算机要自动重启 ?
- 第一个delphi程序
- shape控件的图标上有一个三角形,意思肯定是shape支持三角形了。为甚末我找不到呢?
- 我现在在用神V龙开发一套系统,上面SDK怎么在dephi中用?
- NULL值的问题!
- 怎样确定 一个文件是否存在?
- 如何将字符串转换成对象名?
- 关于dbgrid的问题
- 关于共用ADOCONNECTION,ADOQUERY,DATASOURCE连接数据库的问题,高手请进。
- 非常菜的问题请各位指教
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure WMDropFiles(var Message: TMessage); message WM_DropFiles;
end;var
Form1: TForm1;implementation{$R *.dfm}uses
ShellApi;procedure TForm1.WMDropFiles(var Message: TMessage);
var
Index, iFileCount: Integer;
sFileName: array[0..MAX_PATH] of Char;
begin
Index := -1;
iFileCount := DragQueryFile(Message.WParam, Index, nil, 0); for Index := 0 to iFileCount - 1 do
begin
DragQueryFile(Message.WParam, Index, sFileName, MAX_PATH);
ListBox1.Items.Add(sFileName);
end;
DragFinish(Message.WParam);
end;procedure TForm1.FormCreate(Sender: TObject);
begin
DragAcceptFiles(Handle, True);
end;end.
同意 taxi(游少爷)的方法,简单有效