c#中的简单托盘应用程序

时间:2018-05-13 22:54:14

标签: c# .net visual-studio visual-studio-2017

我在c#中有一个简单的托盘应用程序。我想要做的是左键单击打开上下文菜单。目前它只是在右键单击时打开。这似乎是标准行为。

我设法对左键单击作出反应,但我不知道如何以编程方式打开contextMenu。任何想法?

  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");
      }
  }
}

这是一些解决方案,表示contextMenu1.Show将是解决方案。然而,show函数需要两个参数,我不知道哪些是正确的。它曾经只是一个参数。有任何想法吗?

1 个答案:

答案 0 :(得分:1)

至少有一些旧的答案应该有效。刚刚在Windows 10上测试了我的旧应用程序。调整了一些代码以获得答案。

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.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);
    }
}