![](/img/trans.png)
[英]Set a value in a Configuration File in a Windows Forms C# Application
[英]How to set an icon for C# Windows Forms application via cmake w/o resource file?
我通過cmake
為 Windows 窗體應用程序生成項目和解決方案文件。 項目的資源文件夾包含幾個.ico
文件,我希望從中選擇my_icon3.ico
作為我的應用程序的圖標。 如果我在cmake
創建后立即打開項目,則選擇(Default Icon)
,如下所示:
我需要的是一個直接將圖標設置為my_icon3.ico
的cmake
命令:
如何在沒有資源文件的情況下使用CMakeLists.txt
實現此目的?
謝謝你的幫助。
請在此處找到一個示例來重現我的問題:
1) 打開MS Visual Studio 15
--> 新建 --> 項目... --> Windows 窗體應用程序
2) 設置名稱:我選擇了P0001
和路徑 --> 好的
3) 在源目錄下創建文件夾Resources
,將任意圖標復制到其中。 我選擇了my_icon2.ico
和my_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++
相關:
或者它需要一個resource
文件:
[CMAKE] 設置 Windows 可執行文件的 和 圖標
[已解決] RC 文件和 CMake:可執行圖標問題 (Visual Studio)
真的沒有其他方法可以通過cmake
進行配置嗎?
再次感謝。
我假設:
set_property(TARGET P0001 PROPERTY VS_GLOBAL_ApplicationIcon "${CMAKE_CURRENT_SOURCE_DIR}/Resources/my_icon3.ico")
會做的工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.