简体   繁体   中英

How to set an icon for C# Windows Forms application via cmake w/o resource file?

Issue description

I generate the project and solution files for a Windows Forms application via cmake . The resources folder of the project contains several .ico files, from which I want the select my_icon3.ico as icon for my application. If I open the project just after creation by cmake then the (Default Icon) is selected as shown here:

cmake 调用后的默认图标

What I need is a cmake command that directly sets the icon to my_icon3.ico :

用户选择图标

How can I achieve this with my CMakeLists.txt without a resource file?

Thanks for your help.

MCVE

Please find here an example to reproduce my issue:

1) Open MS Visual Studio 15 --> New --> Project... --> Windows Forms App
2) Set name: I've chosen P0001 and path --> Ok
3) Create folder Resources in the source directory and copy arbitrary icon in it. I've chosen my_icon2.ico and my_icon3.ico .
4) Close MS Visual Studio 15
5) Copy the following CMakeLists.txt file in the base respository:

cmake_minimum_required (VERSION 3.5)

get_filename_component(CMAKE_MODULE_PATH ${CMAKE_COMMAND} DIRECTORY)

include(CSharpUtilities) 

#generates the directory structure given in globalDirPath.
#returns all found files (allFiles) - which need to be added to the output
function(AutoGenDirStruct globalDirPath filterF includeParentDirectory allFiles)
    if(${includeParentDirectory})
        string(FIND ${globalDirPath} "/" startPos REVERSE)
        MATH(EXPR startPos "${startPos}+1")
        string(SUBSTRING ${globalDirPath} ${startPos} 100 subDir)
        string(SUBSTRING ${globalDirPath} 0 ${startPos} globalDir)
        AutoGenDirStructInternalOnly(${globalDir} ${subDir})
    else()
        AutoGenDirStructInternalOnly(${globalDirPath} "")
    endif()

    foreach(filter ${${filterF}})
        file(GLOB_RECURSE allFilesLocal "${globalDirPath}/${filter}")
        set(mergedFiles ${mergedFiles} ${allFilesLocal})
    endforeach()
    set(${allFiles} ${mergedFiles} PARENT_SCOPE)

endfunction()

function(AutoGenDirStructInternalOnly globalDirPath subDirectoryName)

   file(GLOB children RELATIVE "${globalDirPath}/${subDirectoryName}" "${globalDirPath}/${subDirectoryName}/*")

   foreach(child ${children})

          if(IS_DIRECTORY ${globalDirPath}/${subDirectoryName}/${child})

            AutoGenDirStructInternalOnly(${globalDirPath} ${subDirectoryName}/${child})

          else()

              if(NOT ${subDirectoryName} STREQUAL "")
                string(REPLACE "/" "\\" groupname ${subDirectoryName})
                source_group(${groupname} FILES ${globalDirPath}/${subDirectoryName}/${child})
              endif()

          endif()

   endforeach()

endfunction()

SET(CMAKE_INCLUDE_CURRENT_DIR ON)
SET(CMAKE_CSharp_FLAGS "/langversion:7")
SET_PROPERTY(GLOBAL PROPERTY USE_FOLDERS ON)

project (P0001)

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})

# Sub-directories where more CMakeLists.txt exist
ADD_SUBDIRECTORY(P0001/SRC)

6) Copy the following CMakeLists.txt file in the source directory:

cmake_minimum_required (VERSION 3.8)
enable_language(CSharp)

file(GLOB SOURCES
    "*.cs"
    "*.resx"
)

SET(filter "*.cs" "*.bmp" "*.po" "*.ico" "*.config" "*.settings" "*.resx") 
AutoGenDirStruct(${CMAKE_CURRENT_SOURCE_DIR} filter TRUE SOURCES)
csharp_set_designer_cs_properties(${SOURCES}) 
csharp_set_windows_forms_properties(${SOURCES})

add_executable(P0001 WIN32 ${SOURCES} ${EXTERNAL} ${RES_FILES})

set_target_properties(P0001 PROPERTIES VS_GLOBAL_ROOTNAMESPACE "P0001") 

set_property(TARGET P0001 PROPERTY VS_DOTNET_REFERENCES "System;System.Data;System.Configuration;System.Core;System.Data.DataSetExtensions;System.Xml;System.Xml.Linq;System.Windows.Forms;System.Drawing")
set_property(TARGET P0001 PROPERTY FOLDER "Gui")
set_property(TARGET P0001 PROPERTY VS_DOTNET_TARGET_FRAMEWORK_VERSION "v4.0")

7) Call cmake the generate the solution for MS Visual Studio 15 to get screenshot 1

My project repository looks as follows:

<Base dir>
  |--<Build>                       # folder for cmake
  |--CMakeLists.txt                # file from 5)
  |--<P0001>
  |    |--<SRC>
  |    |    |--CMakeLists.txt      # file from 6)
  |    |    |--<Resources>
  |    |    |    |--my_icon3.ico   # required icon

I hope that you can reproduze my issue?

Related issues

I found some help in the net, but it's more C++ related:

Setting the application icon with CMake

or it requires an resource file:

[CMAKE] Setting the and Icon for a Windows Executable

[SOLVED] RC files and CMake : Executable Icon problem (Visual Studio)

Is there really no other way to configure this via cmake ?

Thanks again.


I assume:

set_property(TARGET P0001 PROPERTY VS_GLOBAL_ApplicationIcon "${CMAKE_CURRENT_SOURCE_DIR}/Resources/my_icon3.ico")

will do the job.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM