Conditionally change application icon with Delphi 2006
Asked Answered
W

1

8

I want to produce two different versions of my application, with different icons based on a DEFINE. I succeeded to include different resource files based on the define but I cannot get rid of the MAINICON in the default .res file of my project.

I added a resource Version1.rc 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 C:\MyProject\Version1.RES resource kept; file C:\MyProject\MyProject.RES resource discarded.

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

Wengert answered 17/8, 2015 at 9:51 Comment(6)
Don't let the IDE managed MyProject.res. Have your build script create it. Of course, I routinely suggest that here and every time I do, multiple people disagree with me. It seems to be just me that likes to have a repeatable build process that does not rely on the IDE.......Cordwainer
@DavidHeffernan At the moment I run a script compile_resources.bat and then I click Build All in my project group in the IDE. Delphi however seems to overwrite the MyProject.res in this process. I understand the value of having a script to compile everything, but I think this would be quite some work. On the other hand I would also like to be able to set a define in the IDE and have the right exe being generated.Wengert
You just need to take over from the IDE. Remove the {$R *.res} line from the .dpr file, and you are good to go. On the other hand, if you'd rather stick to building in the IDE, then you are going to struggle with such tasks. If that's what you would prefer to do, then that's up to you obviously.Cordwainer
@DavidHeffernan You are right: if I remove {$R *.res}, the error is gone and I have the right icon. But version information, application title, language info etc. are gone as well. Do you have a link or an example rc-file that I could use to generate my custom MyProject.res?Wengert
Just look at the .res file that the IDE produces in a res file editor, and refer to the MSDN docs to decode it.Cordwainer
@DavidHeffernan thanks, I'll have a look. If you post your comments as an answer, I'll accept.Wengert
W
5

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

  • In the IDE I removed "Include version info" under project options and removed {$R *.res} in the project file.
  • I removed the file MyProject.res
  • I added a file versioninfo.rc with the information, that I formerly provided in the IDE, as described here.
  • I added the files icon_version1.rc and icon_version2.rc, that looked like this:

    LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
    MAINICON icon "..\resources\icons\version1.ico"
    
  • I added a file Resources.pas to my project, that 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 the additional resource files.

Now if I define VERSION1, my application has the icon icon_version1.ico, otherwise icon_version2.ico. One caveat: If version info (or icon) is modified I have to run compile_resources.bat to reflect the changes.

Wengert answered 17/8, 2015 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.