Question

I'd like to create a C# app that will only have a custom tray icon visible, nothing else. No main form, no tray icon menus, just the icon. What's the easiest way to do so?

I guess I should start out with a console application, since I don't need any forms. So far I only found complicated examples of how to hide the main form in a WinForms app, with a lot of other unnecessary stuff.

Was it helpful?

Solution

you can create an app context:

static class Program
    {       
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new TrayApplicationContext());
        }
    }

    public class TrayApplicationContext : ApplicationContext
    {
        private readonly NotifyIcon _trayIcon;
        public TrayApplicationContext()
        {
            _trayIcon = new NotifyIcon
                            {
                                //it is very important to set an icon, otherwise  you won't see your tray Icon.
                                Icon = Resources.AppIcon,                                
                                Visible = true
                            };
        }        
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top