-
Book Overview & Buying
-
Table Of Contents
Delphi Cookbook
By :
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.
The hard work is done by the operating system itself. We have to instruct Windows to provide the following information:
Let's start!
implementation section of the unit, write this use clause:uses System.Win.registry, Winapi.shlobj, System.IOUtils;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;
HKEY_LOCAL_MACHINE\Software\Classes
HKEY_CURRENT_USER\Software\Classes
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;
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;

Changing the default application icon for our application
.secret extension.
The files in Windows Explorer before and after having registered the .secret extension
.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):
Our application, launched by the operating system, while showing the content of the file
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.
Change the font size
Change margin width
Change background colour