Accessing embedded resources in C++/CLI

霸气de小男生 提交于 2019-12-08 03:45:16

问题


I've been working with C# for a while, and I'm trying to write a .NET app in C++ this time. In C# I was able to access the managed resources from the code quite easily, the name of the resource file worked sort of like a class, so if I had a string called "abc" in a resource file called cba.resx, simply writing cba.abc returned with the string from the resource file. Even intellisense works with it. Same thing works with icons, etc.

Is it possible to do the same in C++, and if so, how? Or if not, what is the easiest way to access strings/icons in resource files?


回答1:


In Visual Studio, C# projects have a resx file designer and a properties class generator. C++/CLI projects only get the resx file designer. Both get the build steps to embed the compiled resources in the assembly.

You can write a Properties class yourself or just access the resources in code where you need them like this:

auto resourceAssembly = Reflection::Assembly::GetExecutingAssembly();
// .Resources is the name generated by resxgen, e.g., from the input file name Resources.resx
auto resourceName = resourceAssembly->GetName()->Name + ".Resources"; 
auto resourceManager = gcnew Resources::ResourceManager(resourceName, resourceAssembly);
auto String1 = cli::safe_cast<String^>(resourceManager->GetObject("String1"));


来源:https://stackoverflow.com/questions/16759679/accessing-embedded-resources-in-c-cli

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!