The program at hand needs to modify the registry so that the current program becomes the default open program in a format and associates the icon; Vista requires administrator privileges to operate the registry, which is cumbersome, so try the following.
Unit Unit1; Interface uses Winapi.windows, Winapi.messages, System.sysutils, System.variants, system.classes, Vcl.graphics, Vcl.
Controls, Vcl.forms, Vcl.dialogs, Vcl.stdctrls;
Type TFORM1 = Class (Tform) Memo1:tmemo;
Button1:tbutton;
Procedure Formcreate (Sender:tobject);
Procedure Button1Click (Sender:tobject);
Private {Private declarations} public {public declarations} end;
var Form1:tform1; Implementation {$R *.DFM} uses System.ioutils, Winapi.shellapi, winapi.shlobj;
RegApp function to use the function of the associated default program procedure RegApp (const Aext, aappname, aapppath:string); Const NREGFMT = ' Windows Registry Editor Version 5.00 ' + slinebreak + ' [hkey_classes_root\%0:s] ' + slinebreak + ' @= '%1:s ' + slinebreak + ' [hkey_ Classes_root\%1:s\defaulticon] ' + slinebreak + ' @= '%2:s,0 ' + slinebre AK + ' [Hkey_classes_root\%1:s\shell] ' + slinebreak + ' [hkey_classes_root\%1:s\shell\open] ' + sLi
Nebreak + ' [Hkey_classes_root\%1:s\shell\open\command] ' + slinebreak + ' @= '%3:s ';
var regstr:string;
STR3, str4:string;
regtmpfile:string;
Begin STR3: = Aapppath.replace (' \ ', ' \ \ '); STR4: = Format ('%s ' '%%1 ', [STR3]).
Replace (' "', '; ');
REGSTR: = Format (NREGFMT, [Aext, Aappname, STR3, STR4]);
Regtmpfile: = Tpath.gettemppath + ' Regtmp.reg ';
With Tstringlist.create do begin Text: = REGSTR;
SaveToFile (Regtmpfile);
Free;
End
ShellExecute (0, Nil, Pchar (regtmpfile), nil, nil, SW_SHOWNORMAL); SHChangeNotify (shcne_assocchanged, shcnf_idlist, nil, nil);
Refresh icon display end;
Procedure Tform1.formcreate (Sender:tobject);
var s:string;
Begin S: = PARAMSTR (1);
If FileExists (s) then Memo1.Lines.LoadFromFile (s);
End Execute the registration function;
After execution, you can arbitrarily modify the suffix of a text file to TST, and then double-click the test procedure Tform1.button1click (sender:tobject);
Begin RegApp ('. TST ', ' MyApp1 ', application.exename);
Assuming the program name is MYAPP1, the suffix of the file to be opened is. TST End; End.