home *** CD-ROM | disk | FTP | other *** search
/ PC Expert 29 / Pce29cd.iso / RUNIMAGE / DELPHI40 / DEMOS / ACTIVEX / SHELLEXT / CONTEXTM.PAS next >
Pascal/Delphi Source File  |  1998-06-16  |  6KB  |  188 lines

  1. unit ContextM;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, ActiveX, ComObj, ShlObj;
  7.  
  8. type
  9.   TContextMenu = class(TComObject, IShellExtInit, IContextMenu)
  10.   private
  11.     FFileName: array[0..MAX_PATH] of Char;
  12.   protected
  13.     { IShellExtInit }
  14.     function IShellExtInit.Initialize = SEIInitialize; // Avoid compiler warning
  15.     function SEIInitialize(pidlFolder: PItemIDList; lpdobj: IDataObject;
  16.       hKeyProgID: HKEY): HResult; stdcall;
  17.     { IContextMenu }
  18.     function QueryContextMenu(Menu: HMENU; indexMenu, idCmdFirst, idCmdLast,
  19.       uFlags: UINT): HResult; stdcall;
  20.     function InvokeCommand(var lpici: TCMInvokeCommandInfo): HResult; stdcall;
  21.     function GetCommandString(idCmd, uType: UINT; pwReserved: PUINT;
  22.       pszName: LPSTR; cchMax: UINT): HResult; stdcall;
  23.   end;
  24.  
  25. const
  26.   Class_ContextMenu: TGUID = '{EBDF1F20-C829-11D1-8233-0020AF3E97A9}';
  27.  
  28. implementation
  29.  
  30. uses ComServ, SysUtils, ShellApi, Registry;
  31.  
  32. function TContextMenu.SEIInitialize(pidlFolder: PItemIDList; lpdobj: IDataObject;
  33.   hKeyProgID: HKEY): HResult;
  34. var
  35.   StgMedium: TStgMedium;
  36.   FormatEtc: TFormatEtc;
  37. begin
  38.   // Fail the call if lpdobj is Nil.
  39.   if (lpdobj = nil) then begin
  40.     Result := E_INVALIDARG;
  41.     Exit;
  42.   end;
  43.  
  44.   with FormatEtc do begin
  45.     cfFormat := CF_HDROP;
  46.     ptd      := nil;
  47.     dwAspect := DVASPECT_CONTENT;
  48.     lindex   := -1;
  49.     tymed    := TYMED_HGLOBAL;
  50.   end;
  51.  
  52.   // Render the data referenced by the IDataObject pointer to an HGLOBAL
  53.   // storage medium in CF_HDROP format.
  54.   Result := lpdobj.GetData(FormatEtc, StgMedium);
  55.   if Failed(Result) then
  56.     Exit;
  57.   // If only one file is selected, retrieve the file name and store it in
  58.   // FFileName. Otherwise fail the call.
  59.   if (DragQueryFile(StgMedium.hGlobal, $FFFFFFFF, nil, 0) = 1) then begin
  60.     DragQueryFile(StgMedium.hGlobal, 0, FFileName, SizeOf(FFileName));
  61.     Result := NOERROR;
  62.   end
  63.   else begin
  64.     FFileName[0] := #0;
  65.     Result := E_FAIL;
  66.   end;
  67.   ReleaseStgMedium(StgMedium);
  68. end;
  69.  
  70. function TContextMenu.QueryContextMenu(Menu: HMENU; indexMenu, idCmdFirst,
  71.           idCmdLast, uFlags: UINT): HResult;
  72. begin
  73.   Result := 0; // or use MakeResult(SEVERITY_SUCCESS, FACILITY_NULL, 0);
  74.  
  75.   if ((uFlags and $0000000F) = CMF_NORMAL) or
  76.      ((uFlags and CMF_EXPLORE) <> 0) then begin
  77.     // Add one menu item to context menu
  78.     InsertMenu(Menu, indexMenu, MF_STRING or MF_BYPOSITION, idCmdFirst,
  79.       'Compile...');
  80.  
  81.     // Return number of menu items added
  82.     Result := 1; // or use MakeResult(SEVERITY_SUCCESS, FACILITY_NULL, 1)
  83.   end;
  84. end;
  85.  
  86. function GetCompilerPath: string;
  87. // Returns string containing path to Delphi command line compiler
  88. var
  89.   Reg: TRegistry;
  90. begin
  91.   Reg := TRegistry.Create;
  92.   try
  93.     with Reg do begin
  94.       RootKey := HKEY_LOCAL_MACHINE;
  95.       OpenKey('\SOFTWARE\Borland\Delphi\4.0', False);
  96.       Result := ReadString('RootDir') + '\bin\dcc32.exe';
  97.     end;
  98.     if AnsiPos(' ', Result) <> 0 then
  99.       Result := ExtractShortPathName(Result);
  100.     Result := Result + ' "%s"';
  101.   finally
  102.     Reg.Free;
  103.   end;
  104. end;
  105.  
  106. function TContextMenu.InvokeCommand(var lpici: TCMInvokeCommandInfo): HResult;
  107. var
  108.   H: THandle;
  109. begin
  110.   // Make sure we are not being called by an application
  111.   if (HiWord(Integer(lpici.lpVerb)) <> 0) then
  112.   begin
  113.     Result := E_FAIL;
  114.     Exit;
  115.   end;
  116.  
  117.   // Make sure we aren't being passed an invalid argument number
  118.   if (LoWord(lpici.lpVerb) <> 0) then begin
  119.     Result := E_INVALIDARG;
  120.     Exit;
  121.   end;
  122.  
  123.   // Execute the command specified by lpici.lpVerb
  124.   // by invoking the Delphi command line compiler.
  125.   H := WinExec(PChar(Format(GetCompilerPath, [FFileName])), lpici.nShow);
  126.   if (H < 32) then
  127.     MessageBox(lpici.hWnd, 'Error executing Delphi compiler.', 'Error',
  128.       MB_ICONERROR or MB_OK);
  129.   Result := NOERROR;
  130. end;
  131.  
  132. function TContextMenu.GetCommandString(idCmd, uType: UINT; pwReserved: PUINT;
  133.   pszName: LPSTR; cchMax: UINT): HRESULT;
  134. begin
  135.   if (idCmd = 0) then begin
  136.     if (uType = GCS_HELPTEXT) then
  137.       // return help string for menu item
  138.       StrCopy(pszName, 'Compile the selected Delphi project');
  139.     Result := NOERROR;
  140.   end
  141.   else
  142.     Result := E_INVALIDARG;
  143. end;
  144.  
  145. type
  146.   TContextMenuFactory = class(TComObjectFactory)
  147.   public
  148.     procedure UpdateRegistry(Register: Boolean); override;
  149.   end;
  150.  
  151. procedure TContextMenuFactory.UpdateRegistry(Register: Boolean);
  152. var
  153.   ClassID: string;
  154. begin
  155.   if Register then begin
  156.     inherited UpdateRegistry(Register);
  157.  
  158.     ClassID := GUIDToString(Class_ContextMenu);
  159.     CreateRegKey('DelphiProject\shellex', '', '');
  160.     CreateRegKey('DelphiProject\shellex\ContextMenuHandlers', '', '');
  161.     CreateRegKey('DelphiProject\shellex\ContextMenuHandlers\ContMenu', '', ClassID);
  162.  
  163.     if (Win32Platform = VER_PLATFORM_WIN32_NT) then
  164.       with TRegistry.Create do
  165.         try
  166.           RootKey := HKEY_LOCAL_MACHINE;
  167.           OpenKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions', True);
  168.           OpenKey('Approved', True);
  169.           WriteString(ClassID, 'Delphi 4.0 Context Menu Shell Extension Example');
  170.         finally
  171.           Free;
  172.         end;
  173.   end
  174.   else begin
  175.     DeleteRegKey('DelphiProject\shellex\ContextMenuHandlers\ContMenu');
  176.     DeleteRegKey('DelphiProject\shellex\ContextMenuHandlers');
  177.     DeleteRegKey('DelphiProject\shellex');
  178.  
  179.     inherited UpdateRegistry(Register);
  180.   end;
  181. end;
  182.  
  183. initialization
  184.   TContextMenuFactory.Create(ComServer, TContextMenu, Class_ContextMenu,
  185.     '', 'Delphi 4.0 Context Menu Shell Extension Example', ciMultiInstance,
  186.     tmApartment);
  187. end.
  188.