Page 1 of 1

Can I set the icon of the document?

Posted: Fri Feb 21, 2014 10:10 am
by starhu
Hello,

My customer wants his file icon - which can be seen in windows explorer.
Can it be done?

Thank you

Posted: Fri Feb 21, 2014 1:46 pm
by Ruediger Kabbasch
Just to give you an idea (Not sure if the code snipped from one of my libraries is complete):

First you have to add that icon to you application (or a dll, or other exe).

Than you have to register the file extension with the icon index of this icon.

{==============================================================================}
{ Checks if the given file extension is already associated to an application }
{==============================================================================}
{ Parameter Type Description }
{------------------------------------------------------------------------------}
{ fileExt String The file extension to check }
{==============================================================================}
{ Returns: Boolean, True if already associated }
{=[07.08.2004 (RK)]============================================================}
Function FileExtIsAssociated(fileExt : String) : Boolean;
begin
Result := False;

IF (Length(fileExt) > 0) Then
begin
IF (fileExt[1] <> '.') Then fileExt := Concat('.', fileExt);
Result := RegKeyExists(fileExt, HKEY_CLASSES_ROOT);
end; {<- IF (Length(fileExt) > 0) Then }

end;



{==============================================================================}
{ Determine the application to which the file extension is associated to. }
{==============================================================================}
{ Parameter Type Description }
{------------------------------------------------------------------------------}
{ fileExt String The file extension to check }
{==============================================================================}
{ Returns: String, the application name or '' on any error. }
{=[07.08.2004 (RK)]============================================================}
Function FileExtIsAssociatedTo(const fileExt : String) : String;
var
explorerDesc : String; // Helpvar
regName : String; // Helpvar.
begin
Result := FileExtIsAssociatedTo(fileExt, explorerDesc, regName);
end;



{==============================================================================}
{ Determine the application to which the file extension is associated to. }
{==============================================================================}
{ Parameter Type Description }
{------------------------------------------------------------------------------}
{ fileExt String The file extension to check }
{ explorerDesc String IN: N/A }
{ OUT: The description for the windows explorer}
{ regExtName String IN: N/A }
{ OUT: The name in the registry. }
{==============================================================================}
{ Returns: String, the application name or '' on any error. }
{=[07.08.2004 (RK)]============================================================}
Function FileExtIsAssociatedTo(fileExt : String; var explorerDesc, regExtName : String) : String;
var
reg : TRegistry; // the registry object
regName : String; // The "registry name" of the file type (e.g. Word.Document.8)
iPos : Integer; // Helvpar.
begin

Result := '';
regExtName := '';
explorerDesc := '';

IF (Length(fileExt) > 0) Then
begin
IF (fileExt[1] <> '.') Then fileExt := Concat('.', fileExt);
IF FileExtIsAssociated(fileExt) Then
begin
reg := TRegistry.Create;
Try {[01]}
reg.RootKey := HKEY_CLASSES_ROOT;
IF reg.OpenKey(fileExt, False) Then
begin
regName := reg.ReadString('');
reg.CloseKey;
regExtName := regName;

IF (Length(regName) > 0) Then
begin
IF reg.OpenKey(regName, False) Then
begin
explorerDesc := reg.ReadString('');
reg.CloseKey;
end; {<- IF reg.OpenKey(regName, False) Then }

regName := Concat(regName, REGPATH_DELIMITER, key_ShellOpenCommand);

IF reg.OpenKey(regName, False) Then
begin
Result := reg.ReadString('');
end; {<- IF reg.OpenKey(Concat(regName, REG_PATH_DELIMITER, key_FileShellOpenCommand), False) Then }
end; {<- IF (Length(regName) > 0) Then }
end; {<- IF reg.OpenKey(fileExt, False) Then }
finally {[01]}
reg.Free;
end; {[01]}
end; {<- IF FileExtIsAssociated(fileExt) Then }
end; {<- IF (Length(fileExt) > 0) Then }

{----------------------------}
{ Try to reduce to file name }
{----------------------------}
IF (Length(Result) > 0) Then
begin
IF (Result[1] = '"') Then
begin
Delete(Result, 1, 1);
iPos := AnsiPos('"', Result);
IF (iPos > 0) Then Result := Copy(Result, 1, (iPos - 1));
end
else begin
iPos := AnsiPos('.exe', AnsiLowerCase(Result));
IF (iPos > 0) Then Result := Copy(Result, 1, (iPos + 3));
end; {<- IF (Result[1] = '"') Then }
end; {<- IF (Length(Result) > 0) Then }

end;



{==============================================================================}
{ Remove the nodes of an existing file type association }
{==============================================================================}
{ Parameter Type Description }
{------------------------------------------------------------------------------}
{ extension String The file extension to "unregister" }
{==============================================================================}
{ Returns: Boolean, True if succesfully removed }
{=[08.08.2004 (RK)]============================================================}
Function RemoveRegisteredFileType(fileExt : String) : Boolean;
var
reg : TRegistry; // the registry object
regName : String; // The "registry name" of the file type (e.g. Word.Document.8)
begin

