Question:
How do I create a file association for my application?
Answer:
In Win32, create a new registry entry under the
HKEY_CLASSES_ROOT root key that points to the file
extension, the command line to invoke, and the icon to
display. Under Win16, simply file extension and the command
line to invoke in the [Extensions] sections of Win.ini.
Example:
uses
Registry, {For Win32}
IniFiles; {For Win16}
{For Win32}
procedure TForm1.Button1Click(Sender: TObject);
var
reg: TRegistry;
begin
reg := TRegistry.Create;
reg.RootKey := HKEY_CLASSES_ROOT;
reg.LazyWrite := false;
{Add Program Support}
reg.OpenKey('.bor\shell\open\command',
true);
{Invoke the program passing the file name as the first parameter}
reg.WriteString('',
'C:\Program Files\Borland\Delphi 3\Project1.exe %1');
{Add Icon Display}
reg.CloseKey;
reg.OpenKey('.bor\DefaultIcon',
true);
{Use the first icon in the executable to display}
reg.WriteString('',
'C:\Program Files\Borland\Delphi 3\Project1.exe,0');
reg.CloseKey;
reg.free;
end;
{For Win16}
procedure TForm1.Button2Click(Sender: TObject);
var
WinIni : TIniFile;
WinIniFileName : array[0..MAX_PATH] of char;
s : array[0..64] of char;
begin
GetWindowsDirectory(WinIniFileName, sizeof(WinIniFileName));
StrCat(WinIniFileName, '\win.ini');
WinIni := TIniFile.Create(WinIniFileName);
WinIni.WriteString('Extensions',
'bor',
'C:\PROGRA~1\BORLAND\DELPHI~1\PROJECT1.EXE ^.bor');
WinIni.Free;
StrCopy(S, 'Extensions');
SendMessage(HWND_BROADCAST, WM_WININICHANGE,
0, LongInt(@S));
end;