Wednesday, February 5, 2014

Load an icon file embedded in your executable

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

Code Formater

Paste Here Your Source Code
Source Code Formatting Options
1) Convert Tab into Space :
2) Need Line Code Numbering :
3) Remove blank lines :
4) Embeded styles / Stylesheet :
5) Code Block Width :
6) Code Block Height :
7) Alternative Background :
Copy Formatted Source Code
 
Preview Of Formatted Code