uses
System.Win.Registry,
Winapi.ShellApi;
const
NAMED_DESTINATION_PREFIX = '
/A "nameddest=';
REG_SHELL_OPEN_COMMAND = '
\Shell\Open\Command';
COMMAND_OPEN = '
open';
function GetAssociation(
const DocFileName:
string):
string;
var
FileExtension :
string;
FileClass:
string;
Reg: TRegistry;
begin
Result := '
';
Reg := TRegistry.Create(KEY_EXECUTE);
try
Reg.RootKey := HKEY_CLASSES_ROOT;
FileClass := '
';
FileExtension := ExtractFileExt(DocFileName);
if Reg.OpenKeyReadOnly(FileExtension)
then
begin
FileClass := Reg.ReadString('
');
Reg.CloseKey;
end;
if FileClass <> '
'
then
begin
if Reg.OpenKeyReadOnly(FileClass + REG_SHELL_OPEN_COMMAND)
then
begin
Result := Reg.ReadString('
');
Reg.CloseKey;
end;
end;
finally
Reg.Free;
end;
end;
procedure DoOpenPDF(
const PDFReader, FileName, NamedDestination:
string);
var
ExecuteResult: HINST;
Parameters:
string;
begin
Parameters := NAMED_DESTINATION_PREFIX + NamedDestination + '
" "' + FileName + '
"';
ExecuteResult := ShellExecute(
Application.Handle,
COMMAND_OPEN,
PChar(PDFReader),
PChar(Parameters),
nil,
SW_NORMAL
);
if ExecuteResult < 32
then
RaiseLastOSError;
end;
procedure OpenPDF(
const FileName:
string;
const NamedDestination:
string = '
');
var
PDFReader:
string;
begin
PDFReader := GetAssociation('
.pdf');
// 'FoxitReader.exe'; //'AcroRd32.exe';
PDFReader := ExtractFileName(PDFReader);
PDFReader := PDFReader.Replace('
" "%1"', '
', [rfReplaceAll, rfIgnoreCase]);
DoOpenPDF(PDFReader, FileName, NamedDestination);
end;