Random Thoughts

My thoughts about life, coding, science, technology and other stuff

How to Create a Notify Icon in c# without a form

Tested with .NET Framework 2.0 and Windows XP Professional SP2

This has been bugging me for weeks. I wanted a formless application with a Notify Icon but there didn’t seem to be way to do it. After searching for a solution, I figured I’d give it a try myself. As it turned out it was so easy it was ridiculous. All you have to do create a class that is inherits the iContainer interface. When you create the instance of the notify icon, pass a container object. After that, you can use at as if it was on a form. Make sure you set the Icon property as well. I also have this working with a context menu and I’m guess it will work with any control that requires an iContainer interface. When you create the project, go to the project properties and make sure Output Type is set to Windows Application. Here’s the code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;

class ControlContainer : IContainer
{
   ComponentCollection _components;

  public ControlContainer()
   {
     _components = new ComponentCollection(new IComponent[] { });
   }

  public void Add(IComponent component)
   { }

  public void Add(IComponent component, string Name)
   { }

  public void Remove(IComponent component)
   { }

  public ComponentCollection Components
   {
     get { return _components; }
   }

  public void Dispose()
   {
     _components = null;
   }
}

class MainEntryClass
{
   static void Main(string[] args)
   {
     SomeClass sc = new SomeClass();
     Application.Run();
   }
}

class SomeClass
{
   ControlContainer container = new ControlContainer();
   private System.Windows.Forms.NotifyIcon notifyIcon1;

  public SomeClass()
   {
     this.notifyIcon1 = new NotifyIcon(container);
   }
}

January 24, 2006 - Posted by bluehouse | Coding | | 13 Comments

13 Comments »

  1. If anyone else has any ideas about how to do this please let me know!

    Comment by bluehouse | January 24, 2006

  2. Just found out that you also need [STAThread] tag on the Main Entry Point

    Comment by bluehouse | January 24, 2006

  3. Thanks bluehouse, this worked great!!!!!

    Comment by tmoney | September 26, 2006

  4. Have you also got the focus problem? First tim use seems fine, after that the context menu only shows for a fraction of a second then disappears. Any workarounds for that?

    Comment by Fred | October 13, 2006

  5. Fred, I don’t have that problem. Every time I right click on the notify icon, the menu pops up.

    Comment by bluehouse | October 13, 2006

  6. The solution is more ridiculous than that …

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;

    namespace DataUploader
    {
    static class Program
    {
    [STAThread]
    static void Main()
    {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    NotifyIcon icn = new NotifyIcon();
    icn.Click += new EventHandler(icn_Click);
    icn.Visible = true;
    icn.Icon = new System.Drawing.Icon(@”SomeIcon.ico”);
    Application.Run();
    }

    static void icn_Click(object sender, EventArgs e)
    {
    Application.Exit();
    }
    }
    }

    Comment by Julo | December 11, 2006

  7. Thank you for the Application.Exit();
    I had been stuck without realizing how to get out of the application loop (i tried .dispose, but it didnt exist)

    Comment by JohnDoe | March 7, 2007

  8. Hi I’m new with C#. It would be great if somebody could point out how to use contexttoolStrip with the above implementation of notifyIcon. thanks. :)

    Comment by vv | March 29, 2007

  9. Thank You

    Comment by Alex | April 25, 2007

  10. Also, you can just create an IContainer, assign it to new Container(), and just use that. No form required.

    Comment by Matt | May 12, 2008

  11. Perfect, thanks very much to bluehouse and Julo. That’s exactly what I’m looking for.

    Comment by Mile | July 11, 2009

  12. Funciona perfecto!!!!!
    Works well

    Comment by Carlos | September 8, 2009

  13. bluehouse and Julo: Thanks, I needed that!
    If you really do need a container, just use the one in System.ComponentModel. In SomeClass, use the following, and you can get rid of the ControlContainer.

    IContainer container = new System.ComponentModel.Container();
    

    Comment by Keith Robertson | September 16, 2009


Leave a comment