如何在没有资源文件的情况下,通过CMake为C# Windows Forms应用程序设置图标?

3

问题描述

我通过cmake生成Windows Forms应用程序的项目和解决方案文件。 该项目的资源文件夹包含多个.ico文件,我想从中选择my_icon3.ico作为我的应用程序图标。 如果我在cmake创建后立即打开项目,则会选择(默认图标),如下所示:

Default icon after cmake call

我需要的是一个cmake命令,直接将图标设置为my_icon3.ico

User selected icon

我如何在没有资源文件的情况下使用我的CMakeLists.txt实现这一点?

感谢您的帮助。

MCVE

请在此处找到重现我的问题的示例:

1)打开MS Visual Studio 15 -> 新建 -> 项目... -> Windows表单应用程序
2)设置名称:我选择了P0001和路径 -> 确定
3)在源目录中创建Resources文件夹,并将任意图标复制到其中。 我选择了my_icon2.icomy_icon3.ico
4)关闭MS Visual Studio 15
5)将以下CMakeLists.txt文件复制到基本存储库中:

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) 将以下 CMakeLists.txt 文件复制到源目录中:

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) 调用cmake生成MS Visual Studio 15的解决方案,以获取截图1

我的项目存储库如下:

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

我希望你能复现我的问题?

相关问题

我在网上找到了一些帮助,但它更多与 C++ 相关:

使用 CMake 设置应用程序图标

或者需要一个 resource 文件:

[CMAKE] 为 Windows 可执行文件设置图标

[已解决] RC 文件和 CMake:可执行文件图标问题(Visual Studio)

难道没有其他方式可以通过 CMake 进行配置吗?

再次感谢。



1
谢谢您提供的参考资料,对我的C++情境非常有帮助。 - caoanan
1个回答

5

I assume:

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

会完成工作。

谢谢,cmake 中的此属性设置已按预期完成工作。 - CKE

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接