Home arrow Delphi-Kylix arrow Creating an Associator
DELPHI-KYLIX

Creating an Associator


Have you ever wondered how installation programs are able to associate a file extension with a program? Or how when you double click on a MS Word file, Microsoft Word automatically starts up and loads that particular file? Well, in this article I will explain how it is done.

Author Info:
By: Jacques Noah
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
August 09, 2006
TABLE OF CONTENTS:
  1. · Creating an Associator
  2. · Continuing with the code
  3. · How to enable the application to load the file that has been clicked
  4. · The entire code

print this article
SEARCH DEVARTICLES

Creating an Associator
(Page 1 of 4 )

What you need

You need a copy of Borland Delphi, any version. Although not really necessary, it might be beneficial to you to have a look at the http://delphi.about.com web site for a introductory tutorial on how to interact with the registry.

Code

Let's build the UI. Start a new application and add two TEdits, two buttons and two labels. Add "Associate" and "Exit" as the button captions, and rename them "btnAssoc" and "btnClose" respectively. Add another button and call it "btnbrowse."  Rename the edits to "edappname" and "edext." Then add a TOpendialog from the Dialog Tab and rename it to "op." Arrange them on the form to your liking.

You should now have something like this:

Double click on the "Exit" button and add the following code:

procedure TForm1.btnCloseClick(Sender: TObject);
  begin
    close;
  end;

The code above is self explanatory; it closes the application.

Double click on the browse button and add the following code:

procedure TForm1.btnbrowseClick(Sender: TObject);
  begin
    if op.Execute then begin
      edappname.Text:=op.FileName;
    end
  end;

The code above enables you to browse for the application name on your local disk.

Double click on the "Associate" button and add the following code:

procedure TForm1.btnAssocClick(Sender: TObject);
  var
  reg:tregistry; //registry object
  error:boolean;
  begin
    if (edappname.text = '') OR (edext.text = '') then begin
      MessageDlg('Please ensure that you''ve filled in ALL the fields.', mtError,      [mbOk], 0);
      exit;
    end;
  reg := TRegistry.Create;
  reg.RootKey := HKEY_CLASSES_ROOT;
  reg.LazyWrite := false;
  try
    if
      {Add Program Support}
      reg.OpenKey('.'+edExt.Text+'shellopencommand', true) then begin
      {Invoke the program passing the file name as the first parameter}
        reg.WriteString('', edappname.Text+' %1 ');
        reg.CloseKey;
        error:=false;
        end
    else begin
      error:=true;
      end;
    if not error then begin
      reg.OpenKey('.'+edExt.Text+'DefaultIcon', true);
      {Use the first icon in the executable to display}
      reg.WriteString('', edappname.Text+',0');
      reg.CloseKey;
      end;
    if error then begin
       MessageDlg('There''s been an error creating this association, please check that you''ve entered the information correctly.', mtInformation  ,[mbOk], 0);
       exit;
       end

      else begin

 if MessageDlg(extractfilename(edappname.Text)+ ' have now been associated ' +#13#10+ 'with ' +'extention - '+ '.'+edExt.Text+'!'#13#10+ ' Would you like to make another Association?', mtInformation        , [mbYes, mbNo], 0) = mrYes then

  begin

    edext.Clear;

    edappname.Clear;

   end

   else begin

   close;

   end;

   end;

      //************

finally

reg.Free;

end;

close;

end;

The first thing that happens is a check to see whether the edappname and edext TEdit fields are empty or not. This is very important since you are going to use these parameters to make registry entries.


blog comments powered by Disqus
DELPHI-KYLIX ARTICLES

- Loading an XML Document into the DOM
- Delphi Wrapper Classes and XML
- Delphi and the DOM
- Delphi and XML
- Internet Access: Client Service
- Finishing the Client for an Internet Access ...
- The Client for an Internet Access Control Ap...
- User Management for an Internet Access Contr...
- Important Procedures for an Internet Access ...
- Server Code for an Internet Access Control A...
- Constructing the Interface for an Internet A...
- Building a Server Application for an Interne...
- Building an Internet Access Control Applicat...
- Client Dataset: Working with Data Packets an...
- Using the Client Dataset in an N-Tiered Appl...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials