JumpListTest (C++)

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:

 void __fastcall TForm1::Button1Click(TObject *Sender)
{
	   TFileRegisterInformation *LInfo=new TFileRegisterInformation();
	   LInfo->Extension=".txt";
	   LInfo->FileTypeDescription = "PruebaJumplist text file";
	   LInfo->DefIconPath = Application->ExeName;
	   LInfo->OpenCommand = Application->ExeName+"%1";
	   LInfo->Exename = ExtractFileName(Application->ExeName);
	   if(RegisterFileExtension(LInfo)==false) ShowMessage("Failed");
	   delete LInfo;
}

Code

Header file:

//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <System.Win.Registry.hpp>
#include <vector>
//---------------------------------------------------------------------------
struct TFileRegisterInformation{
  UnicodeString Extension;
  UnicodeString FileTypeDescription;
  UnicodeString DefIconPath;
  UnicodeString OpenCommand;
  UnicodeString Exename;
};

bool __fastcall RegisterFileExtension(TFileRegisterInformation *FileInfo);
bool __fastcall RegisterFileExtensionsToUser(std::vector<TFileRegisterInformation*> aFileExtList);

#endif

Implementation file:

//---------------------------------------------------------------------------

#pragma hdrstop

#include "Unit1.h"
#include <Winapi.ShlObj.hpp>
#include <Winapi.Windows.hpp>
using System::Win::Registry;
//---------------------------------------------------------------------------
#pragma package(smart_init)

//---------------------------------------------------------------------------
bool __fastcall RegisterFileExtension(TFileRegisterInformation *FileInfo)
{
	const String SOFT_CLASSES="\\Software\\Classes";
	TRegistry *LReg=new TRegistry();
	Pointer buffer;
	bool Result=false;
	try
	{
		LReg->RootKey = HKEY_CURRENT_USER;
		if (LReg->OpenKey(SOFT_CLASSES+FileInfo->Extension+"\\OpenWithProgids",true))
			LReg->WriteBinaryData(FileInfo->Exename, buffer, 0);
		if (LReg->OpenKey(SOFT_CLASSES+FileInfo->Exename,true))
			LReg->WriteString("", FileInfo->FileTypeDescription);
		if (LReg->OpenKey(SOFT_CLASSES+FileInfo->Exename +"\\DefaultIcon",true))
			LReg->WriteString("", FileInfo->DefIconPath);
	        if (LReg->OpenKey(SOFT_CLASSES + FileInfo->Exename +"\\shell\\open\\command",true))
			LReg->WriteString("", FileInfo->OpenCommand);
		 Result=true;
	}
	catch(...)
	{
		SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);
	}
	return Result;
}

//---------------------------------------------------------------------------
bool __fastcall RegisterFileExtensionsToUser(std::vector<TFileRegisterInformation*> aFileExtList)
{
	const String SOFT_CLASSES="\\Software\\Classes";
	TFileRegisterInformation aFileRegInfo;

	bool Result=false;
	for (std::vector<TFileRegisterInformation*>::iterator it = aFileExtList.begin() ; it != aFileExtList.end(); ++it)
	{
		Result= RegisterFileExtension(*it);
		if (!Result) break;
	}
	return Result;
}