-添加 FileDrop 属性到 可视化控件(visual control)
2008-04-30 19:28
295 查看
作者: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.
添加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.
相关文章推荐
- -添加 FileDrop 属性到 可视化控件(visual control) 中-
- 添加FileDrop属性到可视化控件
- 为服务器控件添加javascript客户端事件(WebControl的Attributes属性)
- WebControl开发点滴3:给所有控件添加属性
- js(JQuery) 设置servercontrol(服务器控件)的text属性
- silverlight中为控件添加可以在标签中设置的属性
- Asp.Net中FileUpload控件属性PostedFile.ContentType
- 为List Control控件添加可编辑功能
- MFC List Control控件添加单元格编辑和单元格下拉列表项以适用于数据库相关操作
- 【File Field控件】上传图片的同时直接显示图片的属性设置方法
- MFC List Control 控件添加单元格编辑和单元格下拉列表项
- ASP.NET2.0自定义控件开发(4) ----给控件添加树状属性
- DEV GridControl 控件属性大全
- c#.net]做tooltip给控件动态添加属性的做法、IExtenderProvider接口的使用
- .NET中封装控件时添加属性组,类似于Size(Width,Height)
- 富文本(TYAttributedLabel 简单,强大的属性文本控件(无需了解CoreText),支持图文混排显示,支持添加链接,image和UIView控件,支持自定义排版显示)
- DEV控件:gridControl常用属性设置
- 为服务器控件添加属性
- C#如何创建自定义控件以及添加自定义属性和事件使用
- 频播放器,支持基本的拖动,声音、亮度调节,支持边播边缓存,支持视频本身自带 rotation 属性的旋转,重力旋转与手动旋转的同步支持,支持列表播放 ,直接添加控件为封,兼容一些 5.0 的过场效果