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);
}
}
13 Comments »
Leave a comment
-
Recent
- Bush sings U2
- Inflatable spacecraft launches from Russia
- Darth Vader Calls the Emperor
- Why increase minimum wage when you can vote yourself a raise?
- Colbert in Action
- Slow motion beer cannon footage
- Give a dog a bone – over the internet
- Super Mario Brothers Themepark
- Search on Microsoft main page doesn’t work in Firefox
- Singularity – The c# OS
- Skype Wrapper for .NET Beta
- Ricky Manning Jr’s PR Machine
-
Links
-
Archives
- July 2006 (2)
- June 2006 (4)
- May 2006 (5)
- April 2006 (5)
- March 2006 (6)
- February 2006 (22)
- January 2006 (31)
-
Categories
-
RSS
Entries RSS
Comments RSS
If anyone else has any ideas about how to do this please let me know!
Just found out that you also need [STAThread] tag on the Main Entry Point
Thanks bluehouse, this worked great!!!!!
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?
Fred, I don’t have that problem. Every time I right click on the notify icon, the menu pops up.
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();
}
}
}
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)
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.
Thank You
Also, you can just create an IContainer, assign it to new Container(), and just use that. No form required.
Perfect, thanks very much to bluehouse and Julo. That’s exactly what I’m looking for.
Funciona perfecto!!!!!
Works well
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.