在Windows下有一个未公开函数SHChangeNotifyRegister可以吧你的窗口添加到系统的系统消息监视链中,该函数在Delphi <br><br>中的定义如下: <br>Function <br>SHChangeNotifyRegister(hWnd,uFlags,dwEventID,uMSG,cItems:LongWord; <br><br> lpps
IDLSTRUCT):integer;stdcall;external <br>'Shell32.dll' index 2; <br>其中参数hWnd定义了监视系统操作的窗口得句柄,参数uFlags <br>dwEventID定义监视操作参数,参数uMsg定义操作消息,参数cItems <br>定义附加参数,参数lpps指定一个PIDLSTRUCT结构,该结构指定监视的目录。 <br> 当函数调用成功之后,函数会返回一个监视操作句柄,同时系统就会将hWnd指定的窗口加入到操作监视链中,当有文件操作发生 <br><br>时,系统会向hWnd发送uMsg指定的消息,我们只要在程序中加入该消息的处理函数就可以实现对系统操作的监视了。 <br>如果要退出程序监视,就要调用另外一个未公开得函数SHChangeNotifyDeregister来取消程序监视。 <br> <br>下面是使用Delphi编写的具体程序实现范例,首先建立一个新的工程文件,然后在Form1中加入一个Button控件和一个Memo控件, <br><br>程序的代码如下: <br>unit MAIN;<br><br>interface<br><br>uses <br> Windows, Messages, SysUtils, Classes, Graphics, Controls, <br>Forms, Dialogs, <br> StdCtrls,shlobj,Activex; <br><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><br> SHCNF_IDLIST = 0; // LPITEMIDLIST <br> SHCNF_PATHA = $1; // path name <br> SHCNF_PRINTERA = $2; // printer friendly name <br> SHCNF_DWORD = $3; // DWORD <br> SHCNF_PATHW = $5; // path name <br> SHCNF_PRINTERW = $6; // printer friendly name <br> SHCNF_TYPE = $FF; <br><br> SHCNF_FLUSH = $1000; <br><br> SHCNF_FLUSHNOWAIT = $2000; <br> SHCNF_PATH = SHCNF_PATHW; <br> SHCNF_PRINTER = SHCNF_PRINTERW; <br><br> WM_SHNOTIFY = $401; <br> NOERROR = 0; <br><br>type<br> TForm1 = class(TForm)<br> Memo1: TMemo;<br> Button1: TButton;<br> procedure FormClose(Sender: TObject; var Action: <br>TCloseAction);<br> procedure Button1Click(Sender: TObject);<br> procedure FormCreate(Sender: TObject);<br> private<br> { Private declarations } <br> procedure WMShellReg(var Message:TMessage);message <br>WM_SHNOTIFY;<br> public<br> { Public declarations }<br> end;<br><br>type PSHNOTIFYSTRUCT=^SHNOTIFYSTRUCT; <br> SHNOTIFYSTRUCT = record <br> dwItem1 : PItemIDList; <br> dwItem2 : PItemIDList; <br> end; <br><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><br>Type PIDLSTRUCT = ^IDLSTRUCT;<br> _IDLSTRUCT = record <br> pidl : PItemIDList;<br> bWatchSubFolders : LongInt;<br> end; <br> IDLSTRUCT =_IDLSTRUCT; <br><br><br>function SHNotify_Register(hWnd : Integer) : Bool; <br>function SHNotify_UnRegister:Bool; <br>function <br>SHEventName(strPath1,strPath2:string;lParam:Integer):string; <br><br>Function <br>SHChangeNotifyDeregister(hNotify:integer):integer;stdcall;<br> external 'Shell32.dll' name <br>'SHChangeNotifyDeregister';<br>Function <br>SHChangeNotifyRegister(hWnd,uFlags,dwEventID,uMSG,cItems:LongWord;<br> lpps
IDLSTRUCT):integer;stdcall;external <br>'Shell32.dll' name 'SHChangeNotifyRegister';<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><br>var<br> Form1: TForm1;<br> m_hSHNotify:Integer; <br> m_pidlDesktop : PItemIDList;<br><br>implementation<br><br>{$R *.dfm}<br>function <br>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><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 := <br>strPath1+'中移去可移动存储介质'+strPath1+' '+strpath2; <br> SHCNE_DRIVEREMOVED: sEvent := '移去驱动器'+strPath1; <br> SHCNE_DRIVEADD: sEvent := '添加驱动器'+strPath1; <br> SHCNE_NETSHARE: sEvent := '改变目录'+strPath1+'的共享属性'; <br><br> SHCNE_ATTRIBUTES: sEvent := '改变文件目录属性 文件名'+strPath1; <br> SHCNE_UPDATEDIR: sEvent := '更新目录'+strPath1; <br> SHCNE_UPDATEITEM: sEvent := '更新文件 文件名:'+strPath1; <br> SHCNE_SERVERDISCONNECT: sEvent := '断开与服务器的连接'+strPath1+' <br>'+strpath2; <br> SHCNE_UPDATEIMAGE: sEvent := 'SHCNE_UPDATEIMAGE'; <br> SHCNE_DRIVEADDGUI: sEvent := 'SHCNE_DRIVEADDGUI'; <br> SHCNE_RENAMEFOLDER: sEvent := <br>'重命名文件夹'+strPath1+'为'+strpath2; <br> SHCNE_FREESPACE: sEvent := '磁盘空间大小改变'; <br> SHCNE_ASSOCCHANGED: sEvent := '改变文件关联'; <br> else <br> sEvent:='未知操作'+IntToStr(lParam); <br> end; <br> Result:=sEvent; <br>end; <br><br>function SHNotify_Register(hWnd : Integer) : Bool; <br>var <br> ps
IDLSTRUCT;<br>begin <br> {$R-} <br> Result:=False;<br> New(ps);<br> If m_hSHNotify = 0 then begin <br> //获取桌面文件夹的Pidl<br> if SHGetSpecialFolderLocation(0, <br>CSIDL_DESKTOP,m_pidlDesktop)<> NOERROR then<br> Form1.close; <br> if Boolean(m_pidlDesktop) then begin<br> ps.bWatchSubFolders := 1;<br> ps.pidl := m_pidlDesktop;<br><br> // 利用SHChangeNotifyRegister函数注册系统消息处理 <br> m_hSHNotify := SHChangeNotifyRegister(hWnd, (SHCNF_TYPE <br>Or SHCNF_IDLIST), <br> (SHCNE_ALLEVENTS Or <br>SHCNE_INTERRUPT), <br> WM_SHNOTIFY, 1, ps); <br><br> Result := Boolean(m_hSHNotify); <br> end <br> Else <br> // 如果出现错误就使用 CoTaskMemFree函数来释放句柄 <br> CoTaskMemFree(m_pidlDesktop); <br> End;<br> Dispose(ps);<br> {$R+} <br>end; <br><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 <br>begin <br> {$R-} <br> m_hSHNotify := 0; <br> CoTaskMemFree(m_pidlDesktop); <br> Result := True; <br> {$R-} <br> End; <br>end; <br><br>procedure TForm1.WMShellReg(var Message:TMessage); <br>//系统消息处理函数 <br>var <br> strPath1,strPath2:String; <br> charPath:array[0..259]of char; <br> pidlItem
SHNOTIFYSTRUCT; <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><br> <br>Memo1.Lines.Add(SHEvEntName(strPath1,strPath2,Message.lParam)+chr(13)+chr(10)); <br><br>end; <br><br>procedure TForm1.FormClose(Sender: TObject; var Action: <br>TCloseAction); <br>begin <br> //在程序退出的同时删除监视 <br> if Boolean(m_pidlDesktop) then <br> SHNotify_Unregister; <br>end; <br><br>procedure TForm1.Button1Click(Sender: TObject); <br>//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><br>procedure TForm1.FormCreate(Sender: TObject); <br>begin <br> Button1.Caption := '打开监视'; <br>end; <br><br>end.