如题,谢谢!

解决方案 »

  1.   


    一个右健菜单的例子unit ContextMenuHandler ;interface
       uses Windows,ActiveX,ComObj,ShlObj,Classes,Dialogs;type
       TContextMenu = class(TComObject,IShellExtInit,IContextMenu)
       private
          FFileName: array[0..MAX_PATH] of Char;
          FileList : TStringList;
       protected
          function IShellExtInit.Initialize = SEIInitialize; // Avoid compiler warning
          function SEIInitialize(pidlFolder: PItemIDList; lpdobj: IDataObject;
                   hKeyProgID: HKEY): HResult; stdcall;
          function QueryContextMenu(Menu: HMENU; indexMenu, idCmdFirst, idCmdLast,
                   uFlags: UINT): HResult; stdcall;
          function InvokeCommand(var lpici: TCMInvokeCommandInfo): HResult; stdcall;
          function GetCommandString(idCmd, uType: UINT; pwReserved: PUINT;
                   pszName: LPSTR; cchMax: UINT): HResult; stdcall;
    end;const   Class_ContextMenu: TGUID = ''{5027BE23-AE38-4BFE-971F-12FC837A9400}'';
    var   Buffer:array[1..1024]of char;implementationuses ComServ, SysUtils, ShellApi, Registry;function TContextMenu.SEIInitialize(pidlFolder: PItemIDList; lpdobj: IDataObject;
       hKeyProgID: HKEY): HResult;
    var
       StgMedium: TStgMedium;
       FormatEtc: TFormatEtc;
       FileNumber,i:Integer;
    begin//  如果lpdobj等于Nil,则本调用失败
       if (lpdobj = nil) then begin
          Result := E_INVALIDARG;
          Exit;
       end;//      首先初始化并清空FileList以添加文件
       FileList:=TStringList.Create;
       FileList.Clear;
    //  初始化剪贴版格式文件
       with FormatEtc do begin
          cfFormat := CF_HDROP;
          ptd := nil;
          dwAspect := DVASPECT_CONTENT;
          lindex := -1;
          tymed := TYMED_HGLOBAL;
       end;
       Result := lpdobj.GetData(FormatEtc, StgMedium);
       if Failed(Result) then Exit;//   首先查询用户选中的文件的个数
       FileNumber := DragQueryFile(StgMedium.hGlobal,$FFFFFFFF,nil,0);
    //   循环读取,将所有用户选中的文件保存到FileList中
       for i:=0 to FileNumber-1 do begin
          DragQueryFile(StgMedium.hGlobal, i, FFileName, SizeOf(FFileName));
          FileList.Add(FFileName);
          Result := NOERROR;
       end;   ReleaseStgMedium(StgMedium);
    end;function TContextMenu.QueryContextMenu(Menu: HMENU; indexMenu, idCmdFirst,
       idCmdLast, uFlags: UINT): HResult;
    begin
       Result := 0;
       if ((uFlags and $0000000F) = CMF_NORMAL) or
          ((uFlags and CMF_EXPLORE) <> 0) then begin
     // 往Context Menu中加入一个菜单项
        InsertMenu(Menu, indexMenu, MF_STRING or MF_BYPOSITION, idCmdFirst,
          PChar(''@_@文档压缩......''));
     // 返回增加菜单项的个数
       Result := 1;
       end;
    end;function TContextMenu.InvokeCommand(var lpici: TCMInvokeCommandInfo): HResult;
    var
    //   sFile:TFileStream;
       charSavePath:array[0..1023]of char;
       sSaveFile:String;
       i:Integer;
       F: TextFile;
       FirstLine: string;
    begin
       // 首先确定该过程是被系统而不是被一个程序所调用
       Result := E_FAIL;   if (HiWord(Integer(lpici.lpVerb)) <> 0) then
       begin
    //      Result := E_FAIL;
          Exit;
       end;
       // 确定传递的参数的有效性
       if (LoWord(lpici.lpVerb) <> 0) then begin
          Result := E_INVALIDARG;
          Exit;
       end;// 建立一个临时文件保存用户选中的文件名
    //   GetTempPath(1024,charSavePath);
       GetWindowsDirectory(charSavePath,256);
       sSaveFile:=charSavePath+''\Temp\T0001.tmp'';
    //   sSaveFile:=charSavePath+''T0001.tmp'';
       AssignFile(F,sSaveFile);   { next file in Files property }
       ReWrite(F);
    //  将文件名保存到临时文件中
       for i:= 0 to FileList.Count -1 do begin
          FirstLine:=FileList.Strings[i];
          Writeln(F,FirstLine);    { Read the first line out of the file }
       end;
       CloseFile(F);
    //   调用文件操作程序对用户选中的文件进行操作
       ShellExecute(0,nil,''D:\work\目录压缩\compr.exe'',PChar(sSaveFile),charSavePath,SW_NORMAL);
       Result := NOERROR;
    end;function TContextMenu.GetCommandString(idCmd, uType: UINT; pwReserved: PUINT;
             pszName: LPSTR; cchMax: UINT): HRESULT;
    begin
       if (idCmd = 0) then begin
       if (uType = GCS_HELPTEXT) then
          {返回该菜单项的帮助信息,此帮助信息将在用户把鼠标移动到该菜单项时出现在状态条上。}
          StrCopy(pszName, PChar(''点击该菜单项将执行文件操作''));
          Result := NOERROR;
       end
       else
          Result := E_INVALIDARG;
    end; type
       TContextMenuFactory = class(TComObjectFactory)
       public
       procedure UpdateRegistry(Register: Boolean); override;
    end; procedure TContextMenuFactory.UpdateRegistry(Register: Boolean);
    var
       ClassID: string;
    begin
       if Register then begin
          inherited UpdateRegistry(Register);
          ClassID := GUIDToString(Class_ContextMenu);
          CreateRegKey(''Folder\shellex'', '''', '''');
          CreateRegKey(''Folder\shellex\ContextMenuHandlers'', '''', '''');
          CreateRegKey(''Folder\shellex\ContextMenuHandlers\OpenWithWordPad'', '''', ClassID);
          CreateRegKey(''*\shellex'', '''', '''');
          CreateRegKey(''*\shellex\ContextMenuHandlers'', '''', '''');
          CreateRegKey(''*\shellex\ContextMenuHandlers\OpenWithWordPad'', '''', ClassID);      CreateRegKey(''.pkg'', '''', ''pkgFile'');      CreateRegKey(''pkgFile'', '''', '''');
          CreateRegKey(''pkgFile\DefaultIcon'', '''', ''D:\work\目录压缩\compr.exe,0'');
          CreateRegKey(''pkgFile\shell'', '''', '''');
          CreateRegKey(''pkgFile\shell\open'', '''', '''');
          CreateRegKey(''pkgFile\shell\open\Command'', '''', ''"D:\work\目录压缩\compr.exe" "%1"'');
          CreateRegKey(''pkgFile\shell\myshell'', '''', ''@_@文档释放......'');
          CreateRegKey(''pkgFile\shell\myshell\Command'', '''', ''"D:\work\目录压缩\compr.exe" "%1"'');//    如果操作系统为Windows NT的话
          if (Win32Platform = VER_PLATFORM_WIN32_NT) then
          with TRegistry.Create do
          try
             RootKey := HKEY_LOCAL_MACHINE;
             OpenKey(''SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions'', True);
             OpenKey(''Approved'', True);
             WriteString(ClassID, ''Context Menu Shell Extension'');
          finally
             Free;
          end;
       end
       else begin
          DeleteRegKey(''*\shellex\ContextMenuHandlers\FileOpreation'');
          DeleteRegKey(''*\shellex\ContextMenuHandlers'');
    //      DeleteRegKey(''*\shellex'');      DeleteRegKey(''Folder\shellex\ContextMenuHandlers\FileOpreation'');
          DeleteRegKey(''Folder\shellex\ContextMenuHandlers'');
    //      DeleteRegKey(''Folder\shellex'');      DeleteRegKey(''pkgFile\shell\myshell\Command'');
          DeleteRegKey(''pkgFile\shell\myshell'');
          DeleteRegKey(''pkgFile\shell\open'');
          DeleteRegKey(''pkgFile\shell'');
          DeleteRegKey(''pkgFile\DefaultIcon'');      inherited UpdateRegistry(Register);
       end;
    end; initialization
     TContextMenuFactory.Create(ComServer, TContextMenu, Class_ContextMenu,
       ''ContextMenu'', ''Context Menu Shell Extension'', ciMultiInstance,tmApartment);end.