有文件夹或文件被删除的消息是什么?怎么知道被删除文件的位置?(40分)

  • 主题发起人 主题发起人 stuwei
  • 开始时间 开始时间
找找有关hook和shell编程的有关文章你就知道了<br>一般是shell编程类的
 
我在COM编程里面也见到过!
 
WINSHELLAPI int WINAPI SHFileOperation(<br>&nbsp; &nbsp; LPSHFILEOPSTRUCT lpFileOp <br>&nbsp; &nbsp;);<br><br>typedef struct _SHFILEOPSTRUCT { // shfos &nbsp;<br>&nbsp; &nbsp; HWND &nbsp; &nbsp; &nbsp; &nbsp; hwnd; <br>&nbsp; &nbsp; UINT &nbsp; &nbsp; &nbsp; &nbsp; wFunc; <br>&nbsp; &nbsp; LPCSTR &nbsp; &nbsp; &nbsp; pFrom; <br>&nbsp; &nbsp; LPCSTR &nbsp; &nbsp; &nbsp; pTo; <br>&nbsp; &nbsp; FILEOP_FLAGS fFlags; <br>&nbsp; &nbsp; BOOL &nbsp; &nbsp; &nbsp; &nbsp; fAnyOperationsAborted; <br>&nbsp; &nbsp; LPVOID &nbsp; &nbsp; &nbsp; hNameMappings; <br>&nbsp; &nbsp; LPCSTR &nbsp; &nbsp; &nbsp; lpszProgressTitle; <br>} SHFILEOPSTRUCT, FAR *LPSHFILEOPSTRUCT; <br>更具体的说明可以参照Delphi Win SDK的帮助文件和MSDN<br>我记得TurboPower有一组system的控件包中包含一个这样的控件。但是很长时间没用<br>记不太清楚了。如果需要我可以帮你找一下。
 
tingjie所给的方法好像是文件操作的函数吧,我想要的是得到文件被删除的消息
 
自己帮自己UP
 
自己UP<br>大家帮忙啊,解决了另有重谢不胜感激!!!!!!!!!!!!!!!!!!
 
这个问题是Com里的copyHook的经典例子了。一般是和上下文菜单一块讲的。<br><br>你找本讲com的书,里面应该都有的。
 
delphi7有控件
 
给你个好东西,用类做的。可监视多种文件操作动作。<br>http://www.howtodothings.com/showarticle.asp?article=314<br>那上面还有许多可学习的,如果你的英文够好。<br><br>If you have ever needed to know when the shell issues an event here is a<br>&nbsp;wrapper around SHChangeNotifyRegister wich allows you to know the kind of <br>event and the file(s) affected, this component is in stage 1 of developement <br>however it offers the whole functionality.<br><br>The component itself is composed of enhancements made to TSHChangeNotify <br>component by Elliott Shevin (shevine@aol.com)<br><br>By the way here is a list of things you should be aware when using this <br>component for instance restoring an item from the recycle bin to the original <br>location turn on a rename event, while you get a notification over an event <br>you cannot stop it you just get notified of it.<br><br>Deleting an Item causes 3 events: OnFileDelete (When the dialog, or the lack<br>&nbsp;of, is displayed normally this is before the deletion of the file) an<br>&nbsp;OnFolderUpdate (after the element is removed and the folder refreshes) and <br>surprise yet another OnFileDelete (after the file has completely been deleted)<br>&nbsp;this behavior is not of the component but of the api it relies on.<br><br>Also according to some documentation I read the apis used on this article are <br>entirely undocumented, perhaps it can be possible to stop the event however <br>due to lack of documentation, it is unknown to me.<br><br>&lt;-------------- Begin UNIT code ----------------------------&gt;<br>{$IFNDEF VER80} {$IFNDEF VER90} {$IFNDEF VER93}<br>&nbsp; {$DEFINE Delphi3orHigher}<br>{$ENDIF} {$ENDIF} {$ENDIF}<br><br>unit ShellNotify;<br>interface<br><br>uses Windows, Messages, SysUtils, Classes, Controls, Forms, Dialogs,<br>&nbsp; {$IFNDEF Delphi3orHigher} OLE2, {$ELSE} ActiveX, ComObj, {$ENDIF}<br>&nbsp; ShlObj;<br><br>type<br>&nbsp; NOTIFYREGISTER = record<br>&nbsp; &nbsp; pidlPath : PItemIDList;<br>&nbsp; &nbsp; bWatchSubtree : boolean;<br>&nbsp; end;<br>&nbsp; PNOTIFYREGISTER = ^NOTIFYREGISTER;<br><br>const<br>&nbsp; SNM_SHELLNOTIFICATION = WM_USER +1;<br>&nbsp; SHCNF_ACCEPT_INTERRUPTS = $0001;<br>&nbsp; SHCNF_ACCEPT_NON_INTERRUPTS = $0002;<br>&nbsp; SHCNF_NO_PROXY = $8000;<br><br>type<br>&nbsp; TNotificationEvent = (neAssociationChange, neAttributesChange,<br>&nbsp; &nbsp; neFileChange, neFileCreate, neFileDelete, neFileRename,<br>&nbsp; &nbsp; neDriveAdd, neDriveRemove, neShellDriveAdd, neDriveSpaceChange,<br>&nbsp; &nbsp; neMediaInsert, neMediaRemove, neFolderCreate, neFolderDelete,<br>&nbsp; &nbsp; neFolderRename, neFolderUpdate, neNetShare, neNetUnShare,<br>&nbsp; &nbsp; neServerDisconnect, neImageListChange);<br>&nbsp; TNotificationEvents = set of TNotificationEvent;<br><br>&nbsp; TShellNotificationEvent1 = procedure(Sender: TObject;<br>&nbsp; &nbsp; Path: String)of Object;<br>&nbsp; TShellNotificationEvent2 = procedure(Sender: TObject;<br>&nbsp; &nbsp; path1, path2: String) of Object;<br><br>&nbsp; TShellNotification = class(TComponent)<br>&nbsp; private<br>&nbsp; &nbsp; fWatchEvents: TNotificationEvents;<br>&nbsp; &nbsp; fPath: String;<br>&nbsp; &nbsp; fActive, fWatch: Boolean;<br><br>&nbsp; &nbsp; prevPath1, prevPath2: String;<br>&nbsp; &nbsp; PrevEvent: Integer;<br><br>&nbsp; &nbsp; Handle, NotifyHandle: HWND;<br><br>&nbsp; &nbsp; fOnAssociationChange: TNotifyEvent;<br>&nbsp; &nbsp; fOnAttribChange: TShellNotificationEvent2;<br>&nbsp; &nbsp; FOnCreate: TShellNotificationEvent1;<br>&nbsp; &nbsp; FOnDelete: TShellNotificationEvent1;<br>&nbsp; &nbsp; FOnDriveAdd: TShellNotificationEvent1;<br>&nbsp; &nbsp; FOnDriveAddGui: TShellNotificationEvent1;<br>&nbsp; &nbsp; FOnDriveRemove: TShellNotificationEvent1;<br>&nbsp; &nbsp; FOnMediaInsert: TShellNotificationEvent1;<br>&nbsp; &nbsp; FOnMediaRemove: TShellNotificationEvent1;<br>&nbsp; &nbsp; FOnDirCreate: TShellNotificationEvent1;<br>&nbsp; &nbsp; FOnNetShare: TShellNotificationEvent1;<br>&nbsp; &nbsp; FOnNetUnShare: TShellNotificationEvent1;<br>&nbsp; &nbsp; FOnRenameFolder: TShellNotificationEvent2;<br>&nbsp; &nbsp; FOnItemRename: TShellNotificationEvent2;<br>&nbsp; &nbsp; FOnFolderRemove: TShellNotificationEvent1;<br>&nbsp; &nbsp; FOnServerDisconnect: TShellNotificationEvent1;<br>&nbsp; &nbsp; FOnFolderUpdate: TShellNotificationEvent1;<br><br>&nbsp; &nbsp; function PathFromPidl(Pidl: PItemIDList): String;<br>&nbsp; &nbsp; procedure SetWatchEvents(const Value: TNotificationEvents);<br><br>&nbsp; &nbsp; function GetActive: Boolean;<br>&nbsp; &nbsp; procedure SetActive(const Value: Boolean);<br><br>&nbsp; &nbsp; procedure SetPath(const Value: String);<br>&nbsp; &nbsp; procedure SetWatch(const Value: Boolean);<br>&nbsp; protected<br>&nbsp; &nbsp; procedure ShellNotifyRegister;<br>&nbsp; &nbsp; procedure ShellNotifyUnregister;<br>&nbsp; &nbsp; procedure WndProc(var Message: TMessage);<br><br>&nbsp; &nbsp; procedure DoAssociationChange; dynamic;<br>&nbsp; &nbsp; procedure DoAttributesChange(Path1, Path2: String); dynamic;<br>&nbsp; &nbsp; procedure DoCreateFile(Path: String); dynamic;<br>&nbsp; &nbsp; procedure DoDeleteFile(Path: String); dynamic;<br>&nbsp; &nbsp; procedure DoDriveAdd(Path:String); dynamic;<br>&nbsp; &nbsp; procedure DoDriveAddGui(Path: String); dynamic;<br>&nbsp; &nbsp; procedure DoDriveRemove(Path: String); dynamic;<br>&nbsp; &nbsp; procedure DoMediaInsert(Path: String); dynamic;<br>&nbsp; &nbsp; procedure DoMediaRemove(Path: String); dynamic;<br>&nbsp; &nbsp; procedure DoDirCreate(Path: String); dynamic;<br>&nbsp; &nbsp; procedure DoNetShare(Path: String); dynamic;<br>&nbsp; &nbsp; procedure DoNetUnShare(Path: String); dynamic;<br>&nbsp; &nbsp; procedure DoRenameFolder(Path1, Path2: String); dynamic;<br>&nbsp; &nbsp; procedure DoRenameItem(Path1, Path2: String); dynamic;<br>&nbsp; &nbsp; procedure DoFolderRemove(Path: String); dynamic;<br>&nbsp; &nbsp; procedure DoServerDisconnect(Path: String); dynamic;<br>&nbsp; &nbsp; procedure DoDirUpdate(Path: String); dynamic;<br>&nbsp; public<br>&nbsp; &nbsp; constructor Create(AOwner: TComponent); override;<br>&nbsp; &nbsp; destructor Destroy; override;<br>&nbsp; published<br>&nbsp; &nbsp; property Path: String read fPath write SetPath;<br>&nbsp; &nbsp; property Active: Boolean read GetActive write SetActive;<br>&nbsp; &nbsp; property WatchSubTree: Boolean read fWatch write SetWatch;<br><br>&nbsp; &nbsp; property WatchEvents: TNotificationEvents<br>&nbsp; &nbsp; read fWatchEvents write SetWatchEvents;<br><br>&nbsp; &nbsp; property OnAssociationChange: TNotifyEvent<br>&nbsp; &nbsp; read fOnAssociationChange write FOnAssociationChange;<br><br>&nbsp; &nbsp; property OnAttributesChange: TShellNotificationEvent2<br>&nbsp; &nbsp; read fOnAttribChange write fOnAttribChange;<br><br>&nbsp; &nbsp; property OnFileCreate: TShellNotificationEvent1<br>&nbsp; &nbsp; read FOnCreate write FOnCreate;<br><br>&nbsp; &nbsp; property OnFolderRename: TShellNotificationEvent2<br>&nbsp; &nbsp; read FOnRenameFolder write FOnRenameFolder;<br><br>&nbsp; &nbsp; property OnFolderUpdate: TShellNotificationEvent1<br>&nbsp; &nbsp; read FOnFolderUpdate write FOnFolderUpdate;<br><br>&nbsp; &nbsp; property OnFileDelete: TShellNotificationEvent1<br>&nbsp; &nbsp; read FOnDelete write FOnDelete;<br><br>&nbsp; &nbsp; property OnDriveAdd: TShellNotificationEvent1<br>&nbsp; &nbsp; read FOnDriveAdd write FOnDriveAdd;<br><br>&nbsp; &nbsp; property OnFolderRemove: TShellNotificationEvent1<br>&nbsp; &nbsp; read FOnFolderRemove write FOnFolderRemove;<br><br>&nbsp; &nbsp; property OnItemRename: TShellNotificationEvent2<br>&nbsp; &nbsp; read FOnItemRename write FOnItemRename;<br><br>&nbsp; &nbsp; property OnDriveAddGui: TShellNotificationEvent1<br>&nbsp; &nbsp; read FOnDriveAddGui write FOnDriveAddGui;<br><br>&nbsp; &nbsp; property OnDriveRemove: TShellNotificationEvent1<br>&nbsp; &nbsp; read FOnDriveRemove write FOnDriveRemove;<br><br>&nbsp; &nbsp; property OnMediaInserted: TShellNotificationEvent1<br>&nbsp; &nbsp; read FOnMediaInsert write FOnMediaInsert;<br><br>&nbsp; &nbsp; property OnMediaRemove: TShellNotificationEvent1<br>&nbsp; &nbsp; read FOnMediaRemove write FOnMediaRemove;<br><br>&nbsp; &nbsp; property OnDirCreate: TShellNotificationEvent1<br>&nbsp; &nbsp; read FOnDirCreate write FOnDirCreate;<br><br>&nbsp; &nbsp; property OnNetShare: TShellNotificationEvent1<br>&nbsp; &nbsp; read FOnNetShare write FOnNetShare;<br><br>&nbsp; &nbsp; property OnNetUnShare: TShellNotificationEvent1<br>&nbsp; &nbsp; read FOnNetUnShare write FOnNetUnShare;<br><br>&nbsp; &nbsp; property OnServerDisconnect: TShellNotificationEvent1<br>&nbsp; &nbsp; read FOnServerDisconnect write FOnServerDisconnect;<br>&nbsp; end;<br><br>&nbsp; function SHChangeNotifyRegister( hWnd: HWND; dwFlags: integer;<br>&nbsp; &nbsp; &nbsp; &nbsp;wEventMask : cardinal; uMsg: UINT; cItems : integer;<br>&nbsp; &nbsp; &nbsp; &nbsp;lpItems : PNOTIFYREGISTER) : HWND; stdcall;<br>&nbsp; function SHChangeNotifyDeregister(hWnd: HWND) : boolean; stdcall;<br>&nbsp; function SHILCreateFromPath(Path: Pointer; PIDL: PItemIDList;<br>&nbsp; &nbsp; &nbsp; var Attributes: ULONG):HResult; stdcall;<br><br>&nbsp; procedure Register;<br><br>implementation<br><br>const Shell32DLL = 'shell32.dll';<br><br>&nbsp; function SHChangeNotifyRegister; external Shell32DLL index 2;<br>&nbsp; function SHChangeNotifyDeregister; external Shell32DLL index 4;<br>&nbsp; function SHILCreateFromPath; external Shell32DLL index 28;<br><br>{ TShellNotification }<br><br>constructor TShellNotification.Create(AOwner: TComponent);<br>begin<br>&nbsp; inherited Create( AOwner );<br>&nbsp; if not (csDesigning in ComponentState) then<br>&nbsp; &nbsp; Handle := AllocateHWnd(WndProc);<br>&nbsp; PrevEvent := 0;<br>&nbsp; fWatchEvents := [neAssociationChange, neAttributesChange,<br>&nbsp; &nbsp; neFileChange, neFileCreate, neFileDelete, neFileRename,<br>&nbsp; &nbsp; neDriveAdd, neDriveRemove, neShellDriveAdd, neDriveSpaceChange,<br>&nbsp; &nbsp; neMediaInsert, neMediaRemove, neFolderCreate, neFolderDelete,<br>&nbsp; &nbsp; neFolderRename, neFolderUpdate, neNetShare, neNetUnShare,<br>&nbsp; &nbsp; neServerDisconnect, neImageListChange];<br>end;<br><br>destructor TShellNotification.Destroy;<br>begin<br>&nbsp; if not (csDesigning in ComponentState) then<br>&nbsp; &nbsp; Active := False;<br>&nbsp; if Handle &lt;&gt; 0 then DeallocateHWnd( Handle );<br>&nbsp; inherited Destroy;<br>end;<br><br>procedure TShellNotification.DoAssociationChange;<br>begin<br>&nbsp; if Assigned( fOnAssociationChange ) and (neAssociationChange in fWatchEvents) then<br>&nbsp; &nbsp; fOnAssociationChange( Self );<br>end;<br><br>procedure TShellNotification.DoAttributesChange;<br>begin<br>&nbsp; if Assigned( fOnAttribChange ) then<br>&nbsp; &nbsp; fOnAttribChange( Self, Path1, Path2 );<br>end;<br><br>procedure TShellNotification.DoCreateFile(Path: String);<br>begin<br>&nbsp; if Assigned( fOnCreate ) then<br>&nbsp; &nbsp; FOnCreate(Self, Path)<br>end;<br><br>procedure TShellNotification.DoDeleteFile(Path: String);<br>begin<br>&nbsp; if Assigned( FOnDelete ) then<br>&nbsp; &nbsp; FOnDelete(Self, Path);<br>end;<br><br>procedure TShellNotification.DoDirCreate(Path: String);<br>begin<br>&nbsp; if Assigned( FOnDirCreate ) then<br>&nbsp; &nbsp; FOnDirCreate( Self, Path );<br>end;<br><br>procedure TShellNotification.DoDirUpdate(Path: String);<br>begin<br>&nbsp; if Assigned( FOnFolderUpdate ) then<br>&nbsp; &nbsp; FOnFolderUpdate(Self, Path);<br>end;<br><br>procedure TShellNotification.DoDriveAdd(Path: String);<br>begin<br>&nbsp; if Assigned( FOnDriveAdd ) then<br>&nbsp; &nbsp; FOnDriveAdd(Self, Path);<br>end;<br><br>procedure TShellNotification.DoDriveAddGui(Path: String);<br>begin<br>&nbsp; if Assigned( FOnDriveAddGui ) then<br>&nbsp; &nbsp; FOnDriveAdd(Self, Path);<br>end;<br><br>procedure TShellNotification.DoDriveRemove(Path: String);<br>begin<br>&nbsp; if Assigned( FOnDriveRemove ) then<br>&nbsp; &nbsp; FOnDriveRemove(Self, Path);<br>end;<br><br>procedure TShellNotification.DoFolderRemove(Path: String);<br>begin<br>&nbsp; if Assigned(FOnFolderRemove) then<br>&nbsp; &nbsp; FOnFolderRemove( Self, Path );<br>end;<br><br>procedure TShellNotification.DoMediaInsert(Path: String);<br>begin<br>&nbsp; if Assigned( FOnMediaInsert ) then<br>&nbsp; &nbsp; FOnMediaInsert(Self, Path);<br>end;<br><br>procedure TShellNotification.DoMediaRemove(Path: String);<br>begin<br>&nbsp; if Assigned(FOnMediaRemove) then<br>&nbsp; &nbsp; FOnMediaRemove(Self, Path);<br>end;<br><br>procedure TShellNotification.DoNetShare(Path: String);<br>begin<br>&nbsp; if Assigned(FOnNetShare) then<br>&nbsp; &nbsp; FOnNetShare(Self, Path);<br>end;<br><br>procedure TShellNotification.DoNetUnShare(Path: String);<br>begin<br>&nbsp; if Assigned(FOnNetUnShare) then<br>&nbsp; &nbsp; FOnNetUnShare(Self, Path);<br>end;<br><br>procedure TShellNotification.DoRenameFolder(Path1, Path2: String);<br>begin<br>&nbsp; if Assigned( FOnRenameFolder ) then<br>&nbsp; &nbsp; FOnRenameFolder(Self, Path1, Path2);<br>end;<br><br>procedure TShellNotification.DoRenameItem(Path1, Path2: String);<br>begin<br>&nbsp; if Assigned( FOnItemRename ) then<br>&nbsp; &nbsp; FonItemRename(Self, Path1, Path2);<br>end;<br><br>procedure TShellNotification.DoServerDisconnect(Path: String);<br>begin<br>&nbsp; if Assigned( FOnServerDisconnect ) then<br>&nbsp; &nbsp; FOnServerDisconnect(Self, Path);<br>end;<br><br>function TShellNotification.GetActive: Boolean;<br>begin<br>&nbsp; Result := (NotifyHandle &lt;&gt; 0) and (fActive);<br>end;<br><br>function TShellNotification.PathFromPidl(Pidl: PItemIDList): String;<br>begin<br>&nbsp; SetLength(Result, Max_Path);<br>&nbsp; if not SHGetPathFromIDList(Pidl, PChar(Result)) then Result := '';<br>&nbsp; if pos(#0, Result) &gt; 0 then<br>&nbsp; &nbsp; SetLength(Result, pos(#0, Result));<br>end;<br><br>procedure TShellNotification.SetActive(const Value: Boolean);<br>begin<br>&nbsp; if (Value &lt;&gt; fActive) then<br>&nbsp; begin<br>&nbsp; &nbsp; fActive := Value;<br>&nbsp; &nbsp; if fActive then ShellNotifyRegister else ShellNotifyUnregister;<br>&nbsp; end;<br>end;<br><br>procedure TShellNotification.SetPath(const Value: String);<br>begin<br>&nbsp; if fPath &lt;&gt; Value then<br>&nbsp; begin<br>&nbsp; &nbsp; fPath := Value;<br>&nbsp; &nbsp; ShellNotifyRegister;<br>&nbsp; end;<br>end;<br><br>procedure TShellNotification.SetWatch(const Value: Boolean);<br>begin<br>&nbsp; if fWatch &lt;&gt; Value then<br>&nbsp; begin<br>&nbsp; &nbsp; fWatch := Value;<br>&nbsp; &nbsp; ShellNotifyRegister;<br>&nbsp; end;<br>end;<br><br>procedure TShellNotification.SetWatchEvents(<br>&nbsp; const Value: TNotificationEvents);<br>begin<br>&nbsp; if fWatchEvents &lt;&gt; Value then<br>&nbsp; begin<br>&nbsp; &nbsp; fWatchEvents := Value;<br>&nbsp; &nbsp; ShellNotifyRegister;<br>&nbsp; end;<br>end;<br><br>procedure TShellNotification.ShellNotifyRegister;<br>var<br>&nbsp; Option: TNotificationEvent;<br>&nbsp; NotifyRecord: NOTIFYREGISTER;<br>&nbsp; Flags: DWORD;<br>&nbsp; Pidl: PItemIDList;<br>&nbsp; Attributes: ULONG;<br>const<br>&nbsp; NotifyFlags: array[TNotificationEvent] of DWORD = (<br>&nbsp; &nbsp; SHCNE_ASSOCCHANGED, SHCNE_ATTRIBUTES, SHCNE_UPDATEITEM,<br>&nbsp; &nbsp; SHCNE_CREATE, SHCNE_DELETE, SHCNE_RENAMEITEM, SHCNE_DRIVEADD,<br>&nbsp; &nbsp; SHCNE_DRIVEREMOVED, SHCNE_DRIVEADDGUI, SHCNE_FREESPACE,<br>&nbsp; &nbsp; SHCNE_MEDIAINSERTED, SHCNE_MEDIAREMOVED, SHCNE_MKDIR,<br>&nbsp; &nbsp; SHCNE_RMDIR, SHCNE_RENAMEFOLDER, SHCNE_UPDATEDIR,<br>&nbsp; &nbsp; SHCNE_NETSHARE, SHCNE_NETUNSHARE, SHCNE_SERVERDISCONNECT,<br>&nbsp; &nbsp; SHCNE_UPDATEIMAGE);<br>begin<br>&nbsp; ShellNotifyUnregister;<br>&nbsp; if not (csDesigning in ComponentState) and<br>&nbsp; &nbsp; &nbsp;not (csLoading in ComponentState) then<br>&nbsp; begin<br>&nbsp; &nbsp; SHILCreatefromPath( PChar(fPath), Addr(Pidl), Attributes);<br>&nbsp; &nbsp; NotifyRecord.pidlPath := Pidl;<br>&nbsp; &nbsp; NotifyRecord.bWatchSubtree := fWatch;<br><br>&nbsp; &nbsp; Flags := 0;<br>&nbsp; &nbsp; for Option := Low(Option) to High(Option) do<br>&nbsp; &nbsp; &nbsp; if (Option in FWatchEvents) then<br>&nbsp; &nbsp; &nbsp; &nbsp; Flags := Flags or NotifyFlags[Option];<br><br>&nbsp; &nbsp; NotifyHandle := SHChangeNotifyRegister(Handle,<br>&nbsp; &nbsp; &nbsp; SHCNF_ACCEPT_INTERRUPTS or SHCNF_ACCEPT_NON_INTERRUPTS,<br>&nbsp; &nbsp; &nbsp; Flags, SNM_SHELLNOTIFICATION, 1, @NotifyRecord);<br>&nbsp; end;<br>end;<br><br>procedure TShellNotification.ShellNotifyUnregister;<br>begin<br>&nbsp; if NotifyHandle &lt;&gt; 0 then<br>&nbsp; &nbsp; SHChangeNotifyDeregister(NotifyHandle);<br>end;<br><br>procedure TShellNotification.WndProc(var Message: TMessage);<br>type<br>&nbsp; &nbsp;TPIDLLIST = record<br>&nbsp; &nbsp; &nbsp; pidlist : array[1..2] of PITEMIDLIST;<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;PIDARRAY = ^TPIDLLIST;<br>var<br>&nbsp; &nbsp;Path1 : string;<br>&nbsp; &nbsp;Path2 : string;<br>&nbsp; &nbsp;ptr : PIDARRAY;<br>&nbsp; &nbsp;repeated : boolean;<br>&nbsp; &nbsp;event : longint;<br><br>begin<br>&nbsp; case Message.Msg of<br>&nbsp; &nbsp; SNM_SHELLNOTIFICATION:<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; event := Message.LParam and ($7FFFFFFF);<br>&nbsp; &nbsp; &nbsp; Ptr := PIDARRAY(Message.WParam);<br><br>&nbsp; &nbsp; &nbsp; Path1 := PathFromPidl( Ptr^.pidlist[1] );<br>&nbsp; &nbsp; &nbsp; Path2 := PathFromPidl( Ptr^.pidList[2] );<br><br>&nbsp; &nbsp; &nbsp; repeated := (PrevEvent = event)<br>&nbsp; &nbsp; &nbsp; &nbsp; and (uppercase(prevpath1) = uppercase(Path1))<br>&nbsp; &nbsp; &nbsp; &nbsp; and (uppercase(prevpath2) = uppercase(Path2));<br><br>&nbsp; &nbsp; &nbsp; if Repeated then exit;<br><br>&nbsp; &nbsp; &nbsp; PrevEvent := Message.Msg;<br>&nbsp; &nbsp; &nbsp; prevPath1 := Path1;<br>&nbsp; &nbsp; &nbsp; prevPath2 := Path2;<br><br>&nbsp; &nbsp; &nbsp; case event of<br>&nbsp; &nbsp; &nbsp; &nbsp; SHCNE_ASSOCCHANGED : DoAssociationChange;<br>&nbsp; &nbsp; &nbsp; &nbsp; SHCNE_ATTRIBUTES : DoAttributesChange( Path1, Path2);<br>&nbsp; &nbsp; &nbsp; &nbsp; SHCNE_CREATE : DoCreateFile(Path1);<br>&nbsp; &nbsp; &nbsp; &nbsp; SHCNE_DELETE : DoDeleteFile(Path1);<br>&nbsp; &nbsp; &nbsp; &nbsp; SHCNE_DRIVEADD : DoDriveAdd(Path1);<br>&nbsp; &nbsp; &nbsp; &nbsp; SHCNE_DRIVEADDGUI : DoDriveAddGui(path1);<br>&nbsp; &nbsp; &nbsp; &nbsp; SHCNE_DRIVEREMOVED : DoDriveRemove(Path1);<br>&nbsp; &nbsp; &nbsp; &nbsp; SHCNE_MEDIAINSERTED : DoMediaInsert(Path1);<br>&nbsp; &nbsp; &nbsp; &nbsp; SHCNE_MEDIAREMOVED : DoMediaRemove(Path1);<br>&nbsp; &nbsp; &nbsp; &nbsp; SHCNE_MKDIR : DoDirCreate(Path1);<br>&nbsp; &nbsp; &nbsp; &nbsp; SHCNE_NETSHARE : DoNetShare(Path1);<br>&nbsp; &nbsp; &nbsp; &nbsp; SHCNE_NETUNSHARE : DoNetUnShare(Path1);<br>&nbsp; &nbsp; &nbsp; &nbsp; SHCNE_RENAMEFOLDER : DoRenameFolder(Path1, Path2);<br>&nbsp; &nbsp; &nbsp; &nbsp; SHCNE_RENAMEITEM : DoRenameItem(Path1, Path2);<br>&nbsp; &nbsp; &nbsp; &nbsp; SHCNE_RMDIR : DoFolderRemove(Path1);<br>&nbsp; &nbsp; &nbsp; &nbsp; SHCNE_SERVERDISCONNECT : DoServerDisconnect(Path);<br>&nbsp; &nbsp; &nbsp; &nbsp; SHCNE_UPDATEDIR : DoDirUpdate(Path);<br>&nbsp; &nbsp; &nbsp; &nbsp; SHCNE_UPDATEIMAGE : ;<br>&nbsp; &nbsp; &nbsp; &nbsp; SHCNE_UPDATEITEM : ;<br>&nbsp; &nbsp; &nbsp; end;//Case event of<br>&nbsp; &nbsp; end;//SNM_SHELLNOTIFICATION<br>&nbsp; end; //case<br>end;<br><br>procedure Register;<br>begin<br>&nbsp; RegisterComponents('Shell', [TShellNotification]);<br>end;<br><br>end.<br>&lt;------------------ End Unit Code --------------------------&gt;<br><br>还有下面的。。。<br><br>你是否想为你的Windows加上一双眼睛,察看使用者在机器上所做的各种操作(例如建立、删除文件;改变文件或目录名字)呢?<br><br>  这里介绍一种利用Windows未公开函数实现这个功能的方法。<br><br>  在Windows下有一个未公开函数SHChangeNotifyRegister可以把你的窗口添加到系统的系统消息监视链中,该函数在Delphi中的定义如下:<br><br>  Function SHChangeNotifyRegister(hWnd,uFlags,dwEventID,uMSG,cItems:LongWord;<br>  lpps:PIDLSTRUCT):integer;stdcall;external 'Shell32.dll' index 2;<br><br>  其中参数hWnd定义了监视系统操作的窗口得句柄,参数uFlags dwEventID定义监视操作参数,参数uMsg定义操作消息,参数cItems定义附加参数,参数lpps指定一个PIDLSTRUCT结构,该结构指定监视的目录。<br><br>  当函数调用成功之后,函数会返回一个监视操作句柄,同时系统就会将hWnd指定的窗口加入到操作监视链中,当有文件操作发生时,系统会向hWnd发送uMsg指定的消息,我们只要在程序中加入该消息的处理函数就可以实现对系统操作的监视了。<br><br>  如果要退出程序监视,就要调用另外一个未公开得函数SHChangeNotifyDeregister来取消程序监视。<br><br>  下面是使用Delphi编写的具体程序实现范例,首先建立一个新的工程文件,然后在Form1中加入一个Button控件和一个Memo控件,<br><br>  程序的代码如下:<br><br>  unit Unit1;<br>  interface<br>  uses<br>  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>  StdCtrls,shlobj,Activex;<br>  const<br>  SHCNE_RENAMEITEM = $1;<br>  SHCNE_CREATE = $2;<br>  SHCNE_DELETE = $4;<br>  SHCNE_MKDIR = $8;<br>  SHCNE_RMDIR = $10;<br>  SHCNE_MEDIAINSERTED = $20;<br>  SHCNE_MEDIAREMOVED = $40;<br>  SHCNE_DRIVEREMOVED = $80;<br>  SHCNE_DRIVEADD = $100;<br>  SHCNE_NETSHARE = $200;<br>  SHCNE_NETUNSHARE = $400;<br>  SHCNE_ATTRIBUTES = $800;<br>  SHCNE_UPDATEDIR = $1000;<br>  SHCNE_UPDATEITEM = $2000;<br>  SHCNE_SERVERDISCONNECT = $4000;<br>  SHCNE_UPDATEIMAGE = $8000;<br>  SHCNE_DRIVEADDGUI = $10000;<br>  SHCNE_RENAMEFOLDER = $20000;<br>  SHCNE_FREESPACE = $40000;<br>  SHCNE_ASSOCCHANGED = $8000000;<br>  SHCNE_DISKEVENTS = $2381F;<br>  SHCNE_GLOBALEVENTS = $C0581E0;<br>  SHCNE_ALLEVENTS = $7FFFFFFF;<br>  SHCNE_INTERRUPT = $80000000;<br>  SHCNF_IDLIST = 0;<br>  // LPITEMIDLIST<br>  SHCNF_PATHA = $1;<br>  // path name<br>  SHCNF_PRINTERA = $2;<br>  // printer friendly name<br>  SHCNF_DWORD = $3;<br>  // DWORD<br>  SHCNF_PATHW = $5;<br>  // path name<br>  SHCNF_PRINTERW = $6;<br>  // printer friendly name<br>  SHCNF_TYPE = $FF;<br>  SHCNF_FLUSH = $1000;<br>  SHCNF_FLUSHNOWAIT = $2000;<br>  SHCNF_PATH = SHCNF_PATHW;<br>  SHCNF_PRINTER = SHCNF_PRINTERW;<br>  WM_SHNOTIFY = $401;<br>  NOERROR = 0;<br>  type<br>  TForm1 = class(TForm)<br>  Button1: TButton;<br>  Memo1: TMemo;<br>  procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>  procedure Button1Click(Sender: TObject);<br>  procedure FormCreate(Sender: TObject);<br>  private<br>  { Private declarations }<br>  procedure WMShellReg(var Message:TMessage);message WM_SHNOTIFY;<br>  public<br>  { Public declarations }<br>  end;<br>  type PSHNOTIFYSTRUCT=^SHNOTIFYSTRUCT;<br>  SHNOTIFYSTRUCT = record<br>  dwItem1 : PItemIDList;<br>  dwItem2 : PItemIDList;<br>  end;<br>  Type PSHFileInfoByte=^SHFileInfoByte;<br>  _SHFileInfoByte = record<br>  hIcon :Integer;<br>  iIcon :Integer;<br>  dwAttributes : Integer;<br>  szDisplayName : array [0..259] of char;<br>  szTypeName : array [0..79] of char;<br>  end;<br>  SHFileInfoByte=_SHFileInfoByte;<br>  Type PIDLSTRUCT = ^IDLSTRUCT;<br>  _IDLSTRUCT = record<br>  pidl : PItemIDList;<br>  bWatchSubFolders : Integer;<br>  end;<br>  IDLSTRUCT =_IDLSTRUCT;<br>  function SHNotify_Register(hWnd : Integer) : Bool;<br>  function SHNotify_UnRegister:Bool;<br>  function SHEventName(strPath1,strPath2:string;lParam:Integer):string;<br>  Function SHChangeNotifyDeregister(hNotify:integer):integer;stdcall;<br>  external 'Shell32.dll' index 4;<br>  Function SHChangeNotifyRegister(hWnd,uFlags,dwEventID,uMSG,cItems:LongWord;<br>  lpps:PIDLSTRUCT):integer;stdcall;external 'Shell32.dll' index 2;<br>  Function SHGetFileInfoPidl(pidl : PItemIDList;<br>  dwFileAttributes : Integer;<br>  psfib : PSHFILEINFOBYTE;<br>  cbFileInfo : Integer;<br>  uFlags : Integer):Integer;stdcall;<br>  external 'Shell32.dll' name 'SHGetFileInfoA';<br>  var<br>  Form1: TForm1;<br>  m_hSHNotify:Integer;<br>  m_pidlDesktop : PItemIDList;<br>  implementation<br>  {$R *.DFM}<br>  function SHEventName(strPath1,strPath2:string;lParam:Integer):string;<br>  var<br>  sEvent:String;<br>  begin<br>  case lParam of //根据参数设置提示消息<br>  SHCNE_RENAMEITEM: sEvent := '重命名文件'+strPath1+'为'+strpath2;<br>  SHCNE_CREATE: sEvent := '建立文件 文件名:'+strPath1;<br>  SHCNE_DELETE: sEvent := '删除文件 文件名:'+strPath1;<br>  SHCNE_MKDIR: sEvent := '新建目录 目录名:'+strPath1;<br>  SHCNE_RMDIR: sEvent := '删除目录 目录名:'+strPath1;<br>  SHCNE_MEDIAINSERTED: sEvent := strPath1+'中插入可移动存储介质';<br>  SHCNE_MEDIAREMOVED: sEvent := strPath1+'中移去可移动存储介质'+strPath1+' '+strpath2;<br>  SHCNE_DRIVEREMOVED: sEvent := '移去驱动器'+strPath1;<br>  SHCNE_DRIVEADD: sEvent := '添加驱动器'+strPath1;<br>  SHCNE_NETSHARE: sEvent := '改变目录'+strPath1+'的共享属性';<br>  SHCNE_ATTRIBUTES: sEvent := '改变文件目录属性 文件名'+strPath1;<br>  SHCNE_UPDATEDIR: sEvent := '更新目录'+strPath1;<br>  SHCNE_UPDATEITEM: sEvent := '更新文件 文件名:'+strPath1;<br>  SHCNE_SERVERDISCONNECT: sEvent := '断开与服务器的连接'+strPath1+' '+strpath2;<br>  SHCNE_UPDATEIMAGE: sEvent := 'SHCNE_UPDATEIMAGE';<br>  SHCNE_DRIVEADDGUI: sEvent := 'SHCNE_DRIVEADDGUI';<br>  SHCNE_RENAMEFOLDER: sEvent := '重命名文件夹'+strPath1+'为'+strpath2;<br>  SHCNE_FREESPACE: sEvent := '磁盘空间大小改变';<br>  SHCNE_ASSOCCHANGED: sEvent := '改变文件关联';<br>  else<br>  sEvent:='未知操作'+IntToStr(lParam);<br>  end;<br>  Result:=sEvent;<br>  end;<br>  function SHNotify_Register(hWnd : Integer) : Bool;<br>  var<br>  ps:PIDLSTRUCT;<br>  begin<br>  {$R-}<br>  Result:=False;<br>  If m_hSHNotify = 0 then begin<br>  //获取桌面文件夹的Pidl<br>  if SHGetSpecialFolderLocation(0, CSIDL_DESKTOP,<br>  m_pidlDesktop)&lt;&gt; NOERROR then<br>  Form1.close;<br>  if Boolean(m_pidlDesktop) then begin<br>  ps.bWatchSubFolders := 1;<br>  ps.pidl := m_pidlDesktop;<br>  // 利用SHChangeNotifyRegister函数注册系统消息处理<br>  m_hSHNotify := SHChangeNotifyRegister(hWnd, (SHCNF_TYPE Or SHCNF_IDLIST),<br>  (SHCNE_ALLEVENTS Or SHCNE_INTERRUPT),<br>  WM_SHNOTIFY, 1, ps);<br>  Result := Boolean(m_hSHNotify);<br>  end<br>  Else<br>  // 如果出现错误就使用 CoTaskMemFree函数来释放句柄<br>  CoTaskMemFree(m_pidlDesktop);<br>  End;<br>  {$R+}<br>  end;<br>  function SHNotify_UnRegister:Bool;<br>  begin<br>  Result:=False;<br>  If Boolean(m_hSHNotify) Then<br>  //取消系统消息监视,同时释放桌面的Pidl<br>  If Boolean(SHChangeNotifyDeregister(m_hSHNotify)) Then begin<br>  {$R-}<br>  m_hSHNotify := 0;<br>  CoTaskMemFree(m_pidlDesktop);<br>  Result := True;<br>  {$R-}<br>  End;<br>  end;<br>  procedure TForm1.WMShellReg(var Message:TMessage); //系统消息处理函数<br>  var<br>  strPath1,strPath2:String;<br>charPath:array[0..259]of char;<br>  pidlItem:PSHNOTIFYSTRUCT;<br>  begin<br>  pidlItem:=PSHNOTIFYSTRUCT(Message.wParam);<br>   //获得系统消息相关得路径<br>  SHGetPathFromIDList(pidlItem.dwItem1,charPath);<br>  strPath1:=charPath;<br>  SHGetPathFromIDList(pidlItem.dwItem2,charPath);<br>  strPath2:=charPath;<br>  Memo1.Lines.Add(SHEvEntName(strPath1,strPath2,Message.lParam)+chr(13)+chr(10));<br>  end;<br>  procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>  begin<br>  //在程序退出的同时删除监视<br>  if Boolean(m_pidlDesktop) then<br>  SHNotify_Unregister;<br>  end;<br>  procedure TForm1.Button1Click(Sender: TObject); //Button1的Click消息<br>  begin<br>  m_hSHNotify:=0;<br>  if SHNotify_Register(Form1.Handle) then begin //注册Shell监视<br>  ShowMessage('Shell监视程序成功注册');<br>  Button1.Enabled := False;<br>  end<br>  else<br>  ShowMessage('Shell监视程序注册失败');<br>  end;<br>  procedure TForm1.FormCreate(Sender: TObject);<br>  begin<br>  Button1.Caption := '打开监视';<br>  end;<br>  end.<br><br>  运行程序,点击“打开监视”按钮,如果出现一个显示“Shell监视程序成功注册”的对话框,说明Form1已经加入到系统操作监视链中了,你可以试着在资源管理器中建立、删除文件夹,移动文件等操作,你可以发现这些操作都被记录下来并显示在文本框中。<br><br>  在上面的程序中多次使用到了一个PItemIDList的结构,这个数据结构指定Windows下得一个“项目”,在Windows下资源实现统一管理一个“项目”可以是一个文件或者一个文件夹,也可以是一个打印机等资源。另外一些API函数也涉及到了Shell(Windows外壳)操作,各位读者可以参考相应的参考资料。<br><br>  由于使用到了Windows的未公开函数,没有相关得参考资料,所以有一些未知得操作(在Memo1中会显示“未知操作”)。如果哪位读者有兴趣, http://member.netease.com/~blackcat 有实现该功能的VB程序下载。<br><br>  以上程序在Windows98、Windows2000、Delphi5下运行通过。<br><br><br>
 
第二种方法中<br>----------<br>  //获取桌面文件夹的Pidl<br>  if SHGetSpecialFolderLocation(0, CSIDL_DESKTOP,<br>  m_pidlDesktop)&lt;&gt; NOERROR then<br>  Form1.close;<br>  if Boolean(m_pidlDesktop) then begin<br>  ps.bWatchSubFolders := 1; &nbsp;//???<br>  ps.pidl := m_pidlDesktop; //???<br>&nbsp;-------------<br>倒数后两句,我在运行时出现奇怪的问题,<br>第一次在<br>ps.bWatchSubFolders := 1; &nbsp;处出现Access Violation<br>屏蔽后正常运行,<br>隔了几天再次运行,竟然在<br>ps.pidl := m_pidlDesktop; &nbsp;处也出现Access Violation,不知什么变了,<br>我根据以上代码改写的类在这里竟然都不出错,晕。<br>有谁感兴趣,试试在你的机器上出不出错,我的环境是W2k+D6<br><br><br>  
 
放分了,谢谢各位
 

Similar threads

回复
0
查看
818
不得闲
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
517
import
I
后退
顶部