delphi 精要

type
TDropFilesEvent = procedure(Receiver: TWinControl;
const FileNames: TStrings;const FilesCount: Integer;
const DropPoint: TPoint) of object;
TlxpDraggingFilesMonitor = class(TComponent)
{由于组件在运行时不需要可见,所以从TComponent派生}
private
FAcceptFilesControl: TWinControl; {指定接收者}
OldWindowProc: TWndMethod; {保存FAcceptFilesControl原来的窗口过程}
FFilesName: TStrings; {拖放的文件名列表}
FOnDropFiles: TDropFilesEvent; {拖放事件}
procedure SetAcceptFilesControl(const Value: TWinControl);
protected
{FAcceptFilesControl新的窗口过程}
procedure NewWindowProc(var Message: TMessage);
procedure Notification(
AComponent: TComponent; Operation: TOperation); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property AcceptFilesControl: TWinControl read FAcceptFilesControl
write SetAcceptFilesControl;
property OnDropFiles: TDropFilesEvent read FOnDropFiles write
FOnDropFiles;
end;

constructor TlxpDraggingFilesMonitor.Create(AOwner: TComponent);
begin
inherited;
FFilesName := TStringList.Create;
end;
destructor TlxpDraggingFilesMonitor.Destroy;
begin
if Assigned(FAcceptFilesControl) then
DragAcceptFiles(FAcceptFilesControl.Handle, False);
{注销文件拖放监视器}
FreeAndNil(FFilesName);
inherited;
end;
procedure TlxpDraggingFilesMonitor.SetAcceptFilesControl(
const Value: TWinControl);
begin
if FAcceptFilesControl <> Value then
begin
if Assigned(Value) and (not (csDesigning in ComponentState)) then
{设计时不执行下列代码}
begin
DragAcceptFiles(Value.Handle, True); {注册文件拖放监视器}
OldWindowProc := Value.WindowProc; {首先保存过程WindowProc 的指针}
Value.WindowProc := NewWindowProc; {给WindowProc 赋予新过程的地址}
end;
FAcceptFilesControl := Value;
FAcceptFilesControl.FreeNotification(Self);
{注册AcceptFilesControl,确保它被销毁时通知这个组件}
end;
end;
{在这个过程里处理文件拖放消息}
procedure TlxpDraggingFilesMonitor.NewWindowProc(var Message: TMessage);
var
Count, Index, hDrop: Integer;
PFileName: PChar;
P: TPoint;
begin
if Message.Msg = WM_DROPFILES then {如果是文件拖放消息}
begin
hDrop := Message.WParam;
FFilesName.Clear;
GetMem(PFileName, MAX_PATH); {给缓冲区PFileName 分配空间}
Count := DragQueryFile(hDrop, MAXDWORD, PFileName, MAX_PATH-1);
{取得文件总个数}
for Index := 0 to Count-1 do
begin
DragQueryFile(hDrop, Index, PFileName, MAXBYTE);
{得到每个文件的名字}
FFilesName.Add(PFileName);
end;
DragQueryPoint(hDrop, P); {取得鼠标松开时的位置,保存到P}
if Assigned(FOnDropFiles) then {如果用户书写了事件代码,则触发事件}
FOnDropFiles(FAcceptFilesControl, FFilesName, Count, P);
FreeMem(PFileName); {释放缓冲区空间}
DragFinish(hDrop); {完成本次拖放}
end else
OldWindowProc(Message); {调用原来的WindowProc 过程处理别的消息}
end;
procedure TlxpDraggingFilesMonitor.Notification(
AComponent:TComponent; Operation: TOperation);
begin
inherited;
if (AComponent = FAcceptFilesControl) and (Operation = opRemove)
then
FAcceptFilesControl := nil;
end;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值