I am writing a simple application that I would like to control with a notifyIcon rather than a form, I have follwed examples I found through Google, but my notifyIcon will not show up. What am I doing wrong?
static class MainEntryClass
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
C2F TestApp = new C2F();
Application.Run();
TestApp.Dispose();
}
}
class C2F
{
public C2F()
{
InitializeComponent();
loadSettings();
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(C2F));
this.niC2F = new System.Windows.Forms.NotifyIcon(this.components);
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.settingsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.separatorToolStripMenuItem = new System.Windows.Forms.ToolStripSeparator();
this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.contextMenuStrip1.SuspendLayout();
//
// niC2F
//
this.niC2F.BalloonTipText = "MyApp";
this.niC2F.Icon = ((System.Drawing.Icon)(Clipboard2File.Properties.Resources.ResourceManager.GetObject("MyIcon.ico")));
this.niC2F.Text = "MyApp";
this.niC2F.ContextMenuStrip = this.contextMenuStrip1;
this.niC2F.ShowBalloonTip(5);
this.niC2F.Visible = true;
this.niC2F.MouseClick += new System.Windows.Forms.MouseEventHandler(this.niC2F_MouseClick);
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.settingsToolStripMenuItem,
this.separatorToolStripMenuItem,
this.exitToolStripMenuItem});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(153, 76);
//
// settingsToolStripMenuItem
//
this.settingsToolStripMenuItem.Name = "settingsToolStripMenuItem";
this.settingsToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.settingsToolStripMenuItem.Text = "Settings";
this.settingsToolStripMenuItem.Click += new System.EventHandler(this.settingsToolStripMenuItem_Click);
//
// separatorToolStripMenuItem
//
this.separatorToolStripMenuItem.Name = "separatorToolStripMenuItem";
this.separatorToolStripMenuItem.Size = new System.Drawing.Size(149, 6);
this.separatorToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
//
// exitToolStripMenuItem1
//
this.exitToolStripMenuItem.Name = "exitToolStripMenuItem1";
this.exitToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.exitToolStripMenuItem.Text = "Exit";
}
private System.ComponentModel.IContainer components = null;
private Form1 frmSettings = new Form1();
private Settings C2FSettings = new Settings();
private System.Windows.Forms.NotifyIcon niC2F;
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem settingsToolStripMenuItem;
private System.Windows.Forms.ToolStripSeparator separatorToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
}
One more reason why NotifyIcon is not shown is if Windows Explorer is running with elevated privileges while your tray application isn't (only on systems with UAC of course).
This can happen if explorer.exe crashed or was killed, and then user manually restarted it from the elevated Task Manager.
NotifyIcon control uses Shell_NotifyIcon native method inside, but doesn't check for the return value. If Shell_NotifyIcon returns FALSE, you won't be ever notified.
I had to breakpoint with WinDbg on Shell_NotifyIcon and GetLastError gave me ERROR_ACCESS_DENIED. So I realized that there's a permission issue, and it might be caused by restarted explorer elevation. Further tests confirmed this assumption.
However this is rather rare case.
I actually just finished a project that started as a NotifyIcon. Your code (I'm guessing you just provided a snippet) is incredibly similar to mine.
I checked your code, and the only change I had to make to get it to work was changing the way you called the icon to:
Below is a working sample with a right-click menu and double-click functionality:
Hope this helps you, let me know if you have any questions!