If you take a quick look at the Program.cs file of your project, you'll probably see something like:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
Emphasis on:
Application.Run(new Form1());
The Run
method has more than one overhead, and the default is not to receive any parameters. Please remove your form, ie change the code to:
Application.Run();
And see how your application starts without any open windows.
You also will not get anything on the taskbar, so just doing this gets complicated even closing the program. You will only be able to close it with the task monitor ( Ctrl ALT DEL ).
Ok, let's make some more changes. Add the following namespaces:
using System.Drawing; // esse é só pra encurtar o uso de uma classe de ícone
using System.reflection; // esse a gente usa pra pegar o ícone da aplicação
Now add a notification icon. It is an object of class System.Windows.Forms.NotifyIcon
and you do not have to be stuck with any form:)
NotifyIcon ni = new NotifyIcon()
{
Icon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location), // pega o ícone da aplicação
Text = "hello",
Visible = true // porque o padrão para "Visible" é falso
};
The complete code looks like this:
using System;
using System.Drawing;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Whatever
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
NotifyIcon ni = new NotifyIcon()
{
Icon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location),
Text = "hello"
};
Application.Run();
}
}
}
I leave it to you now:
- Run any logic of the other classes that you have implemented;
- Add events to the notification icon to open and close your forms;
- Treat the application close to remove it from the system tray.