delphi 精要

来源:互联网 发布:mac电源以连接,未充电 编辑:程序博客网 时间:2024/05/16 17:39
typeTDropFilesEvent = procedure(Receiver: TWinControl;const FileNames: TStrings;const FilesCount: Integer;const DropPoint: TPoint) of object;TlxpDraggingFilesMonitor = class(TComponent){由于组件在运行时不需要可见,所以从TComponent派生}privateFAcceptFilesControl: 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;publicconstructor Create(AOwner: TComponent); override;destructor Destroy; override;publishedproperty AcceptFilesControl: TWinControl read FAcceptFilesControlwrite SetAcceptFilesControl;property OnDropFiles: TDropFilesEvent read FOnDropFiles writeFOnDropFiles;end;constructor TlxpDraggingFilesMonitor.Create(AOwner: TComponent);begininherited;FFilesName := TStringList.Create;end;destructor TlxpDraggingFilesMonitor.Destroy;beginif Assigned(FAcceptFilesControl) thenDragAcceptFiles(FAcceptFilesControl.Handle, False);{注销文件拖放监视器}FreeAndNil(FFilesName);inherited;end;procedure TlxpDraggingFilesMonitor.SetAcceptFilesControl(const Value: TWinControl);beginif FAcceptFilesControl <> Value thenbeginif Assigned(Value) and (not (csDesigning in ComponentState)) then{设计时不执行下列代码}beginDragAcceptFiles(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);varCount, Index, hDrop: Integer;PFileName: PChar;P: TPoint;beginif Message.Msg = WM_DROPFILES then {如果是文件拖放消息}beginhDrop := Message.WParam;FFilesName.Clear;GetMem(PFileName, MAX_PATH); {给缓冲区PFileName 分配空间}Count := DragQueryFile(hDrop, MAXDWORD, PFileName, MAX_PATH-1);{取得文件总个数}for Index := 0 to Count-1 dobeginDragQueryFile(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 elseOldWindowProc(Message); {调用原来的WindowProc 过程处理别的消息}end;procedure TlxpDraggingFilesMonitor.Notification(AComponent:TComponent; Operation: TOperation);begininherited;if (AComponent = FAcceptFilesControl) and (Operation = opRemove)thenFAcceptFilesControl := nil;end;

0 0
原创粉丝点击