How to make notification through the taskbar in C #?

3

For my app I'm developing I'm creating a icon that will stay on the taskbar to show the notifications to the user and inside it the user will have options like: Exit, Open, Configure and etc, but I have no ideas how to do this.

I have been able to create the icon that will stay on the side of icons, for example sound and wi-fi, but by minimizing the program it disappears from the taskbar and only the icon remains.

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 appreciate it.

    
asked by anonymous 29.05.2016 / 19:23

1 answer

5

You need to add a ContextMenuStrip and its application, and then set it to the ContextMenuStrip property of NotifyIcon , look at 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 with the click of each one and the form follows:

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 in ContextMenuStrip , the important thing is that your ContextMenuStrip is set to NotifyIcon .

Source: link

    
29.05.2016 / 23:40