关于添加资源管理器右键菜单(200分求救) (200分)

  • 主题发起人 主题发起人 kgen
  • 开始时间 开始时间
添加菜单项和操作单个文件我都知道怎样做,但我也想知道:
1.如何知道传过来的是 文件名 还是 目录名
2.用户点取的是多个文件和目录如何得到这些文件和目录的列表



 
还好,还没有散分,我就把代码贴过来。有点乱,自己整理吧!
Listing 1 - FormView.dpr
library FormView;
uses ComServ, FormViewImpl in 'FormViewImpl.pas';exports DllGetClassObject, DllCanUnloadNow, DllRegisterServer, DllUnregisterServer;{$R *.RES}beginend.


Listing 2 - FormViewImpl.pas{===============================================================
FormViewImpl
Unit This unit implements the TFormViewContentMenu class, which is a COM object that supports the IShellExt and IContextMenu interfaces.===============================================================}
unit FormViewImpl;
interface
uses Windows, Forms, StdCtrls, ShellApi, SysUtils, Classes, Controls, ComServ, ComObj, ShlObj, ActiveX;const CLSID_DelphiFormViewerContextMenu: TGUID = '{F169D961-B907-11D0-B8FA-A85800C10000}';
type TFormViewContextMenu = class( TComObject, IShellExtInit, IContextMenu ) private FFileName: string; public // IShellExtInit Methods // Use a Method Resolution Clause because Initialize is // defined as a virtual method in TComObject function IShellExtInit.Initialize = ShellInit; function ShellInit( Folder: PItemIDList; DataObject: IDataObject; ProgID: HKEY ): HResult; stdcall; // IContextMenu Methods function QueryContextMenu( Menu: HMENU; Index, CmdFirst, CmdLast, Flags: UINT ): HResult; stdcall; function GetCommandString( Cmd, Flags: UINT; Reserved: PUINT; Name: LPSTR; MaxSize: UINT ): HResult; stdcall; function InvokeCommand( var CommandInfo: TCMInvokeCommandInfo ): HResult; stdcall; end;implementationuses Registry;{==================================}{== TFormViewContextMenu Methods ==}{==================================}function TFormViewContextMenu.ShellInit( Folder: PItemIDList; DataObject: IDataObject; ProgID: HKEY ): HResult;var Medium: TStgMedium; FE: TFormatEtc;begin if DataObject = nil then begin Result := E_FAIL; Exit; end; with FE do begin cfFormat := CF_HDROP; ptd := nil; dwAspect := DVASPECT_CONTENT; lindex := -1; tymed := TYMED_HGLOBAL; end; // Transfer the data referenced by the IDataObject reference to // an HGLOBAL storage medium in CF_HDROP format. Result := DataObject.GetData( FE, Medium ); if Failed( Result ) then Exit; try // If only one file is selected, retrieve the file name and // store it in FileName. Otherwise fail. if DragQueryFile( Medium.hGlobal, $FFFFFFFF, nil, 0) = 1 then begin SetLength( FFileName, MAX_PATH ); DragQueryFile( Medium.hGlobal, 0, PChar(FFileName), MAX_PATH); Result := NOERROR; end else Result := E_FAIL; finally ReleaseStgMedium( Medium ); end;end;function TFormViewContextMenu.QueryContextMenu( Menu: HMENU; Index, CmdFirst, CmdLast, Flags: UINT ): HResult;var MenuText: string; AddMenuItem: Boolean;begin AddMenuItem := True; if ( Flags and $000F ) = CMF_NORMAL then MenuText := 'View (Form File on Desktop) as Text' else if ( Flags and CMF_VERBSONLY ) <> 0 then MenuText := 'View (Form File via Shortcut) as Text' else if ( Flags and CMF_EXPLORE ) <> 0 then MenuText := 'View (Form File in Explorer) as Text' else AddMenuItem := False; if AddMenuItem then begin InsertMenu( Menu, Index, mf_String or mf_ByPosition, CmdFirst, PChar( MenuText ) ); Result := 1; // Return number of menu items added end else Result := NOERROR;end; {= TFormViewContextMenu.QueryContextMenu =}function TFormViewContextMenu.GetCommandString( Cmd, Flags: UINT; Reserved: PUINT; Name: LPSTR; MaxSize: UINT ): HResult;begin case Cmd of 0: begin if Flags = GCS_HELPTEXT then begin // Return the string to be displayed in the Explorer // status bar when the menu item is selected StrCopy(Name, 'View the selected Delphi form file as text'); end; Result := NOERROR; end; else // Invalid menu item Result := E_INVALIDARG; end;end; {= TFormViewContextMenu.GetCommandString =}function GetViewerPath: string;var R: TRegIniFile;begin R := TRegIniFile.Create( '/Software/Raize/FormFileViewer' ); try Result := R.ReadString( 'Program', 'Path', '' ); Result := '"' + Result + '" "%s"'; finally R.Free; end;end;function TFormViewContextMenu.InvokeCommand( var CommandInfo: TCMInvokeCommandInfo ): HResult;var Success: Boolean; CmdLine: string; SI: TStartupInfo; PI: TProcessInformation;begin // Make sure we are not being called by an application if HiWord( Integer( CommandInfo.lpVerb ) ) <> 0 then begin Result := E_FAIL; Exit; end; // Execute the command specified by CommandInfo.lpVerb case LoWord( CommandInfo.lpVerb ) of 0: begin FillChar( SI, SizeOf( SI ), #0 ); SI.cb := SizeOf( SI ); SI.wShowWindow := sw_ShowNormal; SI.dwFlags := STARTF_USESHOWWINDOW; CmdLine := Format( GetViewerPath, [ FFileName ] ); Success := CreateProcess( nil, PChar( CmdLine ), nil, nil, True, 0, nil, nil, SI, PI ); if not Success then begin MessageBox( CommandInfo.hWnd, 'Could not start the Form File Viewer.', 'Error', mb_IconError or mb_OK ); end; Result := NOERROR; end; else // Invalid menu item Result := E_INVALIDARG; end; { case }end; {= TFormViewContextMenu.InvokeCommand =}initialization // Create a COM object factory which will be responsible for // creating instances of our shell extension. ComServer is // declared in ComServ unit. TComObjectFactory.Create( ComServer, TFormViewContextMenu, CLSID_DelphiFormViewerContextMenu, '', 'View Delphi Form Files', ciMultiInstance );end.


Listing 3 - RegMain.pasunit RegMain;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Panel1: TPanel; Label1: TLabel; Image1: TImage; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.DFM}uses Registry;const FormViewerClassID = '{F169D961-B907-11D0-B8FA-A85800C10000}';procedure TForm1.FormCreate(Sender: TObject);var Reg: TRegistry;begin Reg := TRegistry.Create; try with Reg do begin RootKey := HKEY_CLASSES_ROOT; OpenKey( '/CLSID/' + FormViewerClassID, True ); WriteString( '', 'Delphi Form Viewer Context Menu Shell Extension'); OpenKey( '/CLSID/' + FormViewerClassID + '/InProcServer32', True ); WriteString( '', ExtractFilePath( Application.ExeName ) + '/FormView.dll' ); WriteString( 'ThreadingModel', 'Apartment' ); CreateKey( '/DelphiForm/shellex/ContextMenuHandlers/' + FormViewerClassID ); end; finally Reg.Free; end;end;procedure TForm1.Button1Click(Sender: TObject);begin Close;end;end.

 

Similar threads

回复
0
查看
804
不得闲
D
回复
0
查看
825
DelphiTeacher的专栏
D
D
回复
0
查看
831
DelphiTeacher的专栏
D
后退
顶部