【问题标题】:How to set an icon for C# Windows Forms application via cmake w/o resource file?如何通过 cmake w/o 资源文件为 C# Windows Forms 应用程序设置图标?
【发布时间】:2019-02-05 11:49:17
【问题描述】:

问题描述

我通过cmake 为 Windows 窗体应用程序生成项目和解决方案文件。 该项目的资源文件夹包含几个.ico文件,我想要的 选择my_icon3.ico 作为我的应用程序的图标。如果我在之后打开项目 由cmake 创建,然后选择(Default Icon),如下所示:

我需要的是直接将图标设置为my_icon3.icocmake命令:

如何在没有资源文件的情况下使用我的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) 调用cmakeMS Visual Studio 15 生成解决方案得到screenshot 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++相关:

Setting the application icon with CMake

或者它需要一个resource 文件:

[CMAKE] Setting the and Icon for a Windows Executable

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

真的没有其他方法可以通过cmake 进行配置吗?

再次感谢。


【问题讨论】:

  • 感谢您的参考,它们对我的 C++ 场景很有帮助

标签: c# visual-studio cmake


【解决方案1】:

我假设:

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

会做的。

【讨论】:

  • 谢谢,cmake 中的这个属性设置可以按需要完成。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-22
  • 2013-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多