Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Delphi Cookbook

You're reading from   Delphi Cookbook 50 hands-on recipes to master the power of Delphi for cross-platform and mobile development on Windows, Mac OS X, Android, and iOS

Arrow left icon
Product type Paperback
Published in Sep 2014
Publisher
ISBN-13 9781783559589
Length 328 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Daniele Teti Daniele Teti
Author Profile Icon Daniele Teti
Daniele Teti
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Delphi Basics FREE CHAPTER 2. Become a Delphi Language Ninja 3. Going Cross Platform with FireMonkey 4. The Thousand Faces of Multithreading 5. Putting Delphi on the Server 6. Riding the Mobile Revolution with FireMonkey 7. Using Specific Platform Features Index

Associating a file extension with your application on Windows

In some cases, your fantastic application needs to be opened with just a double-click on a file with an extension associated with it. This is the case with MS Word, MS Excel, and many other well-known pieces of software. If you have a file generated with a program, double-click on the file and the program that generated the file will bring up pointing to that file. So, if you click on a mywordfile.docx file, MS Word will be opened and the mywordfile.docx file's content will be shown. This is what we'd like to do in this recipe. The association can be useful also when you have multiple configurations for a program. Double-click on the ConfigurationXYZ.myext file and the program will start using that configuration.

Getting ready

The hard work is done by the operating system itself. We have to instruct Windows to provide the following information:

  • The file extension to associate
  • The description of file type (this will be shown by Windows Explorer describing the file type)
  • The default icon for the file type (in this recipe, we'll use the application icon itself, but it is not mandatory)
  • The application that we want to associate

Let's start!

How to do it…

  1. Create a new VCL application and drop two TButton components and a TMemo component. Align all the buttons as a toolbar at the top of the form and the memo to all the remaining form client area.
  2. The button on the left-hand side will be used to register a file type while the button on the right-hand side will be used to unregister the association (cleaning the registry).
  3. We have to handle some MS Windows-specific features, so we need some Windows-related units. Under the implementation section of the unit, write this use clause:
    uses System.Win.registry, Winapi.shlobj, System.IOUtils;
  4. In the implementation section, we need two procedures to do the real work; so just after the uses clause, put this code:
    procedure UnregisterFileType(
       FileExt: String; 
       OnlyForCurrentUser: boolean = true);
    var
      R: TRegistry;
    begin
      R := TRegistry.Create;
      try
        if OnlyForCurrentUser then
          R.RootKey := HKEY_CURRENT_USER
        else
          R.RootKey := HKEY_LOCAL_MACHINE;
    
        R.DeleteKey('\Software\Classes\.' + FileExt);
        R.DeleteKey('\Software\Classes\' + FileExt + 'File');
      finally
        R.Free;
      end;
      SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
    end;
    
    procedure RegisterFileType(
       FileExt: String; 
       FileTypeDescription: String;
       ICONResourceFileFullPath: String;
       ApplicationFullPath: String;
       OnlyForCurrentUser: boolean = true);
    var
      R: TRegistry;
    begin
      R := TRegistry.Create;
      try
        if OnlyForCurrentUser then
          R.RootKey := HKEY_CURRENT_USER
        else
          R.RootKey := HKEY_LOCAL_MACHINE;
    
        if R.OpenKey('\Software\Classes\.' + FileExt, 
             true) then begin
          R.WriteString('', FileExt + 'File');
          if R.OpenKey('\Software\Classes\' + FileExt + 'File', 
                   true) then begin
            R.WriteString('', FileTypeDescription);
            if R.OpenKey('\Software\Classes\' + 
             FileExt + 'File\DefaultIcon', true) then
            begin
              R.WriteString('', ICONResourceFileFullPath);
              if R.OpenKey('\Software\Classes\' + 
                   FileExt + 'File\shell\open\command', 
                    true) then
                R.WriteString('', 
                   ApplicationFullPath + ' "%1"');
            end;
          end;
        end;
      finally
        R.Free;
      end;
      SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
    end;
    
  5. These two procedures allows us to register (and unregister) a file type considering only the current user or all the machine users. Note that if you want to register the association for every user, write your data to the following location:
    HKEY_LOCAL_MACHINE\Software\Classes
  6. If you want to register the association for the current user only, write your data to the following location:
    HKEY_CURRENT_USER\Software\Classes
  7. On the newest Windows versions, you need admin rights to register a file type for all the machine users. The last line of the procedures tells Explorer (the MS Windows graphic interface) to refresh its setting to reflect the changes made to the file associations. As a result, for instance, the Explorer file list views will be updated.
  8. We've almost finished. Change the left-hand side button name to btnRegister, the right-hand side button name to btnUnRegister, and put the following code on their onclick event handlers:
    procedure TMainForm.btnRegisterClick(Sender: TObject);
    begin
      RegisterFileType(
        'secret',
        'This file is a secret',
        Application.ExeName,
        Application.ExeName,
        true);
      ShowMessage('File type registered');
    end;
    
    procedure TMainForm.btnUnRegisterClick(Sender: TObject);
    begin
      UnregisterFileType('secret', true);
      ShowMessage('File type unregistered');
    end;
    
  9. Now, when our application is invoked with a double-click, we'll get the file name as a parameter. It is possible to read a parameter passed by Windows Explorer (or command line) using the ParamStr(1) function. Create a FormCreate event handler using the following code:
    procedure TMainForm.FormCreate(Sender: TObject);
    begin
      if TFile.Exists(ParamStr(1)) then
        Memo1.Lines.LoadFromFile(ParamStr(1))
      else
      begin
        Memo1.Lines.Text := 'No valid secret file type';
      end;
    end;
    
  10. Now the application should be complete. However, a nice integration with the operating system requires a nice icon as well. In the code, the associated file will get the same icon as the main program, so let's change our default icon by navigating to Project | Options | Application and choose a nice icon. Click on the Load Icon button, choose an ICO file, and then select the third item from the resultant dialog:
    How to do it…

    Changing the default application icon for our application

  11. Now, create some text files with our registered .secret extension.
  12. These files will appear with the default Windows icon, but in some seconds, they will have a brand new icon.
  13. Run the application by hitting F9 (or navigate to Run | Run).
  14. Click on the btnRegister button and close the application. Now the files get a new icon, as shown in the following screenshot:
    How to do it…

    The files in Windows Explorer before and after having registered the .secret extension

  15. Now, with the application not running, double-click on the .secret file. Our program will be started by Windows itself, using the information stored in the registry about the .secret file, and we'll get this form (the text shown in the memo is the text contained into the file):
    How to do it…

    Our application, launched by the operating system, while showing the content of the file

There's more...

One application can register many file types. In some cases, I've used this technique to register some specific desktop database files to my application (FirebirdSQL Embedded database files or SQLite database files). So a double-click on such database file (registered with an application-specific extension) was actually a connection to that database.

You have been reading a chapter from
Delphi Cookbook
Published in: Sep 2014
Publisher:
ISBN-13: 9781783559589
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime