如何通过不带资源文件的cmake为C#Windows Forms应用程序设置图标?

时间:2019-02-05 11:49:18

标签: c# visual-studio cmake

问题描述

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

Default icon after cmake call

我需要一个my_icon3.ico命令,将图标直接设置为CMakeLists.txt

User selected icon

如何在没有资源文件的情况下使用MS Visual Studio 15来实现此目的?

感谢您的帮助。

MCVE

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

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

CMakeLists.txt

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

cmake

7)调用MS Visual Studio 15<Base dir> |--<Build> # folder for cmake |--CMakeLists.txt # file from 5) |--<P0001> | |--<SRC> | | |--CMakeLists.txt # file from 6) | | |--<Resources> | | | |--my_icon3.ico # required icon 生成解决方案以获取screenshot 1

我的项目存储库如下:

C++

我希望您能重述我的问题?

相关问题

我在网络上找到了一些帮助,但与resource的联系更多:

Setting the application icon with CMake

或者它需要一个cmake文件:

[CMAKE] Setting the and Icon for a Windows Executable

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

真的没有其他方法可以通过{{1}}对此进行配置吗?

再次感谢。


1 个答案:

答案 0 :(得分:1)

我认为:

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

会做。