-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Delphi Cookbook - Second Edition
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 Microsoft Word, Microsoft 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 mywordfile.docx, Microsoft Word will be opened and mywordfile.docx 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:
uses System.Win.registry, Winapi.shlobj, System.IOUtils;
uses clause, add 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 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 registred'); 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;

Figure 12.1: Changing the default application icon for our application
.secret.
Figure 12.2: 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 in the file):
Figure 12.3: Our application, launched by the operating system, while it is showing the contents 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 (Firebird SQL Embedded database files or SQLite database files). So, a double-click actually was a connection to that database.
Change the font size
Change margin width
Change background colour