将菜单项添加到任务托盘应用程序

时间:2017-08-21 12:22:04

标签: c# winforms

我有一个Windows窗体应用程序,在启动时不应该打开表单,而是坐在任务托盘中,当右键单击任务栏中的图标时,它应该显示菜单项。

此时我会调出2个菜单项,当点击菜单项时,它会打开相关的表单或按原样退出。

我想现在添加第三个菜单项,但由于某些原因,当我运行应用程序时它没有显示,右键单击,只有我添加的前两个是选项

我的App.Context看起来像这样

  public class AppContext : ApplicationContext
{
    NotifyIcon notifyIcon = new NotifyIcon();
    Integration configWindow = new Integration();
    receiving_Help helpWindow = new receiving_Help();
    public AppContext()
    {
        MenuItem configMentuItem = new MenuItem("RFID_Integration_Test", new EventHandler(ShowConfig));
        MenuItem exitMenuItem = new MenuItem("Exit", new EventHandler(Exit));
        MenuItem HelpMenuItem = new MenuItem("Help", new EventHandler(showHelp));//This item does not appear in the menu as an option



        NotifyIcon notifyIcon = new NotifyIcon();
        notifyIcon.Icon = SystemIcons.Asterisk;
        notifyIcon.ContextMenu = new ContextMenu(new MenuItem[] { configMentuItem, exitMenuItem });
        notifyIcon.Visible = true;

    }
    void ShowConfig(object sender, EventArgs e)
    {
        //If we are already showing the form, focus on it
        if (configWindow .Visible)
        {
            configWindow .Activate();
        }
        else
        {
           configWindow .ShowDialog();
        }
    }

    void showHelp(object sender, EventArgs e)
    {
        //If we are already showing the form, focus on it
        if (helpWindow.Visible)
        {
            helpWindow.Activate();
        }
        else
        {
            helpWindow.ShowDialog();
        }
    }

    void Exit(object sender, EventArgs e)
    {
        //Remove icon before we exit
        notifyIcon.Visible = false;
        Application.Exit();

    }

我尝试添加一个名为test的新菜单项,并将其链接到现有的退出事件处理程序,以查看是否可行,但它甚至没有将test作为选项显示。

我可以更新现有菜单项的名称,因此不是刷新问题

1 个答案:

答案 0 :(得分:0)

好的,事实证明我没有注意......

我创建了菜单Item,但我没有将它添加到NotifyIcon上下文菜单中。

notifyIcon.ContextMenu = new ContextMenu(new MenuItem[] { configMentuItem, exitMenuItem, HelpMenuItem });