How to run an "empty" Windows Application that only has a NotifyIcon?
Asked Answered
Z

3

6

I want to make an Application that only has a NotifyIcon. It doesn't need to have at all a "Main" Form. When I want to achieve something like this, I just create an invisible form and run it, but would there be a more "elegant" way of doing this, I'd like to know it.

How do you generally do this? This application can't be a Windows Service, as having the NotifyIcon and its Context Menus is important (each one of them will run a different command).

Thanks

Zandrazandt answered 7/5, 2010 at 12:51 Comment(0)
F
2

Check out this blog post:

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.

It gives you the notify icon, but not a context menu.

Fahrenheit answered 7/5, 2010 at 12:53 Comment(2)
@devoured - I don't know. I was going to post that I thought you did need a main form, but decided to do a search first - just to be sure. I came across the post I linked to and it seemed to answer your question. I haven't tried it myself yet.Fahrenheit
Also, I didn't know you could just do Application.Run() and that'd make the application run an empty message loop.Zandrazandt
Y
4

Another approach is to use special ApplicationContext which will have only the controls you need: Creating a Tasktray Application.

Yellowtail answered 14/7, 2010 at 20:21 Comment(0)
F
2

Check out this blog post:

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.

It gives you the notify icon, but not a context menu.

Fahrenheit answered 7/5, 2010 at 12:53 Comment(2)
@devoured - I don't know. I was going to post that I thought you did need a main form, but decided to do a search first - just to be sure. I came across the post I linked to and it seemed to answer your question. I haven't tried it myself yet.Fahrenheit
Also, I didn't know you could just do Application.Run() and that'd make the application run an empty message loop.Zandrazandt
U
0

From the same blog post @ChrisF mentioned https://bluehouse.wordpress.com/2006/01/24/how-to-create-a-notify-icon-in-c-without-a-form/

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();
        }
    }
}
Uranalysis answered 25/1, 2020 at 21:2 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.