When you are developing a Windows Forms application in .NET, it’s not immediately obvious how to programatically load an icon file embedded in your executable.
This recipe shows you the 1 line solution.
Your icon file should be a regular windows icon file. Add it to your project, and in the properties for the icon make sure that it is set to Embedded Resource.
Now in your code, add the following line:
public static NotifyIcon icon;
protected override void OnStartup(StartupEventArgs e)
{
App.icon = new NotifyIcon();
icon.Click += new EventHandler(icon_Click);
icon.Icon = new Icon(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("WorkingHours.time_n.ico"));
//icon.Icon = new System.Drawing.Icon(@"icon\time_n.ico");
icon.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info;
icon.Visible = true;
base.OnStartup(e);
}
You should now be able to use the icon directly in any windows control that uses icons, like the NotifyIcon control.
No comments:
Post a Comment