Setting default document icon with CMake for windows

Hello,

To create an installer for my juce application I am using CPack with NSIS. My application generate .myExt files to save its data. And I want .myExt files to have a fancy icon.

To do so, for what I understand, I need to do this with CPack :

    set(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "
		WriteRegStr HKCR '.myExt' '' 'myfile'
		WriteRegStr HKCR 'myfile' '' 'MyApp Document'
		WriteRegStr HKCR 'myfile\\\\DefaultIcon' '' '$INSTDIR\\\\bin\\\\${EXECUTABLE_NAME}.exe,0'
		WriteRegStr HKCR 'myfile\\\\shell\\\\open\\\\command' '' '$INSTDIR\\\\bin\\\\${EXECUTABLE_NAME}.exe %1'
		WriteRegStr HKCR 'myfile\\\\shell\\\\print\\\\command' '' '$INSTDIR\\\\bin\\\\${EXECUTABLE_NAME}.exe /p %1'
		WriteRegStr HKLM 'Software\\\\RegisteredApplications' '${EXECUTABLE_NAME}' '$INSTDIR\\\\bin\\\\${EXECUTABLE_NAME}.exe'
		WriteRegStr HKCU 'Software\\\\RegisteredApplications' '${EXECUTABLE_NAME}' '$INSTDIR\\\\bin\\\\${EXECUTABLE_NAME}.exe'
		WriteRegStr HKCU 'Software\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Explorer\\\\FileExts\\\\.myExt\\\\OpenWithList' 'a' '$INSTDIR\\\\bin\\\\${EXECUTABLE_NAME}.exe'
	") 

The important line is :

WriteRegStr HKCR 'myfile\\\\DefaultIcon' '' '$INSTDIR\\\\bin\\\\${EXECUTABLE_NAME}.exe,0'

The index at the end of the file is the one of the icon I want to use. It is specified in the .rc files of the application.

Here is my problem, the cmake api of Juce already generate and link a rc file. I cannot generate and use my custom .rc file. If I use the index 0 or 1, it will use the application icon ICON_BIG or ICON_SMALL specify in :

juce_add_gui_app(${EXECUTABLE_NAME}
  ICON_BIG "${PLACEHOLDER_DIR}/icon_big.png"                      # ICON_* arguments specify a path to an image file to use as an icon
  ICON_SMALL "${PLACEHOLDER_DIR}/icon_small.png"
)

Is there a way to specify a third icon in the juce rc file ?
Should I use another method to link a fancy icon to my .myExt files ? Which one ?

Thank you for your help.

Resource files generated by the Projucer and CMake use the preprocessor like so:

#pragma code_page(65001)

#ifdef JUCE_USER_DEFINED_RC_FILE
 #include JUCE_USER_DEFINED_RC_FILE
#else

// predefined data goes here

#endif

IDI_ICON1 ICON DISCARDABLE "icon.ico"
IDI_ICON2 ICON DISCARDABLE "icon.ico"

This means you can write your own resources.rc and add something like JUCE_USER_DEFINED_RC_FILE=${CMAKE_CURRENT_SOURCE_DIR}/path/to/resources.rc to your target_compile_definitions call. The generated resources.rc will then include your custom file.

1 Like

That completely solve my problem.
Thank you @reuk !

@reuk - is it possible for you to explain how you would use this for a custom resources.rc file in a Projucer Project for VS 2019? I’m trying to understand how you might do this, and not coming up with a lot. A lot of old posts seem to talk about it “not working” or “being a bug”.

Where do I put my custom resources.rc file? Do I include it into the Projucer sources? Or it just sits in a folder in my sources, and I provide a path to it?

Where do I define JUCE_USER_DEFINED_RC_FILE? This is defined as the path to the file? And what field/box do I put this info in in the Projucer?

Sorry if this may be obvious, I’m confused.

I think this is not currently supported in the Projucer. It might not be too difficult to enable, I’ll take a look.

1 Like

I’ve updated the Projucer on develop so that this should work now:

  • Create a custom resources file in your Source directory
  • Add JUCE_USER_DEFINED_RC_FILE="/path/to/file" to the custom preprocessor definitions for your project

Depending on the path you use, you may also need to add an entry to the additional header search paths, so that the include can be found.

1 Like

Thanks @reuk! It’s working great.