(C++ WinForms VS 2022) 图标异常

发布于 2025-01-11 17:44:46 字数 2037 浏览 0 评论 0原文

当我尝试在属性窗口中更改表单的图标 (this) 时,此行代码在 .h 文件中生成:

this->Icon = (cli::safe_cast<System::Drawing::Icon^>(resources->GetObject(L"$this.Icon")));

但是当我尝试运行它时,它给了我一个 System.Resources.MissingManfiestResourceException 错误:

System.Resources.MissingManifestResourceException HResult=0x80131532 消息=找不到任何适合指定的资源 文化或中立文化。确保 “Thermodynamics_Generator.Form1.resources”已正确嵌入或 在编译时链接到程序集“thermodynamics_generator”,或者 所有所需的卫星组件都是可加载的并且完全 签署了。来源=mscorlib StackTrace:位于 System.Resources.ManifestBasedResourceGroveler.HandleResourceStreamMissing(字符串 文件名)位于 System.Resources.ManifestBasedResourceGroveler.GrovelForResourceSet(CultureInfo 文化,Dictionary`2 localResourceSets,布尔值 tryParents,布尔值 createIfNotExists、StackCrawlMark&堆栈标记)在 System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo requestCulture,布尔值 createIfNotExists,布尔值 tryParents, StackCrawlMark&堆栈标记)在 System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo 文化、布尔值 createIfNotExists、布尔值 tryParents) at System.Resources.ResourceManager.GetObject(字符串名称,CultureInfo 文化,布尔值wrappUnmanagedMemStream)位于 System.Resources.ResourceManager.GetObject(字符串名称)位于 Thermodynamics_Generator.Form1.InitializeComponent() 中 C:\Users\my_name\source\repos\thermodynamics_generator\thermodynamics_generator\thermodynamics_generator.h:line 第1194章 C:\Users\my_name\source\repos\thermodynamics_generator\thermodynamics_generator\thermodynamics_generator.h:line 26 在 main() 中 C:\Users\my_name\source\repos\thermodynamics_generator\thermodynamics_generator\CppCLR_WinformsProject.cpp:line 11

我已经尝试了另一篇 帖子 中的类似操作来执行此操作:

this->Icon = gcnew System::Drawing::Icon(L"app.ico");

它可以工作,但是当在其他计算机上运行时,它根本不想运行(只想在我的计算机上运行)。

我还检查了 .resx 文件以查看 $this.Icon 是否存在,但仍然给了我一个例外。

如果我提到其他事情,请告诉我。

When I attempt to change the icon of the form in the properties window (this), this line of code gets generated in the .h file:

this->Icon = (cli::safe_cast<System::Drawing::Icon^>(resources->GetObject(L"$this.Icon")));

But when I try to run it, it gives me a System.Resources.MissingManfiestResourceException error:

System.Resources.MissingManifestResourceException HResult=0x80131532
Message=Could not find any resources appropriate for the specified
culture or the neutral culture. Make sure
"Thermodynamics_Generator.Form1.resources" was correctly embedded or
linked into assembly "thermodynamics_generator" at compile time, or
that all the satellite assemblies required are loadable and fully
signed. Source=mscorlib StackTrace: at
System.Resources.ManifestBasedResourceGroveler.HandleResourceStreamMissing(String
fileName) at
System.Resources.ManifestBasedResourceGroveler.GrovelForResourceSet(CultureInfo
culture, Dictionary`2 localResourceSets, Boolean tryParents, Boolean
createIfNotExists, StackCrawlMark& stackMark) at
System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo
requestedCulture, Boolean createIfNotExists, Boolean tryParents,
StackCrawlMark& stackMark) at
System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo
culture, Boolean createIfNotExists, Boolean tryParents) at
System.Resources.ResourceManager.GetObject(String name, CultureInfo
culture, Boolean wrapUnmanagedMemStream) at
System.Resources.ResourceManager.GetObject(String name) at
Thermodynamics_Generator.Form1.InitializeComponent() in
C:\Users\my_name\source\repos\thermodynamics_generator\thermodynamics_generator\thermodynamics_generator.h:line
1194 at Thermodynamics_Generator.Form1..ctor() in
C:\Users\my_name\source\repos\thermodynamics_generator\thermodynamics_generator\thermodynamics_generator.h:line
26 at main() in
C:\Users\my_name\source\repos\thermodynamics_generator\thermodynamics_generator\CppCLR_WinformsProject.cpp:line
11

And I have tried something like this from another post to do this instead:

this->Icon = gcnew System::Drawing::Icon(L"app.ico");

It works, but when ran on other computers, it just does not want to run at all (only wants to run on my computer).

I also checked the .resx file to see $this.Icon is there but still gives me an exception.

If there is other things I mention, let me know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饮惑 2025-01-18 17:44:46
this->Icon = (cli::safe_cast<System::Drawing::Icon^>(resources->GetObject(L"$this.Icon")));

这行代码是自动生成的,其中$this.Icon包含文件路径,可以读取GetObject() 的文档。和 文档指出

如果找不到合适的资源,则调用构造函数
创建一个有效的 ResourceManager 对象。然而,尝试
检索资源会抛出 MissingManifestResourceException
例外。有关处理异常的信息,请参阅
处理 MissingManifestResourceException 和 MissingSatelliteAssembly
本文后面的例外部分。

我们可以得出结论,该程序没有找到您的资源文件。如果要在其他计算机上运行程序,必须保证图标路径与您的计算机一致。

编辑:

我建议您检查资源是否包含为项目,在解决方案资源管理器中选择所有文件,然后右键单击资源文件并选择包含在项目中。

输入图片此处描述

在此处输入图像描述

this->Icon = (cli::safe_cast<System::Drawing::Icon^>(resources->GetObject(L"$this.Icon")));

This line of code is automatically generated, where $this.Icon contains the file path, you could read the documentation for the GetObject(). And the documentation states that

If the appropriate resources cannot be found, the constructor call
creates a valid ResourceManager object. However, the attempt to
retrieve a resource throws a MissingManifestResourceException
exception. For information about dealing with the exception, see the
Handling MissingManifestResourceException and MissingSatelliteAssembly
Exceptions section later in this article.

We could conclude that the program did not find your resource file. If you want to run the program on other computers, you must ensure that the icon path is consistent with your computer.

Edit:

I suggest you to check if the resource is included as a project, select All Files in Solution Exploer, then right click on the resource file and select Include In Project.

enter image description here

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文