Set same icon for all my Forms in C#
Set default form icon for all forms in C#, the simple, best, graceful method, without inherited form, without change forms one by one.
https://stackoverflow.com/questions/189031/set-same-icon-for-all-my-forms
如果C#工程,想更改所有窗体的左上角的图标icon为主程序图标,而不是默认的C#窗体色块图标,可以用下面的方法:
打开program.cs,在
private static void Main()
{
下面添加两行代码:
var icon = System.Drawing.Icon.ExtractAssociatedIcon(Application.ExecutablePath);
typeof(Form).GetField("defaultIcon", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).SetValue(null, icon);
}
就可以了!
网上有的人用写一个基类窗体,然后所有窗体从这个基类窗体继承的方法,可以用但比较累赘。
当然,如果你只想更改某一个窗体的图标,可以修改窗体icon,也可以在窗体load事件中,添加代码:
this.Icon = System.Drawing.Icon.ExtractAssociatedIcon(Application.ExecutablePath);