在C ++ / CLI中访问嵌入式资源 [英] Accessing embedded resources in C++/CLI

查看:140
本文介绍了在C ++ / CLI中访问嵌入式资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用C#一段时间了,这次我正试图用C ++编写.NET应用。在C#中,我可以很容易地从代码访问托管资源,资源文件的名称就像一个类一样工作,因此,如果在资源文件cba.resx中有一个名为 abc的字符串,只需编写cba.abc与资源文件中的字符串一起返回。甚至intellisense也可以使用它。同样的东西也可以用于图标等。

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.

在C ++中是否可以做同样的事情,如果可以,怎么做?否则,访问资源文件中的字符串/图标的最简单方法是什么?

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?

推荐答案

在Visual Studio中,C#项目具有resx文件设计器和属性类生成器。 C ++ / CLI项目仅获得resx文件设计器。两者都获得了将编译后的资源嵌入到程序集中的构建步骤。

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.

您可以自己编写一个Properties类,或者仅在需要它们的代码中访问这些资源,如下所示:

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"));

这篇关于在C ++ / CLI中访问嵌入式资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