System tray icon with c# Console Application won't show menu

前端 未结 4 1409
名媛妹妹
名媛妹妹 2021-01-02 15:32

I\'ve got a small C# (.NET 4.0) Console Application that I\'d like the user to be able to interact by showing a menu when they right-click the System Tray icon. I can add an

相关标签:
4条回答
  • 2021-01-02 16:02

    Concerning Application.Run(), this is an alternative to placing all the other code in another thread would be to create the NotifyIcon, menu, events, etc on a thread other than the main thread.

    This should include Application.Run() as this allows the standard application message loop to work on the current thread. Then since the events were created on the same thread, the Application.Exit() can be used to close out the notification messaging but still allow the main thread to continue. Here's a small example for a console app...

    class Program 
    {
        public static ContextMenu menu;
        public static MenuItem mnuExit;
        public static NotifyIcon notificationIcon;
    
        static void Main(string[] args)
        {
            Thread notifyThread = new Thread(
                delegate()
                {
                    menu = new ContextMenu();
                    mnuExit = new MenuItem("Exit");
                    menu.MenuItems.Add(0, mnuExit);
    
                    notificationIcon = new NotifyIcon()
                    {
                        Icon = Properties.Resources.Services,
                        ContextMenu = menu,
                        Text = "Main"
                    };
                    mnuExit.Click += new EventHandler(mnuExit_Click);
    
                    notificationIcon.Visible = true;
                    Application.Run();
                }
            );
    
            notifyThread.Start();
    
            Console.ReadLine();          
        }
    
        static void mnuExit_Click(object sender, EventArgs e)
        {
            notificationIcon.Dispose();
            Application.Exit();
        }
    
    }
    
    0 讨论(0)
  • 2021-01-02 16:11

    Did you add the event-handler for tray Icon mouse click?

    trayIcon .MouseDown += new MouseEventHandler(trayIcon_MouseDown);
    

    create context menu and do as following inside the trayIcon_MouseDown function

    private void trayIcon_MouseDown (object sender,MouseEventArgs e)
    {
      //add you menu items to context menu
      contextMenu.Items.Add(item);
      contextMenu.IsOpen = true;  
    }
    

    Try this. Think this will help you.

    0 讨论(0)
  • 2021-01-02 16:17

    Here is the solution: You have to use Application.Run() because events of gui in console mode not working. But you can use this solution:

    var task = System.Threading.Tasks.Task.Factory.StartNew(() => ShowTrayIcon());
    
    void ShowTrayIcon()
    {
        some code with tray icon ...
    }
    

    This will start your setup of try icon in new thread ...

    0 讨论(0)
  • 2021-01-02 16:21

    Try adding this after you create the icon:

    Application.Run()
    

    Note that this method will not return, so you can't do anything after calling it. This means that you'll have to do all your other work in a separate thread.

    What happens is that the OS sends your application a message telling it that the tray icon has been right-clicked, but the tray icon code never sees it (because these messages are processed by Application.Run) and so can't respond by opening the menu.

    0 讨论(0)
提交回复
热议问题