Result := False;

IF (Length(fileExt) > 0) Then
begin
IF (fileExt[1] <> '.') Then fileExt := Concat('.', fileExt);
IF FileExtIsAssociated(fileExt) Then
begin
reg := TRegistry.Create;
Try {[01]}
reg.RootKey := HKEY_CLASSES_ROOT;
IF reg.OpenKey(fileExt, False) Then
begin
regName := reg.ReadString('');
reg.CloseKey;

IF (Length(regName) > 0) Then
begin
IF reg.DeleteKey(regName) Then
begin
Result := reg.DeleteKey(fileExt);
end; {<- IF reg.DeleteKey(regName) Then }
end
else begin
{ Just delete the "main" node }
Result := reg.DeleteKey(fileExt);
end; {<- IF (Length(regName) > 0) Then }
end; {<- IF reg.OpenKey(fileExt, False) Then }
finally {[01]}
reg.Free;
end; {[01]}
end
else begin
{ no association exists }
Result := True;
end; {<- IF FileExtIsAssociated(fileExt) Then }
end; {<- IF (Length(fileExt) > 0) Then }
end;


{==============================================================================}
{ Create the registry entries that are necessary to associate an extension }
{ to a specific application. }
{ If the key is already associated to an application the existing keys will }
{ be deleted before
{==============================================================================}
{ Parameter Type Description }
{------------------------------------------------------------------------------}
{ extension String The file extension. }
{ applName String The assoicated application }
{ explorerDesc String The description for the explorer }
{ regName String The name for the registry }
{ iconIndex Integer The icon index }
{ allowOverwrite Boolean If True an existing registry association }
{ will be deleted. }
{==============================================================================}
{ Returns: Boolean, True if succesfully created. }
{=[08.08.2004 (RK)]============================================================}
Function RegisterFileType(fileExt : String; const applName : String; explorerDesc : String = ''; regName : String = ''; iconIndex : Integer = 0; allowOverwrite : Boolean = True) : Boolean;
var
isOK : Boolean; // Helpvar
reg : TRegistry; // The registry object
begin
Result := False;

IF (Length(fileExt) > 0) Then
begin
IF (fileExt[1] <> '.') Then fileExt := AnsiLowerCase(Concat('.', fileExt));

IF FileExtIsAssociated(fileExt) Then
begin
IF allowOverwrite Then
begin
isOK := RemoveRegisteredFileType(fileExt);
end
else begin
isOK := False;
end; {<- IF allowOverwrite Then }
end
else begin
isOK := True;
end; {<- IF FileExtIsAssociated(fileExt) Then }


IF isOK Then
begin
IF (Length(explorerDesc) = 0) Then
begin
explorerDesc := Concat(AnsiUpperCase(Copy(fileExt, 2, (Length(fileExt) - 1))), '-', sFile);
end; {<- IF (Length(explorerDesc) = 0) Then }
IF (Length(regName) = 0) Then
begin
regName := Concat(Copy(fileExt, 2, (Length(fileExt) - 1)), '.', sFile);
end; {<- IF (Length(explorerDesc) = 0) Then }

{------------------------}
{ Create the new entries }
{------------------------}
reg := TRegistry.Create;
Try {[01]}
reg.RootKey := HKEY_CLASSES_ROOT;

IF reg.OpenKey(fileExt, True) Then
begin
try {[02]}
reg.WriteString('', regName);
finally {[02]}
reg.CloseKey;
end; {[02]}
end; {<- IF reg.OpenKey(fileExt, True) Then }

IF reg.CreateKey(regName) Then
begin

IF reg.OpenKey(regName, True) Then
begin
try {[02]}
reg.WriteString('', explorerDesc);
finally {[02]}
reg.CloseKey;
end; {[02]}
end; {<- IF reg.OpenKey(regName) Then }

IF reg.OpenKey(Concat(regName, REGPATH_DELIMITER, key_DefaultIcon), True) Then
begin
try {[02]}
reg.WriteString('', Concat(applName, ',', IntToStr(iconIndex)));
finally {[02]}
reg.CloseKey;
end; {[02]}
end; {<- IF reg.OpenKey(Concat(regName, REGPATH_DELIMITER, key_DefaultIcon), True) Then }

IF reg.OpenKey(Concat(regName, REGPATH_DELIMITER, key_ShellOpenCommand), True) Then
begin
try {[02]}
reg.WriteString('', Concat(applName, ' ', val_part_FileNamePlaceHolder));
finally {[02]}
reg.CloseKey;
end; {[02]}
end; {<- IF reg.OpenKey(Concat(regName, REGPATH_DELIMITER, key_ShellOpenCommand), True) Then }
end; {<- IF reg.CreateKey(regName) Then }

finally {[01]}
reg.Free;
end; {[01]}

Result := FileExtIsAssociated(fileExt);
end; {<- IF isOK Then }

SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NIL, NIL);

end; {<- IF (Length(fileExt) > 0) Then }
end;

Posted: Sun Feb 23, 2014 11:15 am
by starhu
Thank you very much! I'll try it.