How to make notification through the taskbar in c#?

Asked

Viewed 1,234 times

3

For my application I’m developing I’m creating a icon which will be on the taskbar to show the notifications to the user and within it the user will have options such as : Quit, Open, Configure and so on, but I have no idea how to do this.

I managed to create the icon that will be next to the icons for example sound and wi-fi, but when minimizing the program it disappears from the taskbar and only gets the icon.

My code :

 private void FrmIntegracaoPrincipal_Load(object sender, EventArgs e)
        {
            /* Minimizando aplicação na bandeja do windows */
            this.Visible = false;
            this.ShowInTaskbar = false;
            this.WindowState = FormWindowState.Minimized;
            notifyIcon1.Visible = true;
        }

        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            /* Exibindo novamente o programa */
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
            this.ShowInTaskbar = true;
            notifyIcon1.Visible = false;
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Visible = false;
                this.ShowInTaskbar = false;
                this.WindowState = FormWindowState.Minimized;
                notifyIcon1.Visible = true;
            }

            //Verificando se WindowsState == Minimized
            if (this.WindowState == FormWindowState.Minimized)
            {
                //CODIGO 
            }
        }

How can I do this ? If anyone can help me I’d appreciate it.

1 answer

5


You need to add one Contextmenustrip and its application, and then set it in the property ContextMenuStrip of Notifyicon, see in this example:

// 
// notifyIcon
// 
this.notifyIcon.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info;
this.notifyIcon.BalloonTipText = "Notificação exemplo";
this.notifyIcon.ContextMenuStrip = this.contextMenuStrip;
this.notifyIcon.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon.Icon")));
this.notifyIcon.Text = "Aplicação";
this.notifyIcon.Visible = true;
// 
// contextMenuStrip
// 
this.contextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.exibirToolStripMenuItem,
this.fecharToolStripMenuItem});
this.contextMenuStrip.Name = "contextMenuStrip";
this.contextMenuStrip.Size = new System.Drawing.Size(153, 70);
// 
// exibirToolStripMenuItem
// 
this.exibirToolStripMenuItem.Name = "exibirToolStripMenuItem";
this.exibirToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.exibirToolStripMenuItem.Text = "Exibir";
this.exibirToolStripMenuItem.Click += new System.EventHandler(this.exibirToolStripMenuItem_Click);
// 
// fecharToolStripMenuItem
// 
this.fecharToolStripMenuItem.Name = "fecharToolStripMenuItem";
this.fecharToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.fecharToolStripMenuItem.Text = "Fechar";
this.fecharToolStripMenuItem.Click += new System.EventHandler(this.fecharToolStripMenuItem_Click);

This menu has two options, which are Exibir and Fechar, the code associated to the click of each and the form follows below:

using System;
using System.Windows.Forms;

namespace NotificacaoBarraTarefasExemplo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {                
                this.Hide();
            }
        }

        private void exibirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
        }

        private void fecharToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

You can add as many menus as you want to on ContextMenuStrip, the important thing is that your ContextMenuStrip is defined in NotifyIcon.

Source: https://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.contextmenu.aspx

  • It worked, thank you very much, I already gave +1 and also as a response that helped in my problem.

  • But how could it cause when there is an update or changes in the application settings,?

  • @Falion here you can implement using Notifyicon itself, if you want something more customized has this library that can help you.

  • I tested and it didn’t work,it doesn’t appear the way I want,appearing the message with a balloon coming out of the icon,it appears the message if you put the cursor over the icon.

  • The lib I posted there, she’s the one you tested?

  • 1

    I tested another similar code and it worked normally, thank you :)

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.