Conditionally change application icon with Delphi 2006

I want to create two different versions of my application with different icons based on DEFINE. I managed to include various resource files based on the definition, but I can not get rid of the MAINICON in the default .res file of my project.

I added a Version1.rc resource file with the following line:

 MAINICON icon "resource\icons\Version1.ico" 

But if I try to compile MyProject using the IDE, I always get the following error:

E2161 Duplicate resource: Type 14 (ICON GROUP), ID MAINICON; file Resource C: \ MyProject \ Version1.RES saved; Resource file C: \ MyProject \ MyProject.RES is dropped.

I tried to edit MyProject.res using the resource editor and deleted MAINICON, but delphi automatically seems to recreate the file, including the icon.

+6
source share
1 answer

I found a solution thanks to David Heffernan's comments. I ended up with the following:

  • In the IDE, I removed the "Include Version Information" in the project settings and deleted the {$R *.res} in the project file.
  • I deleted the file MyProject.res
  • I added a versioninfo.rc file with the information that I previously provided in the IDE, as described here .
  • I added the icon_version1.rc and icon_version2.rc , which looked like this:

     LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL MAINICON icon "..\resources\icons\version1.ico" 
  • I added the Resources.pas file to my project, which looked like this:

     unit Resources; interface implementation {$IFDEF VERSION1} {$R ICON_VERSION1.RES} //from ICON_VERSION1.RC {$ELSE} {$R ICON_VERSION2.RES} //from ICON_VERSION2.RC {$ENDIF} {$R VERSIONINFO.RES} //from VERSIONINFO.RC end. 
  • I modified my existing script compile_resources.bat to compile additional resource files.

Now, if I define VERSION1 , my application has the icon icon_version1.ico , otherwise icon_version2.ico . One warning: if the version information (or icon) is changed, I need to run compile_resources.bat to reflect the changes.

+4
source

All Articles