[英]Windows Forms: Set window icon in source code (not using the designer tool)
I wrote a Windows Form from scratch, only in C# code, not using Visual Studio's form editor. 我从头开始编写Windows窗体,仅使用C#代码,而不使用Visual Studio的窗体编辑器。
Now I want to set an icon to the window, so I added the myapp.ico
file as a resource and wrote in the Form's constructor: 现在,我想在窗口上设置一个图标,因此我将myapp.ico
文件添加为资源,并写入了Form的构造函数:
var resources = new System.ComponentModel.ComponentResourceManager(typeof(MyForm));
Icon = ((System.Drawing.Icon)(resources.GetObject("myapp.Icon")));
I also set myapp.ico
in the project's Properties>Application>Icon and manifest>Icon. 我还在项目的“属性”>“应用程序”>“图标”和“清单”>“图标”中设置了myapp.ico
。
But the icon is not displayed, I get the default 3 squares icon: 但是没有显示该图标,我得到了默认的3格图标:
Loading the icon from resources like this fixed the issue: 从这样的资源中加载图标可解决此问题:
Icon = ((System.Drawing.Icon)(Properties.Resources.myapp));
(You first need to add the myapp.ico
file as a resource to the project) (您首先需要将myapp.ico
文件作为资源添加到项目中)
I am open to any better solution. 我愿意接受任何更好的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.