Start Windows Forms program in the background

2

I developed a monitoring application using Windows Forms, but it does not need to have anything visual at the moment, so I'd like it to run when it runs in the background (I'd like the icon to stay where the icons are viruses, etc.).

I do not have much experience with Desktop applications and I can not find a way to do this, could anyone help?

    
asked by anonymous 01.08.2017 / 16:17

2 answers

6

Minimizing the application for the system tray is done with the control NotifyIcon of Visual Studio.

NotifyIcon is in the System.Windows.Forms namespace .

1. NotifyIcon Control

Drag and drop the NotifyIcon control to your form and put the notifyIcon1 name.

2. Changing the NotifyIcon icon

We need to change the NotifyIcon icon so that it appears in the system tray, otherwise nothing will show. To do this, put the following line of code in the constructor of your form:

public Form1()
{
    InitializeComponent();
    notifyIcon1.Icon = new Icon(GetType(), "placeholder.ico");
}

The icon must be a .ico and should be set to EmbeddedResource in the icon properties after adding it to the project.

3. Configuring form

On the .cs of your Form, enter the following:

private bool allowVisible;

protected override void SetVisibleCore(bool value) {
    if (!allowVisible) {
        value = false;
        if (!this.IsHandleCreated) CreateHandle();
    }
    base.SetVisibleCore(value);
}

protected override void OnFormClosing(FormClosingEventArgs e) {
    if (!allowClose) {
        this.Hide();
         e.Cancel = true;
    }
    base.OnFormClosing(e);
}

3. Showing the application

Place the code below in the DoubleClick event of the NotifyIcon control to display your application by double-clicking the icon in the system tray :

private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
     allowVisible = true;
     Show();
}

Once you start your application, it will go straight to the system tray with the icon you defined earlier. To open the application, simply double click on the icon in the system tray .

    
01.08.2017 / 16:40
4

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.
01.08.2017 / 16:47