Hide Menu Tray Icon

2

I created a menu for my TrayIcon :

Butiftheuserdoesnotselectanoptionitisstillvisible.WouldanyoneknowhowIcanhideincasetheuserclicksoutsidethemenuarea?

Here'sthecodeI'musingtoparsethemenu:

System.Windows.Forms.NotifyIconiconeTaskBar=null;privatevoidConfigurarIconeTaskBar(){iconeTaskBar=newSystem.Windows.Forms.NotifyIcon();StreamiconStream=Application.GetResourceStream(newUri("pack://application:,,,/IMG;component/Imagens/locationICO.ico")).Stream;
    iconeTaskBar.Icon = new System.Drawing.Icon(iconStream);
    iconeTaskBar.DoubleClick += iconeTaskBar_DoubleClick;
    iconeTaskBar.Text = "IMG";
    iconeTaskBar.MouseDown += new   System.Windows.Forms.MouseEventHandler(iconeTaskBar_MouseDown);
}

private void iconeTaskBar_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {
        ContextMenu menu = new ContextMenu();

        MenuItem menuItemVouPara = new MenuItem() { Header = "Cadastrar saída" };
        menuItemVouPara.Click += menuItemVouPara_Click;
        menuItemVouPara.Icon = new System.Windows.Controls.Image
        { Source = new BitmapImage(new Uri("pack://application:,,,/Imagens/voupara.png", UriKind.Absolute)) };

        menu.Items.Add(menuItemVouPara);

        menu.IsOpen = true;
    }
}
    
asked by anonymous 17.04.2015 / 20:48

1 answer

1

I think the problem is that you are opening the Menu .

The menu should be opened by NotifyIcon . After building the menu you should associate it to it through the ContextMenu property. Doing so will open the Menu when you right-click the icon.

The following code creates an icon in the Task Bar that will display a menu with the Sign Out item. The icon will disappear when the Form is closed.

If you need the menu to fit according to the application's context (context), use the ContextMenu.Popup Event to build the menu items.

public partial class MainWindow : Window
{
    private System.Windows.Forms.NotifyIcon iconeTaskBar;
    private System.ComponentModel.IContainer components;

    public MainWindow()
    {
        InitializeComponent();
        ConfigurarIconeTaskBar();
    }

    private void ConfigurarIconeTaskBar()
    {
        components = new System.ComponentModel.Container();

        //Use o construtor System.Windows.Forms.NotifyIcon(IContainer) para eliminar
        // a instância do NotifyIcon quando a Form é fechada. 
        iconeTaskBar = new System.Windows.Forms.NotifyIcon(components);
        iconeTaskBar.Icon = new System.Drawing.Icon(@"C:\Windows\System32\PerfCenterCpl.ico");
        iconeTaskBar.Text = "IMG";

        //Crie um Menu
        var menu = new System.Windows.Forms.ContextMenu();
        //Crie MenuItems
        var menuItemVouPara = new System.Windows.Forms.MenuItem() {Text = "Cadastrar saída"};
       //Associe o Click event
        menuItemVouPara.Click += menuItemVouPara_Click;
       //associo-o ao Menu
        menu.MenuItems.Add(menuItemVouPara);

        //Associe esse menu à propeiedade ContextMenu do NotifyIcon
        iconeTaskBar.ContextMenu = menu;
        //Torne o NotifyIcon visível
        iconeTaskBar.Visible = true;
    }

    protected override void OnClosed(EventArgs e)
    {
        // Faça o Dispose do IContainer que passou no contrutor de NotifyIcon.
        // A instância de NotifyIcon será eliminada quando garbage collection libertar o Container
        if (components != null)
            components.Dispose();
        base.OnClosed(e);
    }
}
    
22.04.2015 / 15:20