为我的所有表单设置相同的图标
有什么办法可以为我的所有表单设置相同的图标,而不必一一更改? 就像您为解决方案中的所有项目设置 GlobalAssemblyInfo 时一样。
Is there any way to set the same icon to all my forms without having to change one by one?
Something like when you setup GlobalAssemblyInfo
for all your projects inside your solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在项目属性中> 应用>> 图标和清单> 浏览 *.ico 文件并将其添加到此处。
在表单的构造函数或
_Load
事件中,只需添加:In the project properties > Application > Icon and Manifest > browse for a *.ico file and add it there.
In the constructor or
_Load
event of a Form, simply add:一种选择是从一个公共基本表单继承,该表单在构造函数中设置图标(可能来自 resx)。 另一个选择可能是 PostSharp - 看起来应该可以通过 AOP 来做到这一点(设置 .Icon); 不过,这并不是微不足道的。 最后,您可以使用一个简单的实用方法(可能是扩展方法)来执行相同的操作。
最重要的是,使用第一个选项,您可能会冒来自
: Form
或的 Ctrl+H (全部替换)的风险: System.Windows.Forms.Form
到:MyCustomForm
。One option would be to inherit from a common base-Form that sets the Icon in the constructor (presumably from a resx). Another option might be PostSharp - it seems like it should be possible to do this (set .Icon) via AOP; not trivial, though. Finally, you could use a simple utility method (perhaps an extension method) to do the same.
Best of all, with the first option, you could probably risk a Ctrl+H (replace all) from
: Form
or: System.Windows.Forms.Form
to: MyCustomForm
.除了 Marc 的建议之外,您可能还希望表单自动继承包含/调用它们的执行程序集的图标。
这可以通过将以下代码添加到继承的表单中来完成:
In additional to Marc's recommendation, you may want your forms to automatically inherit the icon of the executing assembly that contains/calls them.
This can be done by adding the following code to your inherited form:
这是给所有表单设置相同图标的方法,无需一一更改。
这是我在应用程序中编写的代码。
这是一个可供使用的完整示例。
这里是 FormUtils 类:
这里是 EntryAssemblyInfo 类。 此示例已被截断。 这是我从 System.Winforms.Application 获取的自定义编码类。
This is the way to set the same icon to all forms without having to change one by one.
This is what I coded in my applications.
Here a full example ready to use.
Here is the class FormUtils:
And here the class EntryAssemblyInfo. This is truncated for this example. It is my custom coded class taken from System.Winforms.Application.
我不确定是否要在这里使用继承,因此我使用了扩展方法:
然后在任何形式的构造函数中:
注意:如果您尝试从网络位置运行应用程序,这将导致崩溃
I wasn't sure if I wanted to use inheritance here, so I used an extension method:
Then in any form's constructor:
Note: this will cause a crash if you try to run the application from a network location
在构造函数中设置的替代方法是重写
Owner
属性,并采用所有者表单的图标。Alternative to setting in the constructor is overriding the
Owner
property, and taking the icon of the owner form.这是一个老问题,但这种方法可以让我在所有表单中获得相同的图标,而不必将其添加到每个表单的属性中。
首先,我在“属性”下的“资源”节点中添加了一个新图标(添加资源 => 新图标)。
默认情况下,会创建一些图标并将其存储在您的资源位置(查找图标上的“文件名”属性)。
假设您已准备好 .ico 文件,可以将其复制到该文件夹。
删除在该文件夹中创建的 Visual Studio,并将您自己的名称重命名为完全相同的名称。
Visual Studio 将提示您是否要在资源修改后重新加载资源。 (单击“是”)
这样您就可以在资源下使用您的徽标。
现在我已经制定了一个通用方法,将表单标题更改为默认值并设置表单图标,并在 InitializeComponent(); 之后立即在每个表单中调用它。
在 cs 表单中看起来如何(m 是保存通用方法的类):
这就是方法本身:
当然,您不需要在此处添加表单标题,但这只是因为我想展示一些存储给用户的属性(当前服务器)
It is an old question, but this method worked for me to get the same icon in all forms without having to add it in every form's properties.
First I added a new icon in the 'Resources' node under 'Properties' (add resource => New Icon).
By default some icon is created and stored in your resources location (look for the 'Filename' property on the icon).
Assuming you have a .ico file ready, you can copy it to that folder.
Delete the one Visual Studio created in that folder and rename your own to the exact same name.
Visual Studio will prompt you if you want to reload the resource since it was modified. (click 'Yes')
That way you have your Logo available under resources.
Now I have made a general method to change the form title to my default and set the form icon and call that in every form right after InitializeComponent();
How that looks in the form cs (m is the class that holds the general method):
And this is the method itself:
Of course you don't need to add the form title here, but that is just because I want to show some stored properties to the user (current server)
我不确定 MS VS 设计器是否可以处理不直接从 Form 派生的 Form。 如果没有,那么您可以尝试将主窗体的图标复制到所有其他窗体:
对于 Forms 集合中的每个表单
或者可能在每个表单的 _Loaded 事件中:
I'm not sure if the MS VS designer can deal with Forms that don't derive directly from Form. If not then you may try to copy the main form's icon to all other forms:
for each form in Forms collection
Or perhaps in each Form's _Loaded event: