简体   繁体   English

Windows窗体:在源代码中设置窗口图标(不使用设计器工具)

[英]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.

相关问题 在带有Windows窗体的Designer生成的代码中使用#if指令进行条件编译 - Using the #if directive in designer generated code with windows forms for conditional compilation Windows窗体设计器删除我的代码 - Windows Forms designer deletes my code 无法使用带有 Visual Studio 2010 的 Windows 窗体获取设计器视图窗口 - Unable to get the designer view window back using windows forms with Visual Studio 2010 使用Windows Forms Designer将属性绑定到控件 - Binding a property to a control using Windows Forms Designer Windows窗体,设计器和单例 - Windows Forms, Designer, and Singleton 将应用程序图标和窗口图标设置为Windows 7友好图标 - Set application icon and window icon to Windows 7 friendly icon C#Windows窗体。 覆盖设计师代码 - C# Windows Forms. Override designer code 像Windows窗体一样使用.designer.cs生成代码 - Generate code as Windows Forms do with .designer.cs 是否可以控制IExtenderProvider如何格式化Windows Forms Designer代码中的输出? - Is it possible to control how IExtenderProvider formats the output in the Windows Forms Designer code? 如何使用代码设置Windows窗体表单的StartPosition? - How do you set the StartPosition of a Windows Forms form using code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM