实现CopyHook接口<br><br>unit CopyMain;<br><br>interface<br><br>uses Windows, ComObj, ShlObj;<br><br>type<br> TCopyHook = class(TComObject, ICopyHook)<br> protected<br> function CopyCallback(Wnd: HWND; wFunc, wFlags: UINT; pszSrcFile: PAnsiChar;<br> dwSrcAttribs: DWORD; pszDestFile: PAnsiChar; dwDestAttribs: DWORD): UINT; stdcall;<br> end;<br><br> TCopyHookFactory = class(TComObjectFactory)<br> protected<br> function GetProgID: string; override;<br> procedure ApproveShellExtension(Register: Boolean; const ClsID: string);<br> virtual;<br> public<br> procedure UpdateRegistry(Register: Boolean); override;<br> end;<br><br>implementation<br><br>uses ComServ, SysUtils, Registry;<br><br>{ TCopyHook }<br><br>// This is the method which is called by the shell for folder operations<br>function TCopyHook.CopyCallback(Wnd: HWND; wFunc, wFlags: UINT;<br> pszSrcFile: PAnsiChar; dwSrcAttribs: DWORD; pszDestFile: PAnsiChar;<br> dwDestAttribs: DWORD): UINT;<br>const<br> MyMessage: string = 'Are you sure you want to mess with "%s"?';<br>begin<br> // confirm operation<br> Result := MessageBox(Wnd, PChar(Format(MyMessage, [pszSrcFile])),<br> 'D4DG Shell Extension', MB_YESNO);<br>end;<br><br>{ TCopyHookFactory }<br><br>function TCopyHookFactory.GetProgID: string;<br>begin<br> // ProgID not needed for shell extension<br> Result := '';<br>end;<br><br>procedure TCopyHookFactory.UpdateRegistry(Register: Boolean);<br>var<br> ClsID: string;<br>begin<br> ClsID := GUIDToString(ClassID);<br> inherited UpdateRegistry(Register);<br> ApproveShellExtension(Register, ClsID);<br> if Register then<br> // add shell extension clsid to CopyHookHandlers reg entry<br> CreateRegKey('directory/shellex/CopyHookHandlers/' + ClassName, '',<br> ClsID)<br> else<br> DeleteRegKey('directory/shellex/CopyHookHandlers/' + ClassName);<br>end;<br><br>procedure TCopyHookFactory.ApproveShellExtension(Register: Boolean;<br> const ClsID: string);<br>// This registry entry is required in order for the extension to<br>// operate correctly under Windows NT.<br>const<br> SApproveKey = 'SOFTWARE/Microsoft/Windows/CurrentVersion/Shell Extensions/Approved';<br>begin<br> with TRegistry.Create do<br> try<br> RootKey := HKEY_LOCAL_MACHINE;<br> if not OpenKey(SApproveKey, True) then Exit;<br> if Register then WriteString(ClsID, Description)<br> else DeleteValue(ClsID);<br> finally<br> Free;<br> end;<br>end;<br><br>const<br> CLSID_CopyHook: TGUID = '{66CD5F60-A044-11D0-A9BF-00A024E3867F}';<br><br>initialization<br> TCopyHookFactory.Create(ComServer, TCopyHook, CLSID_CopyHook,<br> 'D4DG_CopyHook', 'D4DG Copy Hook Shell Extension Example',<br> ciMultiInstance, tmApartment);<br>end.<br>