Set same icon for all my Forms
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
.
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:
public MyCustomForm()
{
Icon = GetExecutableIcon();
}
public Icon GetExecutableIcon()
{
IntPtr large;
IntPtr small;
ExtractIconEx(Application.ExecutablePath, 0, out large, out small, 1);
return Icon.FromHandle(small);
}
[DllImport("Shell32")]
public static extern int ExtractIconEx(
string sFile,
int iIndex,
out IntPtr piLargeVersion,
out IntPtr piSmallVersion,
int amountIcons);
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:this.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);