gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:40:53 33 4
gpt4 key购买 nike

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

我设法对左键单击使用react,但我不知道如何以编程方式打开上下文菜单。有什么想法吗?

  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 函数需要两个参数,我不知道哪个是正确的。它曾经只有一个参数。有任何想法吗?

最佳答案

至少一些旧的答案应该有效。刚刚在 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.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);
}
}

关于c# - c#中的简单托盘应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50321295/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com