unit DragFilesMonitor;interfaceuses
Controls,Classes,Types,messages;procedure Register;type
TDropFilesEvent = procedure(Receiver:TWinControl;const FileName:TStrings;
const FilesCount:integer; const DropPoint:TPoint) of object;
TDragFilesMonitor = class(TComponent)
private
FAcceptFilesControl:TWinControl;
OldWindowProc:TWndMethod;
FFilesName:TStrings;
FOnDropFiles:TDropFilesEvent;
procedure SetAcceptFilesControl(const Value:TWinControl);
protected
procedure NewWindowProc(var message:TMessage);
procedure Notification(AComponent:TComponent;Operation:Toperation);override;
public
constructor create(AOwner:TComponent);override;
destructor destroy;override;
published{***********属性和事件都没有在ObjectInspector中显示,为什么?***********}
property AcceptFilesControl:TWinControl read FAcceptFilesControl
write FAcceptFilesControl;
property OnDropFiles:TDropFilesEvent read FOnDropFiles write FOnDropFiles;
end;{TGragFilesMonitor}implementationuses
ShellApi,SysUtils;
constructor TDragFilesMonitor.create(AOwner:TComponent);
begin
inherited;
FFilesName:=TStringList.create;
end;destructor TDragFilesMonitor.destroy;
begin
if Assigned(FAcceptFilesControl) then
DragAcceptFiles(FAcceptFilesControl.handle,false);
FreeAndNil(FFilesName);
inherited;
end;procedure TDragFilesMonitor.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;
value.windowProc:=NewWindowProc;
end;
FacceptFilesControl:=value;
FAcceptFilesControl.FreeNotification(self);
end;
end;procedure TDragFilesMonitor.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,260);
count:=dragqueryfile(hdrop,$FFFFFFFF, pfilename,255);
for index:=0 to count-1 do begin
dragqueryfile(hdrop,index,pfilename,0);
FFilesName.add(PFileName);
end;{for}
DragFinish(hDrop);
if Assigned(FOnDropFIles) then
FOnDropFiles(FAcceptFilesControl,FFilesName,count,p);
FreeMem(PFileName);
DragFinish(hdrop);
end else
OldWindowProc(message);
end;procedure TDragFilesMonitor.Notification(Acomponent:TComponent;
Operation:TOperation);
begin
inherited;
if (AComponent=FAcceptFilesControl)and(Operation=opRemove) then
FAcceptFilesControl:=nil;
end;procedure Register;
begin
RegisterComponents('MyComponent',[TDragFilesMonitor]);
end;end.
Controls,Classes,Types,messages;procedure Register;type
TDropFilesEvent = procedure(Receiver:TWinControl;const FileName:TStrings;
const FilesCount:integer; const DropPoint:TPoint) of object;
TDragFilesMonitor = class(TComponent)
private
FAcceptFilesControl:TWinControl;
OldWindowProc:TWndMethod;
FFilesName:TStrings;
FOnDropFiles:TDropFilesEvent;
procedure SetAcceptFilesControl(const Value:TWinControl);
protected
procedure NewWindowProc(var message:TMessage);
procedure Notification(AComponent:TComponent;Operation:Toperation);override;
public
constructor create(AOwner:TComponent);override;
destructor destroy;override;
published{***********属性和事件都没有在ObjectInspector中显示,为什么?***********}
property AcceptFilesControl:TWinControl read FAcceptFilesControl
write FAcceptFilesControl;
property OnDropFiles:TDropFilesEvent read FOnDropFiles write FOnDropFiles;
end;{TGragFilesMonitor}implementationuses
ShellApi,SysUtils;
constructor TDragFilesMonitor.create(AOwner:TComponent);
begin
inherited;
FFilesName:=TStringList.create;
end;destructor TDragFilesMonitor.destroy;
begin
if Assigned(FAcceptFilesControl) then
DragAcceptFiles(FAcceptFilesControl.handle,false);
FreeAndNil(FFilesName);
inherited;
end;procedure TDragFilesMonitor.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;
value.windowProc:=NewWindowProc;
end;
FacceptFilesControl:=value;
FAcceptFilesControl.FreeNotification(self);
end;
end;procedure TDragFilesMonitor.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,260);
count:=dragqueryfile(hdrop,$FFFFFFFF, pfilename,255);
for index:=0 to count-1 do begin
dragqueryfile(hdrop,index,pfilename,0);
FFilesName.add(PFileName);
end;{for}
DragFinish(hDrop);
if Assigned(FOnDropFIles) then
FOnDropFiles(FAcceptFilesControl,FFilesName,count,p);
FreeMem(PFileName);
DragFinish(hdrop);
end else
OldWindowProc(message);
end;procedure TDragFilesMonitor.Notification(Acomponent:TComponent;
Operation:TOperation);
begin
inherited;
if (AComponent=FAcceptFilesControl)and(Operation=opRemove) then
FAcceptFilesControl:=nil;
end;procedure Register;
begin
RegisterComponents('MyComponent',[TDragFilesMonitor]);
end;end.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货