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

拈花ヽ惹草 提交于 2019-11-29 20:48:46

问题


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 icon to the Tray with no problems, but I just cannot get the menu to appear. I'm using the following code:

NotifyIcon trayIcon = new NotifyIcon();
trayIcon.Text = "TestApp";
trayIcon.Icon = new Icon(SystemIcons.Application, 40, 40);

ContextMenu trayMenu = new ContextMenu();

trayMenu.MenuItems.Add("Blah", item1_Click);
trayMenu.MenuItems.Add("Blah2", item1_Click);
trayMenu.MenuItems.Add("Blah3", item1_Click);

trayIcon.ContextMenu = trayMenu;
trayIcon.Visible = true;

... which puts the icon in the tray. However, right-clicking the icon does nothing. I've tried various permutations of MenuItems.Add, but nothing will make the menu appear. I'm sure I'm missing something simple - any ideas what?


回答1:


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.




回答2:


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

}



回答3:


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 ...




回答4:


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.



来源:https://stackoverflow.com/questions/12817468/system-tray-icon-with-c-sharp-console-application-wont-show-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!