更改整个应用程序的默认 Winform 图标 [英] Change Default Winform Icon Across Entire App

查看:24
本文介绍了更改整个应用程序的默认 Winform 图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以更改 Winform 上使用的默认图标吗?

Can I change the default icon used on a Winform?

我的大多数表单都将其图标属性设置为自定义图标.对于少数从裂缝中溜走的表单,我不想要通用的嘿,看,他在 Visual Studio 中制作的"图标.

Most of my forms have their icon property set to a custom icon. For the few forms that slip through the cracks, I don't want the generic "hey look, he made this in visual studio" icon.

一种解决方案是冗长乏味地检查我的每一个表单,以确保它们要么设置了自定义图标,要么将 ShowIcon 设置为 False.

One solution is to tediously check every one of my forms to make sure they either have a custom icon set or have ShowIcon set to False.

另一种解决方案是让我的每个表单都从一个基类继承,该基类在构造函数中设置了一个自定义图标.

Another solution is to have every one of my forms inherit from a base class that sets a custom icon in the constructor.

除了这些解决方案,我还有什么其他选择?

Aside from those solutions, what other options do I have?

我希望有一种方法可以用我自己的方法替换股票图标的来源.它在某个地方的资源文件中吗?或者它是否嵌入到我不能(或真的,真的不应该)修改的 .NET dll 中?

I was hoping there would be a way to replace the source of the stock icon with my own. Is it in a resource file somewhere? Or is it embedded in a .NET dll that I can't (or really, really shouldn't) modify?

赏金有没有办法在不编辑或编写一行代码的情况下完成此操作?我不在乎解决方案是多么不切实际、多么复杂、多么浪费时间……我只是想知道它是否可能.我需要满足我的好奇心.

BOUNTY Is there a way to accomplish this without editing or writing a single line of code? I don't care how impractical, complicated, waste-of-time the solution is... I just want to know if it's possible. I need to satisfy my curiosity.

推荐答案

default 图标嵌入在 winforms dll 中——看反射器 (DefaultIcon) 是:

The default icon is embedded in the winforms dll - looking at reflector (DefaultIcon) it is:

defaultIcon = new Icon(typeof(Form), "wfc.ico");

其中没有检查另一个常见位置的魔法,因此您无法在不更改代码的情况下执行此操作.

There is no magic in there that checks another common location, so you can't do it without changing code.

您总是可以通过基于场的反射来拥抱黑暗的力量吗?注意:这是hacky和脆弱的.在你自己的头上!但它有效:

You could always embrace the forces of darkness with field-based reflection? Note: this is hacky and brittle. On your own head! But it works:

[STAThread]
static void Main() {
    // pure evil
    typeof(Form).GetField("defaultIcon",
            BindingFlags.NonPublic | BindingFlags.Static)
        .SetValue(null, SystemIcons.Shield);

    // all forms now default to a shield
    using (Form form = new Form()) {
        Application.Run(form);
    }
}

正确地做;两个常用选项;

To do it properly; two common options;

  • 具有图标集的基本 Form
  • 工厂 Form 方法 - 可能类似于:
  • a base Form class which has the icon set
  • a factory Form method - perhaps something like:

代码:

public static T CreateForm<T>() where T : Form, new() {
    T frm = new T();
    frm.Icon = ...
    // any other common code
    return frm;
}

然后代替:

using(var frm = new MySpecificForm()) {
    // common init code
}

类似于:

using(var frm = Utils.CreateForm<MySpecificForm>()) {

}

当然 - 这并没有更漂亮!另一种选择可能是 C# 3.0 扩展方法,也许作为流畅的 API:

Of course - that isn't much prettier! Another option might be a C# 3.0 extension method, perhaps as a fluent API:

public static T CommonInit<T>(this T form) where T : Form {
    if(form != null) {
        form.Icon = ...
        //etc
    }
    return form;
}

using(var frm = new MySpecificForm().CommonInit()) {
    // ready to use
}

这与您现有的代码仅相差一个 .CommonInit().

This is then just a .CommonInit() away from your existing code.

这篇关于更改整个应用程序的默认 Winform 图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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