
上QQ阅读APP看书,第一时间看更新
How to do it...
Let's complete the following steps:
- 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.
- 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).
- We have to handle some features specific to Microsoft Windows, so we need some Windows-related units. Under the implementation section of the unit, write this uses clause:
uses System.Win.registry, Winapi.shlobj, System.IOUtils;
- In the implementation section, we need two procedures to do the real work; so just after the 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;
- These two procedures allow us to register (and unregister) a file type, considering only the current user or all the machine users. Pay attention; if you want to register the association for every user, write your data to:
HKEY_LOCAL_MACHINE\Software\Classes
- If you want to register the association for the current user only, write your data to:
HKEY_CURRENT_USER\Software\Classes
- On the newest Windows versions, you need administrator rights to register a file type for all machine users. The last line of the procedures tells Explorer (the Microsoft Windows graphic interface) to refresh its settings to reflect the changes made to the file associations. As a result, the Explorer file list views will update.
- We've almost finished. Change the left button name to btnRegister, the right button name to btnUnRegister, and put the following code into 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;
- Now, when our application is invoked after double-clicking, we'll get the file name as a parameter. It is possible to read a parameter passed by Windows Explorer (or the 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;
- Now, the application should be complete. However, nice integration with the operating system requires a nice icon. In the code, the associated file will get the same icon as the main program, so let's change our default icon by going to Project | Options | Application dialog, and choosing a nice icon. Click on the Load Icon button, choose an ICO file, and then select the third item from the resultant dialog:

Figure 1.19: Changing the default application icon for our application
- Now, create some text files with our registered extension, .secret.
- These files will appear with the default Windows icons, but in a few seconds, they will have a brand new icon.
- Run the application by hitting F9 (or by going to Run | Run).
- Click on the btnRegister button and close the application. Now, the files get new icons, as shown here:

Figure 1.20: The files in Windows Explorer before and after having registered the SECRET extension
- Now, with the application not running, double-click on a .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 1.21: Our application, launched by the operating system, showing the contents of the file