Hide Tray Icon Menu

Asked

Viewed 372 times

2

I created a menu for my TrayIcon:

inserir a descrição da imagem aqui

However, if the user does not select an option, it is still visible. Would anyone know how to hide if the user clicks outside the menu area?

Here’s the code I’m using for the menu:

System.Windows.Forms.NotifyIcon iconeTaskBar = null;

private void ConfigurarIconeTaskBar()
{
    iconeTaskBar = new System.Windows.Forms.NotifyIcon();
    Stream iconStream = Application.GetResourceStream(new      Uri("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;
    }
}
  • Trayicon with WPF? What api are you using?

  • None, I did with c# using reference to TrayIcon windows Forms.

  • @ramaral, I did more or less like this on this site --> https://ebbypeter.wordpress.com/2010/06/28/minimize-a-wpf-application-to-system-tray-in-c/

  • If you’re wearing one System.Windows.Forms.Contextmenu() and then associates it with Notifyicon through NotifyIcon.ContextMenu, the Contextmenu should close by him.

  • @ramaral, I added the code to the question, how I’m doing to create the menu.

1 answer

1


I think the problem is that you open the Menu.

The menu opening should be done by Notifyicon. After building the menu you should associate it to it through the property Contextmenu.
Thus making the Notifyicon will open the Menu when you click the right mouse button on the icon.

The following code creates an icon in the Task Bar which will display a menu with the item Register exit. The icon will disappear when the Form is closed.

If you need the menu to adapt to the application’s state(context), use the Contextmenu.Popup Event to build 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);
    }
}
  • Thank you very much, I work right, have how I put an image in each menu item?

  • Instead of class System.Windows.Forms.ContextMenu use System.Windows.Forms.Contextmenustrip then use iconeTaskBar.ContextMenuStrip = menu;

  • Look at this reply in the Soen.

  • Thank you very much!

Browser other questions tagged

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