3

I have a simple tray application in c#. What i want to do is to open the context menu on left click. At the moment it is only opening at the right click. This seems to be the standart behaviour.

I managed to react on left click but i don't know how to open the contextMenu programatically. Any Ideas?

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using System.Threading;
  using System.Drawing;


  namespace trackingCore
  {
  static class Program
  {
      /// <summary>
      /// Der Haupteinstiegspunkt für die Anwendung.
      /// </summary>
      [STAThread]
      static void Main()
      {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          NotifyIcon notifyIcon1 = new NotifyIcon();
          ContextMenu contextMenu1 = new ContextMenu();
          MenuItem menuItem0 = new MenuItem();
          MenuItem menuItem1 = new MenuItem();
          MenuItem menuItem2 = new MenuItem();
          contextMenu1.MenuItems.AddRange(new MenuItem[] { menuItem0 });
          contextMenu1.MenuItems.AddRange(new MenuItem[] { menuItem1 });
          contextMenu1.MenuItems.AddRange(new MenuItem[] { menuItem2 });
          menuItem0.Index = 0;
          menuItem0.Text = "open test";
          menuItem0.Click += new EventHandler(menuItem1_Click);
          menuItem1.Index = 1;
          menuItem1.Text = "stop test";
          menuItem1.Click += new EventHandler(menuItem1_Click);
          menuItem2.Index = 2;
          menuItem2.Text = "close test";
          menuItem2.Click += new EventHandler(menuItem1_Click);
          notifyIcon1.Icon = new Icon("test.ico");
          notifyIcon1.Text = "testitest";
          notifyIcon1.ContextMenu = contextMenu1;
          notifyIcon1.Click += new EventHandler(menuItem1_Click);
          notifyIcon1.Visible = true;
          Application.Run();
          notifyIcon1.Visible = false;
      }
      private static void menuItem1_Click(object Sender, EventArgs e)
      {
          Application.Exit();
      }

      private static void iconClick(object Sender, EventArgs e)
      {
          Console.Write("open context menu with left click");
      }
  }
}

That are some solutions that say contextMenu1.Show would be the solution. However the show functions needs two parametern and I can't tell which ones would be correct. It used to be only one paramter. Any ideas?

Silve2611
  • 2,198
  • 2
  • 34
  • 55

1 Answers1

2

At least some of the old answers should work. Just tested my old app on windows 10. Adjusted code a little for the answer.

public class ProcessIcon : IDisposable
{
    public NotifyIcon Icon { get; set; }

    public ProcessIcon()
    {
        Icon = new NotifyIcon();
    }

    public void Display()
    {
        Icon.MouseClick += OnIconMouseClick;
        Icon.Text = "Some text";
        Icon.Icon = new Icon(SystemIcons.Application, 40, 40);
        Icon.Visible = true;

        // Here you insert all your items like you did with ContextMenu
        // I am not even sure what's the diffrence
        var contenxtMenu = new ContextMenuStrip();
        Icon.ContextMenuStrip = contenxtMenu;
    }

    public void Dispose()
    {
        Icon.Dispose();
    }

    private void OnIconMouseClick(object sender, MouseEventArgs e)
    {
        // Works for me
        Icon.ContextMenuStrip.Show(Cursor.Position.X, Cursor.Position.Y);
    }
}
Alex Logvin
  • 766
  • 10
  • 12
Curly Brace
  • 515
  • 3
  • 13
  • thx for the answer. It is working but I wanted to use the ContextMenu, not the ContextMenuStrip. `Icon.ContextMenuStrip.Show(Cursor.Position);` is also working without X and Y – Silve2611 May 14 '18 at 17:12