JumpListTest (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example uses the RegisterFileExtension function to register your executable on the system as a file handler. The RegisterFileExtensionsToUser function works for a list of instances. As a result, you can have Frequent and Recent Lists available for your app.

This is an example of usage of the RegisterFileExtension function:

 procedure TJumpListMain.Button1Click(Sender: TObject);
var
  LInfo: TFileRegisterInformation;
begin
  LInfo.Extension := '.txt';
  LInfo.FileTypeDescription := 'Jumplist text file';
  LInfo.DefIconPath := Application.ExeName;
  LInfo.OpenCommand := Application.ExeName + ' %1';
  Linfo.Exename := ExtractFileName(Application.ExeName);
  if not RegisterFileExtension(LInfo) then
    ShowMessage('Failed');
end;

Code

unit uRegisterFileType;

interface

uses
   System.Win.Registry, System.Generics.Collections;

type
  TFileRegisterInformation = record
   Extension: string;
   FileTypeDescription: string;
   DefIconPath: string;
   OpenCommand: string;
   Exename: string;
end;

function RegisterFileExtensionsToUser(aFileExtList: TList<TFileRegisterInformation>): boolean;
function RegisterFileExtension(FileInfo: TFileRegisterInformation): Boolean;

implementation

uses
  Winapi.ShlObj, Winapi.Windows;

function RegisterFileExtension(FileInfo: TFileRegisterInformation): Boolean;
const 
  SOFT_CLASSES = '\Software\Classes\'; // do not localize
var
  LReg: TRegistry;
  buffer: Pointer;
begin
  Result := False;
  LReg := TRegistry.Create;
  try
    LReg.RootKey := HKEY_CURRENT_USER;
    if LReg.OpenKey(SOFT_CLASSES + FileInfo.Extension + '\OpenWithProgids', true) then
        LReg.WriteBinaryData(FileInfo.Exename, buffer, 0);
    if LReg.OpenKey(SOFT_CLASSES + FileInfo.Exename, true) then
        LReg.WriteString('', FileInfo.FileTypeDescription);
    if LReg.OpenKey(SOFT_CLASSES + FileInfo.Exename + '\DefaultIcon', true) then
        LReg.WriteString('', FileInfo.DefIconPath);
    if LReg.OpenKey(SOFT_CLASSES + FileInfo.Exename +'\shell\open\command', true) then
        LReg.WriteString('', FileInfo.OpenCommand);
    Result := True;
  finally
    LReg.Free;
  end;
  SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);
end;

function RegisterFileExtensionsToUser(aFileExtList: TList<TFileRegisterInformation>): Boolean;
const
  SOFT_CLASSES = '\Software\Classes\'; // do not localize
var
  aFileRegInfo: TFileRegisterInformation;
begin
  Result := False;
  for aFileRegInfo in aFileExtList do
  begin
    Result := RegisterFileExtension(aFileRegInfo);
    if not Result then Break;
  end;
end;

end.

Uses

Personal tools
Assistance