Print Page | Close Window

How to use explorer context for your tool

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: Sample Code
Forum Description: Share Debenu Quick PDF Library sample code with other forum members
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=2457
Printed Date: 22 Nov 24 at 7:12PM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: How to use explorer context for your tool
Posted By: Ingo
Subject: How to use explorer context for your tool
Date Posted: 17 Nov 12 at 1:29PM
Hi!


I can answer regarding Delphi/Pascal.
First i give you the needed functions and at the end you'll find code how to use it.

Don't forget ShlObj and Registry in the usage section.


function RegisterFileAssociation( FileAssocInfo : TFileAssocInfo):boolean;

  {-----------------------------------------------------}
  function IsEmptyString(const S: string): boolean;
  begin
  Result := ( S = '' );
  end;
  {-----------------------------------------------------}
  function AssebleOpenCmd(OpenCmd, AppPath:string):string;
  begin
    OpenCmd := Trim(OpenCmd);
    if not IsEmptyString(OpenCmd) then OpenCmd := OpenCmd + ' ';
    Result := AppPath + ' ' + OpenCmd + '%1';
  end;
  {-----------------------------------------------------}

var
S : string;
begin

  Result := False;

  with  FileAssocInfo do
  begin
    if IsEmptyString(AppPath) then Exit;
    if IsEmptyString(Extension) then Exit;
    if Length(Extension) > 3 then raise Exception.Create('Too long extension');
    if IsEmptyString(Key) then Exit;
    if IsEmptyString(Description) then Exit;
    if AddLocalMenu and IsEmptyString(MenuString) then Exit;

    Extension := '.' + Extension;


    { write... }
    with TRegIniFile.Create('') do
    try
      RootKey := HKEY_CLASSES_ROOT;

      { write the extension - the Key points to description registy key}
      WriteString(Extension, '', Key);

      { write the description key }
      WriteString(Key, '', Description);

      { write the default icon if defined }
      if not IsEmptyString(Icon) then WriteString(Key + '\DefaultIcon','',Icon);

      { write the OpenCmd - association }
      OpenCmd := AssebleOpenCmd(OpenCmd, AppPath);
      WriteString(Key + '\shell\open\command', '', OpenCmd );

      { add the Explorer local menu item }
      if AddLocalMenu
      then
        begin
          S := Key + '\shell\' + 'Local_Menu';
          WriteString(S, '', MenuString);
          S := Key + '\shell\' + 'Local_Menu' + '\command';
          WriteString(S, '', OpenCmd);
        end;

    finally
      CloseKey;
      Free;
    end;

  end;

  Result := True;

end;

procedure DeleteFileAssociation(Extension, Key:string);
begin
  with TRegIniFile.Create('') do
  try
    RootKey := HKEY_CLASSES_ROOT;
    EraseSection('.' + Extension);
    EraseSection(Key);
  finally
    CloseKey;
    Free;
  end;
end;

procedure CreateSendToShortcut;
var
   UK : IUnknown;
   ShLnk : IShellLink;
   PF: IPersistFile;
   Shortcut: string;
   pIDL :pItemIDList;
   Buffer: array[0..MAX_PATH]of char;
   Malloc: IMalloc;
begin
   SHGetSpecialFolderLocation(0, CSIDL_SENDTO, pidl);
   ShGetPathFromIdList(pIDL, PChar(@Buffer));
   Shortcut:= Buffer;
   OLECheck(SHGetMalloc(Malloc));
   if pidl <> nil then Malloc.Free(pIDL);
   Shortcut:=Shortcut+'\'+Application.Title+'.lnk';
   CoInitialize(nil);
   UK := CreateComObject(CLSID_ShellLink);
   ShLnk := UK as IShellLink;
   PF := UK as IPersistFile;
   ShLnk.SetPath(pChar(Application.ExeName));
   PF.Save(PWChar(WideString(Shortcut)), False);
   CoUninitialize;
end;  


// ------------------------------


//  How to set your item directly into the explorer-context:
//  anywhere via buttonclick or anything else...

    with FileAssocInfo do
        begin
           AppPath        := Application.ExeName;
           Extension      := 'PDF';
           Key            := 'Your:tool_name';
           Description    := 'PDF Your_tool_name';
           AddLocalMenu   := True;
           MenuString     := 'working with my pdf-tool!';
           OpenCmd        := '';
           Icon           := '';
        end;
    RegisterFileAssociation(FileAssocInfo);

//  How to remove your item from the explorer-context:
//  anywhere via buttonclick or anything else...

    DeleteFileAssociation('PDF', 'Your_tool_name');

//  How to insert your item directly into the explorer-context:
//  anywhere via buttonclick or anything else...

    CreateSendToShortcut;

//  How to remove your send-to-item from the explorer-context:
//  anywhere via buttonclick or anything else...

    filename := Application.Title+'.lnk';
    SHGetSpecialFolderLocation(Form1.Handle, CSIDL_SENDTO, SFolder);
    SHGetPathFromIDList(SFolder, SpecialPath);
    deletefile(strpas(Specialpath)+'\'+ filename);


Cheers Ingo




Replies:
Posted By: delphiide
Date Posted: 06 Mar 15 at 9:58PM
for more pro, you can use ShellPlus components

-------------
...
Delphi IDE Blog!
http://delphiide.blogspot.com



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk