-添加 FileDrop 属性到 可视化控件(visual control) 中-

作者:Damir Kojevod

添加FileDrop属性到可视化控件[visual control] 中


1.首先从可视化控件(visual control )中继承一个新的控件。
2.增加接受文件拖拽消息的属性。
3.增加消息被处触发时要响应处理的事件。
4.使用测试这个新创建的控件。


详细说明:
1。在delphi中 Component|Newcomponent 选择一个祖先。这里使用TEdit,给这个控件取名 TFileDropEdit,可设置其他参数,并选择确定或安装(这种情况表示自动安装控件到 控件模板)。
2。增加接收文件拖拽的属性。
在private部分 增加一个消息过程,声明如下:
procedure WMDROPFILES(var Message: TWMDROPFILES); message WM_DROPFILES;
这个消息过程完成从Windows 接收WM_DROPFILES消息并且能进行'初加工'。这个过程完成一系列通常的处理,接收者从消息中提取消息参数等。同时我们还允许用户定义她自己的动作(把提取的信息交给用户处理),所以我们必须定义一个事件指针来完成这个功能。如果用户定义了处理过程,消息处理过程会调用用户定义的过程。另外,控件必须向windows声明自己是能接收文件拖拽的控件。是否注册要调用windows函数 DragAcceptFiles.参数中需要控件的句柄,所以不能在控件的构造器中完成注册,这里在public部分增加属性。这里用AcceptFiles属性来注册和注销文件拖拽功能。

3。定义消息触发事件(提供事件指针)以便用户定义处理过程事件。
先定义一个过程事件:参数为TStringList。如下:
TFileDropEvent = procedure(File: TStringList) of object;
再在published 部分定义属性 如下
property OnFileDrop: TFileDropEvent Read FFileDrop write FFileDrop;
同时在private部分定义过程指针变量:
FFileDrop: TFileDropEvent;

 

下面的源代码是 TDropEdit控制的全部代码。注意:开始控件是不接受文件拖拽的,如果用户设置AcceptFile 属性为真则可接受文件拖拽。

unit FileDropEdit;

interface

uses
Windows,
Messages,
SysUtils,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
StdCtrls,
ShellApi;

type
TFileDropEvent = procedure(files: tstringlist) of object;

TFileDropEdit = class(TEdit)
private
{ Private declarations }
FFileDrop: TFileDropEvent;
FAcceptFiles: Boolean;
procedure WMDROPFILES(var Message: TWMDROPFILES); message WM_DROPFILES;
procedure SetAcceptFiles(const Value: Boolean);
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;

property AcceptFiles: Boolean read FAcceptFiles write SetAcceptFiles;
published
{ Published declarations }
property OnFileDrop: TFileDropEvent read FFileDrop write FFileDrop;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TFileDropEdit]);
end;

constructor TFileDropEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner: TComponent);
FFileDrop := nil;
FAcceptFiles := False;
end;

procedure TFileDropEdit.WMDROPFILES(var Message: TWMDROPFILES);
var
NumFiles: integer;
buffer: array[0..255] of char;
i: integer;
l: TStringList;
begin
if Assigned(FFiledrop) then
begin
l := TStringList.Create;
NumFiles := DragQueryFile(Message.Drop, $FFFFFFFF, nil, 0); {thanks to Mike Heydon for D5 adjustment of parameters}
for i := 0 to NumFiles - 1 do {Accept the dropped file}
begin
DragQueryFile(Message.Drop, i, buffer, sizeof(buffer));
l.append(StrPas(buffer))
end;
FFileDrop(l);
l.free
end
end;

procedure TFileDropEdit.SetAcceptFiles(const Value: Boolean);
begin
if FAcceptFiles <> Value then
begin
FAcceptFiles := Value;
DragAcceptFiles(Handle, Value);
end
end;

end.


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值