IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

May Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...Using Data   Print This     
  From the September 2009 Issue of Prestwood eMag
 
Delphi Using Data:
Delphi Components 101
 
Posted 17 years ago on 12/26/2002 and updated 12/12/2006
Take Away: Be careful who you associate with!

KB100088



Windows file association allows you to specify what application recognizes a particular extension. For example, launching a file with a .DOC extension will start Word and load the document.

In Windows 95/98/NT, these associations are stored in the registry under the key HKEY_CLASSES_ROOT. There are two parts to the association, first a key is created whose name is the extension (i.e. .doc). The other part is a key that the extension points to, which contains all the information about the icon to use and the command to execute when the file is launched.

For example, the .TXT extension is associated with NOTEPAD.EXE when Windows is first installed. To accomplish this, there is a key .txt, which has a description of txtfile. There is also a key called txtfile. Under this key there are two sub-keys of importance; DefaultIcon and Shell. DefaultIcon tells Windows what icon to use when the file is represented on the

desktop or in explorer. The default string should be the path and filename of the .EXE or.DLL that contains the icon, followed by a comma and the index of the icon in the file (i.e. C:\Windows\Notepad.exe,0).

The sub-keys under Shell identify what commands to add to the right-click menu for the file-type. The most common is Open, but there can be others like Print (these are defined by the application). Under Shell is a key Open and under Open is a key Command. Command is where the actual command is stored. The should contain the command line to execute (i.e. C:\Windows\Notepad.exe %1).

How can the component help
However, messing with one's registry is not a good thing to do, especially if you are unfamiliar with it. If you would like to add file association capabilities to your application, then TLRAssociate is right up your alley. This component provides facilities for creating associations with the application that contains it.

To use it, simply drop the component onto a form and set it's properties. Then call Associate and pass it an extension, the extension will be associated. It also has methods for unassociating extensions, registering and unregistering your application, and getting a list of extensions associated with your application.

When you associate an extension, if it has a previous association this will be stored in the registry. Then if you unassociate it, the old association will be restored.

The properties available and their purpose are as follows:

  • KeyName - the name of the application. This name will be used to create the key in the registry when registering the application.
  • PathToApp - the path and filename of the executable to launch for this association.
  • IconPath - the path and filename of the file that contains the icon to use.
  • Icon - the index of the icon in the file.
  • ShellCmd - the command to add to the right-click menu (should be Open).

 

Once the properties have been specified, calling RegisterApp creates an entry in the registry for the application. To register an extension, call Associate and pass in the extension to associate. Enumerate returns a TStringList, which contains all the extensions, associated with the application.

Following is an example of how to set up the component and associate the file type .LOG with it:

procedure TForm1.FormCreate(Sender:

          TObject);

begin

  LRAssociate1.KeyName   := 

                        Application.Title;

  LRAssociate1.PathToApp := 

                      Application.ExeName;

  LRAssociate1.IconPath  := ParamStr(0);

  LRAssociate1.Icon      := 0;

  LRAssociate1.ShellCmd  := 'Open';

  LRAssociate1.Shell     := 

           Application.ExeName + ' "%1"';

  LRAssociate1.TypeName  := 

         Application.Title + ' document';

  LRAssociate1.New       := False;

  LRAssociate1.Quick     := True;



  LRAssociate1.RegisterApp;

  LRAssociate1.Associate('.LOG');

end;

Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1257A1
Enter key:
Article Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #100088 Counter
6740
Since 4/2/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2019 Prestwood IT Solutions.   [Security & Privacy]